简体   繁体   English

如何将位图从服务器发送到客户端?

[英]How to send Bitmap from server to client?

In my app I'm uploading a Bitmap to the server using MultiPartEntity. 在我的应用程序中,我正在使用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. 然后从请求中获取位图并将其存储在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) 有关使用Google App Engine(Java)提供动态图片的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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