解决中文文件名乱码

最关键的解决乱码:

new String(filename.getBytes("gb2312"),"iso8859-1"));

找了半天,终于解决了。不容易呢

 



之前,写过一个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" style="border: 1px #FFFFFF solid;background:#efefef" > <FONT color=red>*</FONT></TD>
            </TR>
            <TR align="center">
              <TD height=60 colspan="2" vAlign=center 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"; //假设你的文件名是:中文测试.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();
}
%>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值