我需要实现文件下载。我不想直接下载任何服务器端文件URL。我创建了一个servlet,它将打开文件并将其写入response.Now来到前面gwt我有onResponseReceived (请求请求,响应响应)将在接收响应时调用。现在如何继续进行? 。我需要的操作是,流中的文件应该下载到客户端计算机。
可以帮我解决这个问题吗?
答案 0 :(得分:2)
你试过Window.open(ServletUrl, "_parent", "location=no")
吗?
尝试在“application / exe”
的响应中设置内容类型这将提示用户保存或运行。 Servlet代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
File file = new File("/path/to/files", filename);
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
response.setHeader("Content-Length", file.length());
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
} finally {
if (output != null) try { output.close(); } catch (IOException ignore) {}
if (input != null) try { input.close(); } catch (IOException ignore) {}
}
}
答案 1 :(得分:2)
您可以使用_blank,_parent,_top,_self
中的任何一个答案 2 :(得分:0)