I am sending an image from android phone to the server which hanldes it ,but now i am confused on how to use the image in the server
my code for android phone which sends the image is
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);
and my code which handles the image in the server is like this
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();
now how do i use the byte array to display the image on the server or use to edit the image with some other stuffs like string,other image etc.
I think this should work for you...
http://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/
on the server side (imageInByte is your data[])...
//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"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.