在我的应用程序中,我使用 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();
}
}
我的问题是:如何使用响应将其发送回客户端?谢谢。