一般无论网站还是其他系统都会用到文件的上传和下载,对于文件的上传前面已经介绍过了,在本文中将详细介绍一下文件的下载实现以及经常碰到的问题,本文代码已经过调试,可以正常使用:
文件的下载一般采用的有两种方式:通过流或利用jspsmartupload.jar下载。
一.采用数据流下载:
第一个页面:(存在“下载”按钮的页面)
function doDownload(filePath,disName) {
var utl="download.jsp?filePath="+filePath+"&disName="+disName;
document.all.opForm.action=url;
// document.all.opForm.target="_blank";
document.all.opForm.submit();
}
<form name="opForm" method="post" action="">
<table width="10%" name="table1">
<tr>
<td>
<input name=”cmdDownload”onclick=”doDownload(‘<%=filePath%>’,’<%=fileName%>’)” value=”下载”>
</td>
</tr>
</table>
</form>
第二个页面(download.jsp):
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.io.FileInputStream" %>
<%
//response.reset();
//filePath为全路径:D:/upload/a.doc
String path = request.getParameter("filePath");
int k = path.lastIndexOf("/");
String name = path.substring(k + 1 , path.length());
response.setContentType("unknown");
response.addHeader("Content-Disposition","filename=\"" + name + "\"");
try {
OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(path);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b)) > 0) {
os.write(b, 0 ,i);
}
fis.close();
os.flush();
os.close();
} catch(Exception e) {
e.printStackTrace();
}
%>
如果只是这样写在tomcat底下可以正常使用,但weblogic底下会报如下错误:
java.net.ProtocolException: Exceeded stated content-length of: '19456' bytes
at weblogic.servlet.internal.ServletOutputStreamImpl.checkCL(ServletOutputStreamImpl.java:219)
at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:170)
at com.jspsmart.upload.SmartUpload.downloadFile(SmartUpload.java:579)
at com.jspsmart.upload.SmartUpload.downloadFile(SmartUpload.java:508)
at jsp_servlet._page.__download._jspService(__download.java:184)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:431)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)
at com.nstc.web.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:30)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6297)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:97)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3575)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2573)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:178)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:151)
>
这是因为weblogic会向response中写东西造成的,解决方式是将此句response.reset();的注释打开,这样在使用response时先将其重置。
二 .利用jspsmartupload.jar实现下载
第一个页面同上
download.jsp实现如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*" %>
<%!
public String toUtf8String(String s)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if (c >= 0 && c <= 255)
{
sb.append(c);
}
else
{
byte[] b;
try
{
b = Character.toString(c).getBytes("utf-8");
}
catch (Exception ex)
{
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++)
{
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
%>
<%
response.reset();//如果在weblogic底下同样要加上此句
String filePath = request.getParameter("filePath");
String disName = request.getParameter("disName");
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开。
su.setContentDisposition(null);
// 下载文件
String fileName = toUtf8String(disName);
try{
su.downloadFile(filePath,null,fileName);
}
catch(java.io.FileNotFoundException e){
%>
<script Language="javascript">
alert('服务器上未找到要下载的文件!');
</script>
<%}
%>下载完毕!
至此文件下载介绍完毕。