Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need to encode a JPEG and send from applet to servlet

843841Dec 14 2005 — edited Dec 15 2005
Hi,

I am trying to encode an image into a JPEG and send it from an applet to a servlet. The servlet will then save the JPEG to a file. I am setting contentType to image/jpeg.

This is the code in my applet which sends the JPEG:

******************************************************************
public String saveJPEG ( String filename )
{
BufferedImage bi = new BufferedImage ( photo.getWidth(null),
photo.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics2D g2 = bi.createGraphics();
g2.drawImage ( photo, null, null );

try
{
URL url = new URL("http://localhost:8000/JWebCam/saveImage");
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setAllowUserInteraction(true);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
httpConn.setUseCaches(false);
httpConn.setDefaultUseCaches(false);
httpConn.setRequestProperty("Content-Type", "image/jpeg");
OutputStream outputStream = httpConn.getOutputStream();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( outputStream );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
param.setQuality ( 1.0f, false ); // 100% high quality setting, no compression
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bi );
outputStream.close();

InputStream in = httpConn.getInputStream();
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
int len = 0;
while ((len = in.read(buf)) > 0) {
bos.write(buf, 0, len);
}
in.close();
} catch (IOException e) {
//...
}

return ("JPEG saved");
}
catch ( Exception ex )
{
ex.printStackTrace();
return ("Error saving JPEG : " + ex.getMessage() );
}
}

}
******************************************************************

And this is the code in my servlet which processes the POST request:

******************************************************************
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

log.debug("Received POST Request");

response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();

ObjectInputStream in = null;

try {
//start reading the request
OutputStream out = new FileOutputStream("C:/photo1.jpeg");
InputStream hIn = request.getInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = hIn.read(buf)) > 0) {
out.write(len);
}
out.close();
hIn.close();

} catch (Exception e) {
e.printStackTrace();
}

printWriter.println("<html>recieved request</html>");
}

**********************************************************************

The servlet recieves a request and creates a jpeg file, but the file seems to be corrupted because I cannot view the image. Does anyone know what I should do? My guess is that I need to set the contentType differently or I am not using the io streams properly.

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 12 2006
Added on Dec 14 2005
1 comment
133 views