之前,寫過一個(gè)download.jsp文件,可以解決下載文件亂碼問題(諸如:doc,xsl文件等等).
后來發(fā)現(xiàn),遇到中文名的文件的時(shí)候,文件下載將會(huì)報(bào)錯(cuò)~~~~
今天,通過改寫原download.jsp文件已經(jīng)徹底解決了這個(gè)問題~
現(xiàn)在,把一整套的文件上傳下載的方法給貼出來~~~以便大家借鑒!~!~!~!~!  
作者:古埃及法老
-------------------------------------------------------------------------------------------------------------------
測(cè)試環(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"); // 這句話很重要,否則遇到中文就出錯(cuò)~
%>
<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>
        請(qǐng)輸入附件文件的所在路徑<font color=red> * </font>為必填項(xiàng)目<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);//你要存放文件所在文件夾的相對(duì)路徑
     out.println("文件:<b>"+filename+"</b>上傳成功!<br>文件大小:" + myfile.getsize() + "kb<br>");
     }
%>
</body></html>
---------------------------------------------------------
文件下載: 
-----------------------------------------
文件的超連接寫法范例:
<% string fname ="中文測(cè)試.xsl"; //假設(shè)你的文件名是:中文測(cè)試.xsl
%>
<a target="_blank" href="download.jsp?filename=<%=fname%>">下 載</a>
文件的超連接寫法范例-2 重新用utf-8對(duì)文件名編碼:
<%@ 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();
}
%> 
| 
 
 | 
新聞熱點(diǎn)
疑難解答
圖片精選