请各位高手指教一下关于文件下载的中文问题

streetgrass 2004-03-11 08:50:49
在坛上找的源码,但每次下文件的时候中文文件名是乱码,文件内的内容也是乱码,请高人指点一二,谢谢

import java.io.*;
public class FileDownload {

public FileDownload() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;


System.out.println("aFileName:"+aFileName);
try {
File ff=new File(aFileName);
System.out.println("ff.getName():"+ff.getName());
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));

response.setContentType( "application/msword" );
int pos=aFileName.lastIndexOf("/");

aFileName=aFileName.substring(pos+1);
aFileName=new String(aFileName.getBytes("iso-8859-"),"gb2312");
System.out.println("aFileName1:"+aFileName);
response.setHeader("Content-disposition", "attachment; filename=\""+aFileName+"\"");
//传送数据
dout = new BufferedOutputStream(response.getOutputStream());

int once = 0;
int total = 0;
while ((total<length) && (once>=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误1! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
public static void main(String[] args) {
FileDownload download1 = new FileDownload();
}
}
...全文
32 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
streetgrass 2004-04-01
  • 打赏
  • 举报
回复
是网页上的问题,下载时网页上的代码之间不能有硬回车,论坛上有一篇这方面的文章,我就是靠它解决问题,需要的话可以搜索一下.
xylohouse 2004-03-31
  • 打赏
  • 举报
回复
?
Arias 2004-03-30
  • 打赏
  • 举报
回复
就是就是!
xylohouse 2004-03-30
  • 打赏
  • 举报
回复
楼主是怎么解决的?
能把代码贴出来吗?
谢谢!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧