Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓上传删除字节_Android_File_Video_Upload - Fatal编程技术网

Android 安卓上传删除字节

Android 安卓上传删除字节,android,file,video,upload,Android,File,Video,Upload,我正在尝试从我的android手机(三星Galaxy s2)上传4.1.2版的视频 我已经配置了一个apache服务器来承载上传的视频。我的问题是:每次我从手机上传视频时,我都有一个损坏的文件。服务器上的文件比手机文件少几个字节。它不是来自服务器,因为我使用html表单做了一些测试,一切都很顺利。所以我猜,它来自我的代码,但我就是不知道在哪里。谁能帮帮我吗,我已经花了好几天时间了 这是我的代码: @凌驾 公共布尔上传文件(){ HttpURLConnection=null; DataOutput

我正在尝试从我的android手机(三星Galaxy s2)上传4.1.2版的视频

我已经配置了一个apache服务器来承载上传的视频。我的问题是:每次我从手机上传视频时,我都有一个损坏的文件。服务器上的文件比手机文件少几个字节。它不是来自服务器,因为我使用html表单做了一些测试,一切都很顺利。所以我猜,它来自我的代码,但我就是不知道在哪里。谁能帮帮我吗,我已经花了好几天时间了

这是我的代码: @凌驾 公共布尔上传文件(){

HttpURLConnection=null;
DataOutputStream outputStream=null;
字符串pathToOurFile=mMedia.getLocalPath();
字符串urlServer=UploadConf.UPLOAD\u SERVER;
字符串lineEnd=“\r\n”;
字符串双连字符=“--”;
字符串边界=“*******”;
int字节读取、字节可用、缓冲区大小、总字节读取;
字节[]缓冲区;
int maxBufferSize=UploadConf.MAX\u BUFFER\u SIZE;
试一试{
文件文件=新文件(pathToOurFile);
FileInputStream FileInputStream=新的FileInputStream(文件);
Log.i(Log_标记,“MediaUploader upload”+file.getName()+“=>”+file.getPath());
Map params=新的HashMap();
put(“mediatetitle”,file.getName());
URL URL=新URL(urlServer+getFormattedParams(params));
Log.i(Log_标记,“URL=>”+urlServer+getFormattedParams(params));
connection=(HttpURLConnection)url.openConnection();
//允许输入和输出
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(UploadConf.CHUNK\u SIZE);
//启用POST方法
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
outputStream=新的DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(两个连字符+边界+行结束);
outputStream.writeBytes(“内容处置:表单数据;名称=\“mediaFile\”+lineEnd+lineEnd+file.getName()+lineEnd”);
outputStream.writeBytes(两个连字符+边界+行结束);
outputStream.writeBytes(“内容配置:表单数据;名称=\”视频文件\“文件名=\”+文件.getName());
outputStream.writeBytes(lineEnd);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
//读取文件
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
totalBytesRead=字节读取;
而(字节读取>0)
{   
写入(缓冲区,0,缓冲区大小);
bytesAvailable=fileInputStream.available();
bufferSize=Math.min(字节可用,maxBufferSize);
bytesRead=fileInputStream.read(缓冲区,0,缓冲区大小);
Log.i(Log_标签,“Progress”+totalBytesRead+“Read”);
totalBytesRead+=字节读取;
//outputStream.flush();
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(两个连字符+边界+两个连字符+lineEnd);
//得到回应
InputStream is=connection.getInputStream();
BufferedReader rd=新的BufferedReader(新的InputStreamReader(is));
弦线;
StringBuffer响应=新的StringBuffer();
而((line=rd.readLine())!=null){
响应。追加(行);
append('\r');
}
rd.close();
//来自服务器的响应(代码和消息)
int serverResponseCode=connection.getResponseCode();
字符串serverResponseMessage=connection.getResponseMessage();
Log.d(Log_标签,“服务器响应代码”+serverResponseCode);
Log.d(Log_标签,“服务器响应消息”+服务器响应消息);
Log.d(Log_标记,“服务器响应”+Response.toString());
字符串结果=response.toString();
if(result==null | | result.equals(“”){
broadcastIntent(mContext.getString(R.string.upload\u失败\u通知)、PRIORITY.ERROR、fileInputStream、outputStream);
}
JSONObject json=null;
试一试{
json=新的JSONObject(结果);
}
捕获(JSONException e){
e(Log_标记,“不可能解析Json字符串”);
broadcastIntent(mContext.getString(R.string.upload\u失败\u通知)、PRIORITY.ERROR、fileInputStream、outputStream);
返回false;
} 
JSONObject jsonResult=json.getJSONObject(Constants.json_RESULT_标记);
if(jsonResult==null){
e(Log_标记,“Json结果为空”);
broadcastIntent(mContext.getString(R.string.upload\u失败\u通知)、PRIORITY.ERROR、fileInputStream、outputStream);
返回false;
}
JSONArray错误;
if(jsonResult.has(Constants.JSON_ERRORS_TAG)){
if((errors=jsonResult.getJSONArray(Constants.JSON\u errors\u TAG))!=null){
字符串通知=”;
对于(int i=0;i    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;

    String pathToOurFile = mMedia.getLocalPath();
    String urlServer = UploadConf.UPLOAD_SERVER;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize, totalBytesRead;
    byte[] buffer;
    int maxBufferSize = UploadConf.MAX_BUFFER_SIZE;

    try {
        File file = new File(pathToOurFile);
        FileInputStream fileInputStream = new FileInputStream(file);
        Log.i(LOG_TAG, "MediaUploader upload"  + file.getName() + " => "  +  file.getPath());

        Map<String, String> params = new HashMap<String, String>();
        params.put("mediaTitle", file.getName());

        URL url = new URL(urlServer + getFormattedParams(params));
        Log.i(LOG_TAG, "URL => " + urlServer + getFormattedParams(params));
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setChunkedStreamingMode(UploadConf.CHUNK_SIZE);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        outputStream = new DataOutputStream( connection.getOutputStream() );
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"mediaFile\"" + lineEnd + lineEnd + file.getName() + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"video_file\";filename=\"" + file.getName());
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        totalBytesRead = bytesRead;

        while (bytesRead > 0)
        {   
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            Log.i(LOG_TAG, "Progress " + totalBytesRead + " Read ");
            totalBytesRead += bytesRead;
            //outputStream.flush();
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        //Get Response  
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer(); 
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();


        // Responses from the server (code and message)
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();
        Log.d(LOG_TAG,"Server Response Code " + serverResponseCode);
        Log.d(LOG_TAG,"Server Response Message " + serverResponseMessage);

        Log.d(LOG_TAG,"Server Response " + response.toString());

        String result = response.toString();

        if (result == null || result.equals("")) {
            broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
        }

        JSONObject json = null;
        try {
            json = new JSONObject(result);
        }
        catch (JSONException e) {
            Log.e(LOG_TAG, "Impossible to parse Json String");
            broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
            return false;
        } 

        JSONObject jsonResult = json.getJSONObject(Constants.JSON_RESULT_TAG);

        if (jsonResult == null) {
            Log.e(LOG_TAG, "Json result is null ");
            broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, fileInputStream, outputStream);
            return false;
        }

        JSONArray errors;

        if (jsonResult.has(Constants.JSON_ERRORS_TAG)) {
            if ((errors = jsonResult.getJSONArray(Constants.JSON_ERRORS_TAG)) != null) {

                String notification = "";

                for (int i = 0; i < errors.length(); i++) {
                    JSONObject o = errors.getJSONObject(i);

                    notification +=  o.getString(Constants.JSON_ERROR_TAG);
                }
                broadcastIntent(notification, PRIORITY.ERROR, null, null);
                return false;
            }
        }

        mMedia.setSyncState(SyncState.SYNCED.ordinal());
        mMediaManager.updateMedia(mMedia);
        broadcastIntent(mContext.getString(R.string.upload_succeedeed_notification), PRIORITY.INFO, fileInputStream, outputStream);
        return true;
    } catch (UnknownHostException ex) {
        broadcastIntent(mContext.getString(R.string.upload_hostname_error_notification), PRIORITY.ERROR, null, null);
        Log.d(LOG_TAG, ex.getStackTrace().toString());
    } catch (Exception ex) {    
        broadcastIntent(mContext.getString(R.string.upload_failed_notification), PRIORITY.ERROR, null, null);
        Log.d(LOG_TAG,"Upload failed");
        ex.printStackTrace();
    }
    return false;
}