Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a file object on server response and without saving file on server?

I am using Spring with DWR . I want to return a file object as response , however I save the file (to be sent) at server temporary location and then send its location as href for anchor tag on client side , however I wonder if there could be a way to throw the file directly to browser on response object without saving it temporarily on server.

I expected if there could be a way to send file as a response via DWR.

like image 267
Sankalp Avatar asked Sep 05 '25 20:09

Sankalp


1 Answers

public ModelAndView writeFileContentInResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {

        FileInputStream inputStream = new FileInputStream("FileInputStreamDemo.java");  //read the file

        response.setHeader("Content-Disposition","attachment; filename=test.txt");
        try {
            int c;
            while ((c = inputStream.read()) != -1) {
            response.getWriter().write(c);
            }
        } finally {
            if (inputStream != null) 
                inputStream.close();
                response.getWriter().close();
        }

        }
like image 53
Rupok Avatar answered Sep 08 '25 12:09

Rupok