找回密码
 注册
搜索
热搜: 回贴

彻底解决中文名文件下载和下载文件内容乱码问题

2009-12-22 00:22| 发布者: admin| 查看: 50| 评论: 0|原作者: 回梦游仙

之前,写过一个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"); // 这句话很重要,否则遇到中文就出错~
%>
上传











请输入附件文件的所在路径 * 为必填项目













附件路径: *






---------------------------------------------------------
upload_ok.jsp文件
---------------------------------------------------------
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="com.jspsmart.upload.*" %>
上传成功!







<%
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("文件:"+fileName+"上传成功!
文件大小:" + myFile.getSize() + "kb
");
}
%>

---------------------------------------------------------
文件下载:
-----------------------------------------
文件的超连接写法范例:
<% String fname ="中文测试.xsl"; //假设你的文件名是:中文测试.xsl
%>
下 载
文件的超连接写法范例-2 重新用utf-8对文件名编码:
<%@ page contentType="text/html;charset=gb2312" session="true"%>
<% String name=java.net.URLEncoder.encode("世界文化.doc","UTF-8"));%> 世界文化.doc
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();
}
%>

最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 01:42 , Processed in 0.103157 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.