[英]How to send Bitmap from server to client?
在我的應用程序中,我正在使用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();
}
}
我的問題是:如何使用響應將其發送回客戶端? 謝謝。
發送回一個鏈接作為響應,並從收到的鏈接開始下載。
response.setContentType("image/png"); // fill proper image type
response.getOutputStream().write(IOUtils.toByteArray(imgStream));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.