I want to upload list of files (images in my case) from JBoss server to android. I am doing so by the below written code:
@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;
} .....
.......
}
On Android side I want to read the response (the files in multipart form). I am using okHttp to make the connection. Searching a lot on internet I tried the below code to read the multipart response, but it is not working. It seems that is not reading anything from the stream.
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();
.......
}
Can anyone please help me solving this. I am stuck here from 2 days. What am I doing wrong .