如何在服务器响应上编写文件对象而不在服务器上保存文件? [英] how to write a file object on server response and without saving file on server?
本文介绍了如何在服务器响应上编写文件对象而不在服务器上保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用带DWR的Spring。我想返回一个文件对象作为响应,但是我将文件(待发送)保存在服务器临时位置,然后在客户端将其位置作为href发送给锚标记,但是我想知道是否有办法抛出直接将文件直接发送到响应对象上的浏览器,而不是暂时保存在服务器上。
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.
我预计是否可以通过DWR将文件作为响应发送。
I expected if there could be a way to send file as a response via 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();
}
}
这篇关于如何在服务器响应上编写文件对象而不在服务器上保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文