各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。! 各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <a href="abc.rar">download abc</a> <tr> <td height="25" class="tdcor">附 件 </td> <td colspan="3" height=50> <% if (null != publish.getAttatchFilename() &&publish.getAttatchFilename().length() > 0) { %> <a href="publish_do.jsp?method=download&fileName=<%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>"><%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%></a> <% } %> </td></tr> <% else if (null != method && method.equals("download")) {//下载附件 String fileName = request.getParameter("fileName"); File file = new File(Constants.PUBLISH_FILE_PATH + "/" + URLDecoder.decode(fileName,"GBK")); response.reset(); response.setContentType("application/octet-stream; charset=GBK"); response.addHeader("Content-Disposition", "attachment; filename=" + CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1")); response.setContentLength((int) file.length()); byte[] buffer = new byte[4096]; BufferedOutputStream output = null; BufferedInputStream input = null; // 写缓冲区: try { output = new BufferedOutputStream(response.getOutputStream()); input = new BufferedInputStream(new FileInputStream(file)); int n = (-1); while ((n = input.read(buffer, 0, 4096)) > -1) { output.write(buffer, 0, n); } response.flushBuffer(); } catch (Exception e) { } // maybe user cancelled download finally { if (input != null) input.close(); if (output != null) output.close(); } 这样可以下载,但是我希望是用java代码写的,可以下载任何文件格式的文件!不过仍然很感谢你! <%@ page language="java" pageEncoding="GB18030"%><% out.clear(); out = pageContext.pushBody(); String fileName = request.getParameter("filename"); InputStream inStream=new FileInputStream(fileName); //设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition","attachment; filename=\"" + ruleName + "\""); //循环取出流中的数据 byte[] b = new byte[100]; int len; while((len=inStream.read(b)) >0) response.getOutputStream().write(b,0,len); inStream.close(); out.clear(); out = pageContext.pushBody(); %> NetBeans web服务总是提示出错,各位大哥,能不能帮我看看到底出了什么错 为什么要防止表单重复提交 struts标签问题 数据库连接池 如何使用jsf中的inputFileUpload组件实现文件上传下载? 根据下面代码 请问如何传参数给下一个jsp页面 在线等 解决后散分! 一个半年都没有人回答的问题!在这期间我郁闷无限 郁闷,散分50~~~ 大家讨论一下jsp的web服务器啊?? 对象不支持此属性或方法 如何在JSP页面上显示进度条??? 有高手吗?关于session的问题!谢谢了
<td height="25" class="tdcor">附 件 </td>
<td colspan="3" height=50>
<%
if (null != publish.getAttatchFilename() &&
publish.getAttatchFilename().length() > 0) {
%>
<a href="publish_do.jsp?method=download&fileName=
<%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>">
<%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%></a>
<%
}
%>
</td>
</tr>
<% else if (null != method && method.equals("download")) {//下载附件 String fileName = request.getParameter("fileName");
File file = new File(Constants.PUBLISH_FILE_PATH + "/" + URLDecoder.decode(fileName,"GBK"));
response.reset();
response.setContentType("application/octet-stream; charset=GBK");
response.addHeader("Content-Disposition", "attachment; filename=" + CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1"));
response.setContentLength((int) file.length()); byte[] buffer = new byte[4096];
BufferedOutputStream output = null;
BufferedInputStream input = null; // 写缓冲区:
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file)); int n = (-1);
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
response.flushBuffer();
}
catch (Exception e) {
} // maybe user cancelled download
finally {
if (input != null) input.close();
if (output != null) output.close();
}
不过仍然很感谢你!
<%
out.clear();
out = pageContext.pushBody();
String fileName = request.getParameter("filename"); InputStream inStream=new FileInputStream(fileName);
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + ruleName + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
out.clear();
out = pageContext.pushBody();
%>