【发布时间】:2017-07-03 14:23:45
【问题描述】:
我想将文件列表(在我的情况下为图像)从 JBoss 服务器上传到 android。 我是通过下面的书面代码这样做的:
@GET
@Path("/report/{filename}")
@Produces({MediaType.MULTIPART_FORM_DATA})
public MultipartFormDataOutput getReport(@PathParam("filename") String filename, @Context HttpServletRequest request) {
try {
String token = request.getHeader("Authorization");
List<File> file = processImage.getImage(filename,token);
MultipartFormDataOutput output = new MultipartFormDataOutput();
for(File image: file){
System.out.println("class of this" +image + "MMMM" +image.exists());
output.addPart(image, new MediaType("image","jpg"));
}
return output;
} .....
.......
}
在 Android 端,我想读取响应(多部分形式的文件)。我正在使用 okHttp 进行连接。 在互联网上搜索了很多我尝试下面的代码来读取多部分响应,但它不起作用。似乎没有从流中读取任何内容。
ByteArrayDataSource ds = new ByteArrayDataSource(response.body().byteStream(), "multipart/form-data");
MimeMultipart multipart = new MimeMultipart(ds);
BodyPart jsonPart = multipart.getBodyPart(0);
System.out.println("Response body = " + jsonPart.getInputStream());
File reportFile = new File(Environment.getExternalStorageDirectory() + File.separator + "downloadedFile.jpg");
InputStream is = jsonPart.getInputStream();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(reportFile);
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while ((bufferLength = is.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bufferLength);
}
is.close();
fileOutputStream.close();
.......
}
谁能帮我解决这个问题。我被困在这里两天了。我做错了什么。
【问题讨论】:
-
能否显示这一行输出 System.out.println("Response body = " + jsonPart.getInputStream());
-
@AjayPandya 和 AndyDeveloper,你们都指向多部分请求的解决方案。问题是关于如何处理多部分响应。我希望我有答案...我在这里寻找相同问题的答案。
-
已经有解决方案了:)
标签: android jax-rs multipartform-data resteasy okhttp