0

This is the code i used to download the image and saved.Can anyone suggest what else i have to do or what i missed here?? thanks in advance

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    BufferedWriter writer = null;
    InputStream in = new BufferedInputStream(request.getInputStream());
            try {                       
        byte[] buffer = new byte[100000];
        int n = -1;
        while ((n = in.read(buffer)) >= 0) {
            out.write(buffer, 0, n); // used for image
        }
        out.close();
        byte[] res = out.toByteArray();
        out.flush();
        FileOutputStream fos = new FileOutputStream("D://"+ new SimpleDateFormat("yyyyMMdd_HHmmssss").format(Calendar.getInstance().getTime()) +".jpg");
        fos.write(res);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
RDD
  • 145
  • 1
  • 18
  • @Manish i'm not trying to open in program,just opening the saved file in the file system,but photo viewer couldn't open the picture – RDD Jul 25 '13 at 06:53
  • If you are willing to download a file from servlet somehow somewhere you will have to write the byte[] as servlet response. – Manish Kumar Jul 25 '13 at 06:56
  • check this [SO](http://stackoverflow.com/questions/1154254/help-getting-image-from-servlet-to-jsp-page) and [this SO question](http://stackoverflow.com/questions/8623709/output-an-image-file-from-a-servlet) – Manish Kumar Jul 25 '13 at 06:58

2 Answers2

0

May be because you are not setting the response header and content type.try this

 protected void doGet( HttpServletRequest request,HttpServletResponse response)
    {
        try {
            Path path = Paths.get("c:\\test.jpg");
            response.setHeader("Content-Length", ""+Files.size(path));
            response.setContentType("image/jpeg");
            response.setHeader("Content-Disposition", "attachment; filename=\""+path.getFileName()+"\";");
            ServletOutputStream outputStream;
            outputStream = response.getOutputStream();
            byte[] data = Files.readAllBytes(path);
            outputStream.write(data);
        } catch (IOException ex) {ex.printStackTrace();}
    }
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
0

i found an alternate solution for my problem based on Multipart/form-data,for that the server should be capable of handling mulipart/form-data. for that i have to have

private static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(
        System.getProperty("java.io.tmpdir"));

inside the handler

also

if (request.getContentType() != null
            && request.getContentType().startsWith("multipart/form-data")) {
        baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT,
                MULTI_PART_CONFIG);
    }

inside the handle method then we can get the file from request as follows

@Override
public void handle(String target, Request baseRequest,
        HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    if (request.getContentType() != null
            && request.getContentType().startsWith("multipart/form-data")) {
        baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT,
                MULTI_PART_CONFIG);
    }
    // response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    baseRequest.setHandled(true);


        final FileOutputStream output = new FileOutputStream("D:\\Dir\\"+ request.getParameter("imageName") + ".jpg");
        IOUtils.copy(request.getPart("file").getInputStream(), output);
        output.close();


}

here IOUtils from apache commons-io jar file

RDD
  • 145
  • 1
  • 18