java 如何从JSP下载附件文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2727371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download attachment file from JSP
提问by Stardust
I want to know how can I download any file from JSP page based on content disposition as an attachmentfrom mail server.
我想知道如何根据内容配置从 JSP 页面下载任何文件作为邮件服务器的附件。
I want to create a link on JSP page, and by clicking on that link user can download file from mail server. The link should be for content dispostion's attachmenttype. How can I do that in JSP?
我想在 JSP 页面上创建一个链接,通过单击该链接用户可以从邮件服务器下载文件。该链接应用于内容处理的附件类型。我怎样才能在 JSP 中做到这一点?
回答by BalusC
Don't use a JSP for this, it's recipe for trouble when using it to stream binary files, because all whitespace outside the <% %>tags will be printed to the response as well which would only corrupt binary content. All you need to do is to just place a HTML link like <a href="fileservlet/file.ext">in the JSP and use a servlet class to do all the processing and streaming task. To set a response header, just use HttpServletResponse#setHeader().
不要为此使用 JSP,使用它来流式传输二进制文件时会遇到麻烦,因为<% %>标签外的所有空白也将打印到响应中,这只会破坏二进制内容。您需要做的就是像<a href="fileservlet/file.ext">在 JSP 中一样放置一个 HTML 链接,并使用一个 servlet 类来完成所有的处理和流式传输任务。要设置响应头,只需使用HttpServletResponse#setHeader().
response.setHeader("Content-Disposition", "attachment;filename=name.ext");
You can find here a basic servlet example which does exactly this: FileServlet.
您可以在此处找到一个基本的 servlet 示例,该示例正是这样做的:FileServlet.
回答by aioobe
I suggest you break this question down a bit.
我建议你把这个问题分解一下。
Do you know how to access the attachments from within a regular java program? How to interface with the mail-server etc? If you know that, it should be an easy exercise to provide the attachment in a downloadable format through jsp. Although, I would strongly recommend you to do a regular servlet, since you would probably not have much use of the extra machinery around jsp.
您知道如何从常规 Java 程序中访问附件吗?如何与邮件服务器等接口?如果您知道这一点,那么通过 jsp 以可下载格式提供附件应该是一项简单的练习。虽然,我强烈建议您做一个常规的 servlet,因为您可能不会大量使用围绕 jsp 的额外机制。
Just make sure you set the content type according to what's being downloaded:
只需确保根据正在下载的内容设置内容类型:
In jsp: <%@page contentType="image/png" %>
在jsp中: <%@page contentType="image/png" %>
In a servelt: response.setContentType("image/png");
在一个伺服器中: response.setContentType("image/png");
回答by anoop
URL url = new URL("http://localhost:8080/Works/images/abt.jpg");
//for image
response.setContentType("image/jpeg");
response.setHeader("Content-Disposition", "attachment; filename=icon" + ".jpg");
//for pdf
//response.setContentType("application/pdf");
//response.setHeader("Content-Disposition", "attachment; filename=report" + ".pdf");
//for excel sheet
// URL url = new URL("http://localhost:8080/Works/images/address.xls");
//response.setContentType("application/vnd.ms-excel");
//response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
int len;
byte[] buf = new byte[1024];
while ((len = stream.read(buf)) > 0) {
outs.write(buf, 0, len);
}
outs.close();

