站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。!

各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。!

摘要: 各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。!


各位仁兄 请问如何在网页中实现文件的下载,高分相送,谢谢啦。!


download abc


<tr>
<td height="25" class="tdcor">附&nbsp;&nbsp;件&nbsp;</td>
<td colspan="3" height=50>
<%
if (null != publish.getAttatchFilename() &&
publish.getAttatchFilename().length() > 0) {
%>
<%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>">
<%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%>
<%
}
%>
</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();
%>


用流的方式写个程序,保存文件就行了


↑返回目录
前一篇: 如何redirect,使iframe的Parent实现页面转换?
后一篇: 有高手吗?关于session的问题!谢谢了