徹底解決中文名文件下載和下載文件內(nèi)容亂碼問題!!!!! 之前,寫過一個Download.jsp文件,可以解決下載文件亂碼問題(諸如:DOC,XSL文件等等).
后來發(fā)現(xiàn),遇到中文名的文件的時候,文件下載將會報錯~~~~
今天,通過改寫原Download.jsp文件已經(jīng)徹底解決了這個問題~
現(xiàn)在,把一整套的文件上傳下載的方法給貼出來~~~以便大家借鑒!~!~!~!~!
作者:古埃及法老
-------------------------------------------------------------------------------------------------------------------
測試環(huán)境:WEBLOGIC 8.1,WIN XP SP4,IE 6.0
-----------------------------------------------------
文件上傳:
-----------------------------------------
準(zhǔn)備工作:導(dǎo)入著名的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=middle nowrap>附件路徑:</TD>
<TD><input type="file" name="file" style="border: 1px #FFFFFF solid;background:#efefef" > <FONT color=red>*</FONT></TD>
</TR>
<TR align="center">
<TD height=60 colspan="2" valign=middle nowrap> <INPUT style="height:22px" name=B1 type=submit value=" 確 定 " >
<INPUT style="height:22px" 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"; //假設(shè)你的文件名是:中文測試.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();
}
%>
J-CN工作室
www.j-cn.org