tags:

views:

15

answers:

0

Hello, I first made a simple java app to convert an image to user desired format using JAI. eg. the code to convert to jpeg format is:-

     outputFile="C:\\eclipse\\" + outputFile + ".bmp";
     // Load the input image.
     RenderedOp src = JAI.create("fileload", inputFile);

     // Encode the file as a BMP image.
     OutputStream os = null;
    try {
        os = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        System.out.println("Cannot write the BMP File");
        e.printStackTrace();
    }

    BMPEncodeParam param = new BMPEncodeParam();
    ImageEncoder enc = ImageCodec.createImageEncoder("BMP", os, param);
    try {
        enc.encode(src);
    } catch (IOException e) {
        System.out.println("Exception in encoding");
        e.printStackTrace();
    }
    try {
        os.close();
    } catch (IOException e) {
        System.out.println("Error in closing file");
        e.printStackTrace();
    }

Now i wanted to try similar app at GWT. But GWT doesn't support FileOutputStream. So looking for an alternative i tried an app on GWT to upload and simply download an image file.

This is the servlet code that i have put up till now

        String fileName = (String)req.getParameter("fileName");
    String inputFile = "c:/users/rohit/desktop/"+fileName;
    File file=new File("c:/users/rohit/desktop/"+fileName);
    int length=0;
    String outputFile="c:/users/rohit/desktop/out.jpeg";
     // Load the input image.
     RenderedOp src = JAI.create("fileload", inputFile);

     // Encode the file as a JPEG image.
     OutputStream os=null;
    try {
        os = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
        System.out.println("Cannot write the JPEG File");
        e.printStackTrace();
    }

    JPEGEncodeParam param = new JPEGEncodeParam();
    ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os, param);
    try {
        enc.encode(src);
    } catch (IOException e) {
        System.out.println("Exception in encoding");
        e.printStackTrace();
    }
    try {
        os.close();
    } catch (IOException e) {
        System.out.println("Error in closing file");
        e.printStackTrace();
    }

    ServletContext context =getServletConfig().getServletContext();
    String mimetype=context.getMimeType("out.jpeg");
    resp.setContentType(mimetype);
    resp.setContentLength((int)outputFile.length());
    resp.setHeader("Content-Disposition", "attachment; filename=\""+"out.jpeg"+"\"");
    ServletOutputStream op = resp.getOutputStream();

    byte[] buffer = new byte[4096];
    DataInputStream in=new DataInputStream(new FileInputStream(outputFile));
    while ((in!=null)&&((length = in.read(buffer))!=-1)){
        op.write(buffer, 0, length);
    }
    os.close();
    op.flush();
    op.close();

}

This code now converts the image as well as saves it on the hard disk. but the converted image that is downloaded is not opening after download. So it means i'm committing a mistake in last few lines of the code. Please help.