1

在我的应用程序中,我使用 MultiPartEntity 将位图上传到服务器。

编码:

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

并从请求中获取位图并将其存储在 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();
    }
}

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

4

2 回答 2

0

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

于 2012-05-30T15:49:19.753 回答
0
response.setContentType("image/png"); // fill proper image type
response.getOutputStream().write(IOUtils.toByteArray(imgStream));

使用 Google App Engine (Java) 提供动态图像的更多信息

于 2012-05-30T15:51:52.850 回答