如何从JSP下载附件文件 [英] How to download attachment file from JSP

查看:84
本文介绍了如何从JSP下载附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据邮件服务器中的内容配置(附件)从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页面上创建一个链接,通过单击该链接,用户可以从邮件服务器下载文件.该链接应用于内容发布的附件类型.如何在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 attachment type. How can I do that in JSP?

推荐答案

请勿为此使用JSP,因为它会打印<% %>标记之外的所有空白,所以使用它来流二进制文件时会遇到麻烦.以及只会破坏二进制内容的响应.您需要做的只是在JSP中放置一个HTML链接,如<a href="fileservlet/file.ext">,并使用servlet类来完成所有处理和流传输任务.要设置响应标头,只需使用

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

response.setHeader("Content-Disposition", "attachment;filename=name.ext");

您可以在此处找到一个基本的servlet示例,该示例正是这样做的: FileServlet .

You can find here a basic servlet example which does exactly this: FileServlet.

这篇关于如何从JSP下载附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆