java – 如何在服务器响应上编写文件对象而不在服务器上保存文件?
作者:互联网
我正在使用带DWR的Spring.我想返回一个文件对象作为响应,但是我将文件(待发送)保存在服务器临时位置,然后将其位置作为href发送到客户端的锚标记,但是我想知道是否有办法抛出直接在浏览器上写入响应对象而不将其临时保存在服务器上.
我期望是否可以通过DWR将文件作为响应发送.
解决方法:
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(); } }
标签:response-write,httpwebresponse,java,spring,dwr 来源: https://codeday.me/bug/20191009/1876195.html