<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html; charset=UTF-8" language="java"
import="java.io.*,java.net.*,java.util.*;" errorPage=""%>
<%
/**
* @author migle 2011-4-1
* 流形式下载文件 downloadfile.jsp?fc=filecode
*
* 在map中通过filecode查找具体文件路径
***/
String filecode = request.getParameter("fc") != null ? request
.getParameter("fc") : "";
//可以扩展到从数据库或配置文件中读取
Map<String, String> map = new HashMap<String, String>();
map.put("999","d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_0.xls");
map.put("70", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_70.xls");
map.put("71", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_71.xls");
map.put("72", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_72.xls");
map.put("73", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_73.xls");
map.put("74", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_74.xls");
map.put("75", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_75.xls");
map.put("76", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_76.xls");
map.put("77", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_77.xls");
map.put("79", "d:/qhBI/downloadFile/market_survey_data_report/market_survey_data_report_79.xls");
if (map.containsKey(filecode)) {
response.reset();
File file = new File(map.get(filecode));
//处理中文编码问题,IE9没有问题
String fileName = URLEncoder.encode(file.getName(), "UTF-8");
response.setContentType("application/octet-stream; charset=UTF-8");
response.addHeader("Content-disposition"," attachment; filename=\"" + fileName + "\"");
FileInputStream fis = null;
OutputStream os = null;
try {
//out.clear();
//可以扩展到从FTP等网络中读取
os = response.getOutputStream();
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
fis = null;
}
if (os != null) {
os.close();
os = null;
}
if (out != null) {
out.clear();
out = pageContext.pushBody();
}
}
} else {
response.setStatus(404);
out.write("404 NOT FOUND! 你懂的");
}
%>
直接上代码了
分享到:
相关推荐
使用JSP实现上传下载文件jspSmartUpload 里面讲述怎么用jsp实现
jsp 实现 文件下载 jspsmartupload
完整的讲述了基于jsp的文件上传下载功能,具体讲解见http://blog.csdn.net/it_tingge/article/details/49025273
用java写的文件上传和下载,上传到硬盘物理路劲下,上传后用io读取txt文本文件,修改不能上传和下载是中文名称的bug,简单易用
实现jsp页面的下载功能,文档下载,文件下载,软件下载,测试过好用。
使用jsp下载excel文件 JSP文件下载后台生成的excel文件到本地机。
jsp+servlet实现文件下载(而不是在网页中显示出文件内容) 迅雷下载功能.zip
使用jspSmartUpload实现文件上传下载,包含SmartUpload方法注解、上传示例及组件包
JSP实现文件上传与下载.pdf
用JSP下载word文件(不会直接用IE打开.rar
java+jsp代码实现从FTP服务器上传下载文件。 里面给了java方法和jsp页面的代码。主要是实现从FTP服务器上面下载文件到自己本地文件夹下面。另外还给了几个网站供大家参考。
jsp页面文件下载功能实现,简单实用,快点下载吧!
jsp文件上传下载jar包。jsp文件上传下载jar包
一个基于myeclipse开发平台的jsp文件上传、文件下载实例
1,只需要将文件夹upload拷在...3,有jsp文件上传与下载攻略是讲文件上传与下载的各种类中的函数含义的 4,主要包括完整的 jspsmartupload上传与下载,还有jspsmartupload的完整包,并且包括包中各种上传与下载的解释
利用jsp实现文件上传下载,将文件上传到tomcat服务器之后下载。
JSP实现上传下载文件代码,简单好用,万试万灵。绝对好用。
java+jsp实现文件下载, 1、downloadtool.jsp为进入下载的页; 2、dodownloadtool.jsp实现下载;有详细的注释! 希望能帮到各位!
JSP实现文件的上传与下载,方便大家参考使用,实在方便,用到了文件上传组件,但是下载没有,大家参考参考学习学习,定会有所收获的。
JSP下载文件源代码,利用jsp-SmartUpload实现,代码简单,使用方便,调用简单。