[英]how to display or use an image received from Android phone on the server
我正在將圖像從android手機發送到處理它的服務器,但是現在我對如何在服務器中使用該圖像感到困惑
我發送圖像的android手機的代碼是
Log.i("sAMPLE","Info:" );
//String postURL = HOST_SERVER_URL + HOST_PHOTO_UPLOAD_URI;
String postURL ="http://10.0.2.2:8080/SimpleServlet/simple-servlet";//server URL
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(postURL);
ByteArrayBody bab = new ByteArrayBody(imageBytes, "file_name_ignored");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("source", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
我處理服務器中圖像的代碼是這樣的
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
System.out.println("Before Mutlipart");
if(!isMultipart)
throw new ServletException("upload using multipart");
ServletFileUpload upload = new ServletFileUpload(fif);
upload.setSizeMax(1024 * 1024 * 10 /* 10 mb */);
List<FileItem> items;
try {
items = upload.parseRequest(req);
//}// catch (FileUploadException e) {
// throw new ServletException(e);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
throw new ServletException(e);
}
if(items == null || items.size() == 0)
throw new ServletException("No items uploaded");
FileItem item = items.get(0);
//BufferedImage Img=item.getString();
System.out.println(item.getContentType());
byte[]data=item.get();
現在我該如何使用字節數組在服務器上顯示圖像或使用其他內容(例如字符串,其他圖像等)來編輯圖像。
我認為這應該對您有用...
http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/
在服務器端(imageInByte是您的數據[])...
//convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg",
new File("c:\\image\\mypic_new.jpg"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.