gpt4 book ai didi

android - 如何使用okHttp读取从resteasy发送到android的多部分响应?

转载 作者:太空狗 更新时间:2023-10-29 13:53:38 25 4
gpt4 key购买 nike

我想从 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();
.......
}

谁能帮我解决这个问题。我从 2 天起就被困在这里。我做错了什么。

最佳答案

您可以执行以下操作:

  • 创建一个在后台运行的异步任务
  • 使用 okhttp3.Request 创建请求
  • 创建OkHttpClient发送请求并将响应保存在okHttp3 Response中
  • 然后从 okhttp3.ResponseBody 获取响应主体,并使用 InputStreamReader 和 BufferedReader 读取文件。

像下面这样的东西应该可以工作:

private class FileTask extends AsyncTask<Void, Void, Void> {
private File file = null;
@Override
protected Void doInBackground(Void... params) {

okhttp3.Request request = new okhttp3.Request.Builder()
.url("Your URL for getReport()")
.get()
.build();
OkHttpClient okHttpClient = new OkHttpClient.Builder().
connectTimeout(80, TimeUnit.SECONDS)
.writeTimeout(80, TimeUnit.SECONDS)
.readTimeout(80, TimeUnit.SECONDS)
.build();


try {
Response response = okHttpClient.newCall(request).execute();
okhttp3.ResponseBody in = response.body();
InputStream is = in.byteStream();
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader buffReader = new BufferedReader(inputStreamReader);

file = new File(Environment.getExternalStorageDirectory() + File.separator + "filename.txt"); //Your output file
OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(output);


String line=buffReader.readLine();
while ((line=buffReader.readLine()) != null) {
output.write(line.getBytes());
output.write('\n');
}
buffReader.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
//do post execution steps
}
}
}

关于android - 如何使用okHttp读取从resteasy发送到android的多部分响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com