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

时间:2012-05-30 15:40:01

标签: java android google-app-engine servlets

在我的应用中,我使用MultiPartEntity将Bitmap上传到服务器。

代码:

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);
    .
    .
}

从请求中获取Bitmap并将其存储在blob中。

守则:

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();
    }
}

我的问题是:如何使用回复将其发回客户端?感谢。

2 个答案:

答案 0 :(得分:0)

发回回复的链接并从收到的链接发起下载。

答案 1 :(得分:0)

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

Serving Dynamic Images with Google App Engine (Java)

的更多信息