2017-08-09 56 views
0

我使用okhttp3從Android手機發送視頻到Java servlet。當我發送多個文件的第一對夫婦的文件發送,但在某些時候,我開始得到此異常:Android okhttp3:預計2030字節但收到2362

java.net.ProtocolException: expected 2030 bytes but received 2362 
at okhttp3.internal.http.Http1xStream$FixedLengthSink.write(Http1xStream.java:283) 

我的Android代碼來發送視頻文件是:

import android.util.Log; 

import java.io.File; 
import java.io.IOException; 
import java.util.HashMap; 
import java.util.Set; 

import okhttp3.MediaType; 
import okhttp3.MultipartBody; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.RequestBody; 
import okhttp3.Response; 

public class DataTransfer { 
    public static void sendFile(final String method, final String path, final String fileSubType, final HashMap<String, String> hm) { 
     Thread t = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       OkHttpClient client = new OkHttpClient(); 

       MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM); 
       builder.addFormDataPart("file", path, RequestBody.create(MediaType.parse(fileSubType), new File(path))); 

       Set<String> set = hm.keySet(); 
       for (String s : set) { 
        builder.addFormDataPart(s,hm.get(s)); 
       } 

       RequestBody request_body = builder.build(); 

       try { 
        Log.e(Constants.TAG, "++++++++++++++++++++++\n\n\nSIZE = " + request_body.contentLength() + "\n\n\n====================================\n"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Request request = new Request.Builder() 
         .url(Constants.URL + "/" + method) 
         .post(request_body) 
         .build(); 

       try { 
        Response response = client.newCall(request).execute(); 
        Log.d(Constants.TAG, "+++++++++++++++++++++\n" + response.body().toString() + "==========================================="); 
        if(!response.isSuccessful()){ 
         throw new IOException("Error : "+response); 
        } 
       } catch (IOException e) { 
        Log.d(Constants.TAG, Log.getStackTraceString(e)); 
       } 
      } 
     }); 

     t.start(); 
    } 
} 

的Java servlet代碼,即可獲得文件:

@RequestMapping(value="/dataTransfer", method = RequestMethod.POST) 
    protected void dataTransfer(HttpServletRequest request, HttpServletResponse response) throws Exception {   
     try { 
      List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); 
      List<File> files = new ArrayList<File>();//Holds all the files that were received 
      HashMap<String, String> map = new HashMap<String, String>(); 
      for (FileItem item : items) { 
       if (item.isFormField()) { 
        // Process regular form field (input type="text|radio|checkbox|etc", select, etc). 
        String fieldName = item.getFieldName(); 
        String fieldValue = item.getString(); 

        map.put(fieldName, fieldValue); 
       } else { 
        // Process form file field (input type="file"). 
        String fileName = FilenameUtils.getName(item.getName());//If the name was not specified set default 

        String filePath = Constants.ORIGINAL_DIRECTORY_PATH + fileName; 
        File storeFile = new File(filePath); 
        item.write(storeFile); 

        files.add(storeFile); 
       } 
      } 

      //Do other stuff to file 
     } catch (FileUploadException e) { 
      throw new ServletException("Cannot parse multipart request.", e); 
     } 
    } 

而在服務器端我收到此錯誤信息:

org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly 

這是怎麼發生的?我該如何解決它?

-----------------------------編輯於8/10/17 ----------- ----

發現它不是一個服務器,而是一個android問題,因爲如果我從servlet註釋掉所有內容,就會發生同樣的問題。

回答

0

通常情況下會出現這種錯誤,因爲您的線程在緩衝區刷新之前退出。

你可以通過puttying一個finally代碼塊來優化關閉流/文件,好像你在服務器中丟失了一個。

this answer ..

+0

我沒有任何打開的流/文件中,在我的文件我的服務器的唯一的地方/ O是'item.write(storeFile);'但是我不能關閉這個。 – develop1

相關問題