一、实现功能:页面上显示附件链接,点击链接,弹出“文件下载”框,打开或保存文件。
(ps:最简单常见的功能,却有很多地方需要注意)
二、过程:采用 servlet1 提供“附件列表”数据,在页面显示所有附件链接,点击链接经过servlet2处理,弹出“文件下载”框,打开或保存文件。
1、在index页面,通过点击链接向GotoAttachServlet发出请求。
2、GotoAttachServlet 提供附件列表,转向download.jsp 页面。
3、在download.jsp 点击某个附件链接,通过DownloadServlet处理,弹出“文件下载”框,打开或保存该文件。
注:为了方便,数据没有从数据库中取。整个过程是完整的,其实只要关注DownloadServlet.java中相关代码即可。
三、实现。
1、index中的相关代码。
<a href="GotoAttachServlet">附件页面</a>
2、GotoAttachServlet中相关代码。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Attachment>attachList = new ArrayList<Attachment>();
Attachment attach1=new Attachment(1,"flower.png","d://flower.jpg");
attachList.add(attach1);
Attachment attach2=new Attachment(2,"tree.txt","d://tree.txt");
attachList.add(attach2);
request.setAttribute("attachList", attachList);
request.getRequestDispatcher("download.jsp").forward(request, response);
}
3、download.jsp 代码。
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>下载</title>
<script src="<%=basePath%>js/jquery-1.4.4.js"></script>
<script type="text/javascript">
function download(attachId){
var url="/MyWeb/DownloadServlet?attachId="+attachId;
$('#attachmentForm').attr('action',url).submit();
}
</script>
</head>
<body>
<div>附件下载</div>
<form id="attachmentForm" method="post">
<table>
<tr>
<td>附件:</td>
<td>
<ul>
<c:forEach items="${attachList}" var="attach">
<li>
<a href="#" onclick="download('${attach.id}')">${attach.name}</a>
</li>
</c:forEach>
</ul>
</td>
</tr>
</table>
</form>
</body>
</html>
4、DownLoadServlet.java中相关代码。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idStr=request.getParameter("attachId");
Attachment attachment=null;
if(idStr.equals("1")){
attachment=new Attachment(1,"flower.png","d://flower.jpg");
}else{
attachment=new Attachment(2,"tree.txt","d://tree.txt");
}
String downFilePath=attachment.getPath();
String fileName = attachment.getName();
fileName = URLEncoder.encode(fileName, "UTF-8");
// 设置响应信息
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename="
+ fileName);
response.setCharacterEncoding("utf-8");
// 得到流
InputStream inputStream = new FileInputStream(downFilePath);
ServletOutputStream servletOutputStream=response.getOutputStream();
final int size = 1024;
byte[] buffer = new byte[size];
int length;
while ((length = inputStream.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, length);
}
servletOutputStream.flush();
servletOutputStream.close();
} }
5、你可能看不明白的地方:
1、response.setContentType("application/octet-stream"); 参看:http://wuyechun.iteye.com/blog/1132423
2、response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
在使用来自 Web 服务器的文档时,您可能希望立即提示用户将文件直接保存在用户磁盘上,而不要在浏览器中打开该文件。不过,对于已知的 MIME(多用途 Internet 邮件扩展)类型,如 Microsoft Word ("application/ms-word"),默认行为是在 Internet Explorer 中打开文档。
可以使用 Content-disposition 头来覆盖此默认行为。其格式是:
Content-disposition: attachment; filename=fname.ext
分享到:
相关推荐
今天整理了一个jsp附件上传模块,结合jquery Uploadify插件,可以一次性上传多个附件,而且自带上传进度功能,非常漂亮的界面和强大的功能。 Uploadify插件是由国外开发的一个jquery插件,功能非常的齐全,包括同时...
很好用的一个jsp文件上传下载开源,村!!!!!!!
jsp邮箱系统,实现了收邮件和发送附件,非常好
中国移动业务管理系统源码(SSH)基于java的三大框架源码
一个简单的javaWeb项目,在前台jsp界面上传附件 亲测可以实现。如有问题欢迎讨论
jsp实现向oracle中blob字段上传附件
一个index页面,三个链接实现excel的动态读取和导入,最最重要的是里面的类可以复用。。。自己改改应该可以应用到自己的工程中。。。。。
这是一个纯jsp实现的文件上传功能. 无页面刷新,表单数据可以依次连续填写,最后上传文件. 文件上传后将文件名和上传路径返回,供业务保存DB使用.
教程名称:jsp项目设计方案汇总(更新中)课程目录:【】JSP基于STRUTS...JSP社会养老保险管理系统的设计与实现【】JSP程序设计精彩实例打包下载(PDG)【】J 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
kindeditor插件,用来实现附件上传功能,jsp版的,可惜应用量不大,由于电脑上没有JSP环境,因此未能测试,感谢您对烈火下载的支持。
最近在做文件上传下载,搜集了点资料希望,对大家有帮助!
直接把文件部署到tomcat里即可!jdk1.6以上 tomcat6.0以上本人测试已成功!具体上传路径已配置!部署原创在网上反编译的!
jsp + jquery实现的漂亮上传 客户端使用了jquery的uploadify插件,显示多文件的一步上传. 实时显示上传比例. 有着漂亮的界面. 演示效果:http://www.uploadify.com/demo/ 后台使用了Apache的Commons fileUpload 来...
基于jsp实现博客管理系统,该系统有登录,验证码,管理员可以上传下载附件,另外可以分类查询,增加删除更改用户、博客等功能。
jsp带附件邮件发送工具,运用了servlet,基本功能都实现了
资源名称:jsp实现留言墙 word版内容简介: JSP技术使用Java编程语言编写类XML的tags和scriptlets,来封装产生动态网页的处理逻辑。网页还能通过tags和scriptlets访问存在于服务端的资源的应用逻辑。...
java+jsp+mysql实现注册登录-附件资源
x-oss-process=style/pnp8(101.29KB,下载次数:2)下载附件2019-10-1815:58上传【课程介绍】:今天发布了一个由jsp+ssm框架实现的自适应新闻博客管理系统的源码和视频开发(spring、springMVC、mybaits)教程,这里...
任何jsp页面只要使用包含附件中的loading.jsp将自动拥有“页面正在加载的。。”提示信息,在页面加载完后将自动隐藏。 我为什么使用这个组件? 1。交互性。 客户清楚知道,你的页面是在加载,需要等待,而不用...
jsp/servlet 模式的发送邮件系统 可发送多个附件 存在的问题为 subject可能会乱码 有问题可以给我留言