简体   繁体   中英

How to send Bitmap from server to client?

In my app I'm uploading a Bitmap to the server using MultiPartEntity.

The code:

public static boolean UploadFile(String stringUrl, byte[] bitmapArray) throws Exception { 
  String uploadUrl = stringUrl;
  HttpPost postRequest = new HttpPost(uploadUrl);
  MultipartEntity entity = new MultipartEntity();
  entity.addPart("image", new ByteArrayBody(bitmapArray, "image/png", "image"));
  postRequest.setEntity(entity);
  HttpResponse httpResponse;
  HttpClient httpClient = getHttpClient();
  httpClient.getParams().setBooleanParameter("http.protocol.handle-redirects",false);
  httpResponse = httpClient.execute(postRequest);
    .
    .
}

And getting the Bitmap from the request and storing it in a blob.

The Code:

private void handleFileUpload(HttpServletRequest request, HttpServletResponse response) {
  try {
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iter = upload.getItemIterator(request);
    FileItemStream imageItem = iter.next();
    InputStream imgStream = imageItem.openStream();
    Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
    MyImage myImage = new MyImage(imageItem.getName(), imageBlob);
      .
      .
    } catch (FileUploadException e) {       
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
}

My question is: How can I send it back to the client using the response? Thanks.

发送回一个链接作为响应,并从收到的链接开始下载。

response.setContentType("image/png"); // fill proper image type
response.getOutputStream().write(IOUtils.toByteArray(imgStream));

more info at Serving Dynamic Images with Google App Engine (Java)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM