之前,写过一个download.jsp文件,可以解决下载文件乱码问题(诸如:doc,xsl文件等等).
后来发现,遇到中文名的文件的时候,文件下载将会报错~~~~
今天,通过改写原download.jsp文件已经彻底解决了这个问题~
现在,把一整套的文件上传下载的方法给贴出来~~~以便大家借鉴!~!~!~!~!
作者:古埃及法老
-------------------------------------------------------------------------------------------------------------------
测试环境:weblogic 8.1,win xp sp4,ie 6.0
-----------------------------------------------------
文件上传:
-----------------------------------------
准备工作:导入著名的smartupload.jar组件包
upload.jsp文件
---------------------------------------------------------
<%@ page contenttype="text/html; charset=gb2312" %>
<%
request.setcharacterencoding("gb2312"); // 这句话很重要,否则遇到中文就出错~
%>
<html><head><title>上传</title>
<meta content="text/html; charset=gb2312" http-equiv=content-type>
</head>
<body leftmargin=0 topmargin=0>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#dee7ef">
<tr>
<td align="center">
<form action="upload_ok.jsp" method=post name="upload" enctype="multipart/form-data">
<br>
请输入附件文件的所在路径<font color=red> * </font>为必填项目<br>
<br>
<table width="317" border=0 cellpadding=0>
<tbody>
<tr>
<td align=right valign=center nowrap>附件路径:</td>
<td><input type="file" name="file" > <font color=red>*</font></td>
</tr>
<tr align="center">
<td height=60 colspan="2" valign=center nowrap> <input name=b1 type=submit value=" 确 定 " >
<input name=b2 type=reset value=" 取 消 " >
</td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</table>
</body></html>
---------------------------------------------------------
upload_ok.jsp文件
---------------------------------------------------------
<%@ page contenttype="text/html;charset=gb2312" %>
<%@ page import="com.jspsmart.upload.*" %>
<html><head><title>上传成功!</title>
<meta content="text/html; charset=gb2312" http-equiv=content-type>
</head>
<body leftmargin=0 topmargin=0>
<jsp:usebean id="mysmartupload" scope="page" class="com.jspsmart.upload.smartupload" />
<table width="80%" border="0" cellpadding="0" cellspacing="0" bgcolor="#dee7ef">
<tr>
<td align="center">
<%
int count=0;
string filename = null;
mysmartupload.initialize(pagecontext);
mysmartupload.upload();
com.jspsmart.upload.file myfile = mysmartupload.getfiles().getfile(0);
if (!myfile.ismissing()) {
//string ext=myfile.getfileext();//得到后缀
filename = myfile.getfilename();
myfile.saveas("/files/" + filename);//你要存放文件所在文件夹的相对路径
out.println("文件:<b>"+filename+"</b>上传成功!<br>文件大小:" + myfile.getsize() + "kb<br>");
}
%>
</body></html>
---------------------------------------------------------
文件下载:
-----------------------------------------
文件的超连接写法范例:
<% string fname ="中文测试.xsl"; //假设你的文件名是:中文测试.xsl
%>
<a target="_blank" href="download.jsp?filename=<%=fname%>">下 载</a>
文件的超连接写法范例-2 重新用utf-8对文件名编码:
<%@ page contenttype="text/html;charset=gb2312" session="true"%>
<% string name=java.net.urlencoder.encode("世界文化.doc","utf-8"));%> <a href="c:/<%=name%>">世界文化.doc</a>
download.jsp文件
---------------------------------------------------------
<%
java.io.bufferedinputstream bis=null;
java.io.bufferedoutputstream bos=null;
try{
string filename=request.getparameter("filename");
filename=new string(filename.getbytes("iso8859-1"),"gb2312");
response.setcontenttype("application/x-msdownload");
response.setheader("content-disposition","attachment; filename="+new string(filename.getbytes("gb2312"),"iso8859-1"));
bis =new java.io.bufferedinputstream(new java.io.fileinputstream(config.getservletcontext().getrealpath("files/" + filename)));
bos=new java.io.bufferedoutputstream(response.getoutputstream());
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesread);
}
}
catch(exception e){
e.printstacktrace();
}
finally {
if (bis != null)bis.close();
if (bos != null)bos.close();
}
%>
新闻热点
疑难解答