Hello, I hope I can ask this question in this forum. So, I am trying to create client and servlet apps. Basically what I want to do is:
1. Client reads image file into byte[], then sends it via post request to the servlet. 2. Servlet will do something to this byte[], and sends it back to the client. For testing purpose, I just send back directly. 3. Client gets the byte[] and save it as image file. As my servlet doesn't do anything to the byte[], I should have got the same image file as a result. But I got only java exception - not a jpeg file. And if I print the resulting byte[], it doesn't give me the exact value as the input. Here is the code in client app. File img = new File("catavatare.jpg"); FileInputStream fis = null; try { fis = new FileInputStream(img); } catch (FileNotFoundException e) { e.printStackTrace(); } ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = null; buf = new byte[fis.available()]; for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); } byte[] bytes = bos.toByteArray(); //this is the byte[] of the image String paramStr = null; paramStr = URLEncoder.encode(new String(bytes), "UTF-8"); URL url = new URL("http://localhost:8080/ImageFilter/imagefilter"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(paramStr); //write the parameter to the request body out.flush(); InputStream in = conn.getInputStream(); byte[] ins = new byte[in.available()]; in.read(ins); //write the resulting byte[] /* the rest of the code is to save the resulting byte[] as image file */ ByteArrayInputStream bis = new ByteArrayInputStream(ins); Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpeg"); ImageReader reader = (ImageReader) readers.next(); Object source = bis; ImageInputStream iis = ImageIO.createImageInputStream(source); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Image image = reader.read(0, param); BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bufferedImage.createGraphics(); g2.drawImage(image, null, null); File imageFile = new File("catavatare2.jpg"); ImageIO.write(bufferedImage, "jpg", imageFile); System.out.println(imageFile.getPath()); The code in servlet app: public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { ServletInputStream in = req.getInputStream(); byte[] imgOld = new byte[in.available()]; in.read(imgOld); in.close(); resp.setContentType("image/jpeg"); ServletOutputStream out = resp.getOutputStream(); out.write(imgOld); //write the input byte[] into output stream - send it back without doing anything out.flush(); out.close(); } I would really appreciate if anyone gives me a hint on this. Thanks!! -- You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en