无法将图像文件从 Android 应用程序发布到 Tomcat 上托管的 Java servlet
我正在开发的 Android 应用程序的一个功能是拍照并通过 HTTP Post 将其上传到 Java servlet。我找到了许多关于一般过程应该如何工作的示例,并尝试了所有这些示例。目前,代码如下所示:
Client:
String fileName = pathToFile;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(path);
MultipartEntity entity = new MultipartEntity();
FileBody fileBody = new FileBody(new File(fileName));
entity.addPart("file", fileBody);
method.setEntity(entity);
HttpResponse response = httpclient.execute(method);
Server:
List<FileItem> fileItems =
new ServletFileUpload( new DiskFileItemFactory( 1024 * 1024, new File("C:\\tmp" ))).
parseRequest(request);
for ( FileItem item : fileItems ) {
String fieldName = item.getFieldName();
if ( item.isFormField()) {
item.getString();
}
else {
item.getInputStream();
} // File uploaded
}
现在,我不担心如何处理输入流,因为当我们调用 parseRequest(request) 并出现以下错误时,此代码会失败: org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException:流意外结束
我是 Apache HttpUpload 库的新用户。我在这里缺少什么?
提前致谢。
One feature of an Android app I am working on is to take a picture and upload it via HTTP Post to a Java servlet. I have found a number of examples of how the general process should work, and tried all of them. Currently, the code looks like this:
Client:
String fileName = pathToFile;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(path);
MultipartEntity entity = new MultipartEntity();
FileBody fileBody = new FileBody(new File(fileName));
entity.addPart("file", fileBody);
method.setEntity(entity);
HttpResponse response = httpclient.execute(method);
Server:
List<FileItem> fileItems =
new ServletFileUpload( new DiskFileItemFactory( 1024 * 1024, new File("C:\\tmp" ))).
parseRequest(request);
for ( FileItem item : fileItems ) {
String fieldName = item.getFieldName();
if ( item.isFormField()) {
item.getString();
}
else {
item.getInputStream();
} // File uploaded
}
Right now, I am not worried about what to do with the input stream, because this code fails when we call parseRequest(request) with this error:org.apache.tomcat.util.http.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
I am a new user of the Apache HttpUpload library. What am I missing here?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用构造函数:
默认使用 STRICT 模式,这可能是问题所在。
Try using the constructor :
The default is using mode STRICT and this may be the problem.