简体   繁体   English

如何从JSP下载附件文件

[英]How to download attachment file from JSP

I want to know how can I download any file from JSP page based on content disposition as an attachment from 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. 我想在JSP页面上创建一个链接,然后通过单击该链接,用户可以从邮件服务器下载文件。 The link should be for content dispostion's attachment type. 该链接应用于内容伪装的附件类型。 How can I do that in JSP? 如何在JSP中做到这一点?

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. 请勿为此使用JSP,因为使用JSP来流二进制文件时会麻烦,因为<% %>标记之外的所有空格也会被打印到响应中,这只会破坏二进制内容。 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. 您需要做的就是在JSP中放置一个<a href="fileservlet/file.ext">类的HTML链接,并使用servlet类来完成所有处理和流传输任务。 To set a response header, just use HttpServletResponse#setHeader() . 要设置响应头,只需使用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

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

I suggest you break this question down a bit. 我建议您将这个问题分解一下。

Do you know how to access the attachments from within a regular java program? 您知道如何从常规Java程序中访问附件吗? 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. 如果您知道这一点,那么通过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. 虽然,我强烈建议您执行常规的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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM