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

前端 未结 4 1075
不思量自难忘°
不思量自难忘° 2021-01-14 03:59

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 hre

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 04:20

    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();
            }
    
            }
    

提交回复
热议问题