我正在使用okhttp3将视频从Android手机发送到Java servlet。当我发送多个文件时,会发送第一对文件,但在某些时候我开始得到这个例外:
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
为什么会这样?我该如何解决?
----------------------------- Edit 8/10/17 ------------ ---
弄清楚它不是服务器而是android问题,因为如果我对servlet中的所有内容进行评论,就会发生同样的问题。
答案 0 :(得分:0)