如何在JSP中实现文件下载 谢谢,来点详细的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看这篇文章吧:http://hisonjon.blog.ccidnet.com/blog/ccid/do_showone/tid_63446.html 可以自己进行文件流读写,或者采用专用下载工具,但是jspsmartupload应该比较方便了。 下载????<td><a target='_blank' href='/upload/images/Jsp.doc'><img src="/upload/images/doc.jpg"></a></td>一样可以下载啊!!! jspsmartupload组件或者Apache common upload组件 Servlet中的一段代码,自己看看改改:try { // 构造要下载的文件名,/download/${sessionID}.xls ServletContext context = servlet.getServletContext(); String path = context.getRealPath("/download/"); path = path + System.getProperty("file.separator") + request.getSession().getId() + ".xls"; // 下载时显示的名称 String filenamedisplay = URLEncoder.encode("监控报表.xls", "UTF-8"); // 清除输出缓冲区中的数据 response.reset(); // 设置打开方式为EXCEL,其他后缀自己找找吧^_^ response.setContentType("application/x-msdownload"); // 在线打开或者下载保存 response.addHeader("Content-Disposition", "attachment;filename=" + filenamedisplay); ServletOutputStream output = response.getOutputStream(); java.io.FileInputStream fis = new java.io.FileInputStream(path); byte[] b = new byte[1024]; int i = 0; // 将文件写到输出流 while ( (i = fis.read(b)) > 0 ) { output.write(b, 0, i); } fis.close(); output.flush(); output.close(); } catch (IOException ex) { ex.printStackTrace();} jspsmartupload 的例子有很多。。找个注释多点的。很快就ok 请教各位看一下这段代码,怎么就错了呢? 安装tomcat5.5是否与JDK版本有关? 一个环境变量的问题。 跪求高手帮我解答tomcat连接mysql问题 jasperReport报表打印的问题 请问如何点一下按钮,就会出现一个上传的控件?! InvokeServlet 与 DefaultServlet,高手进! 在标签库中dostarttag使用pageContext.getOut().flush();是什么意思? ■■求教sun-j2ee-ri.xml里配置的问题■■ servlet实时画图 如果是div绝对居中 谁有遮盖层的js代码
一样可以下载啊!!!
try {
// 构造要下载的文件名,/download/${sessionID}.xls
ServletContext context = servlet.getServletContext();
String path = context.getRealPath("/download/");
path = path + System.getProperty("file.separator")
+ request.getSession().getId() + ".xls"; // 下载时显示的名称
String filenamedisplay = URLEncoder.encode("监控报表.xls", "UTF-8");
// 清除输出缓冲区中的数据
response.reset();
// 设置打开方式为EXCEL,其他后缀自己找找吧^_^
response.setContentType("application/x-msdownload");
// 在线打开或者下载保存
response.addHeader("Content-Disposition", "attachment;filename=" + filenamedisplay);
ServletOutputStream output = response.getOutputStream();
java.io.FileInputStream fis = new java.io.FileInputStream(path);
byte[] b = new byte[1024];
int i = 0;
// 将文件写到输出流
while ( (i = fis.read(b)) > 0 ) {
output.write(b, 0, i);
}
fis.close();
output.flush();
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}