请各位高手指教一下关于文件下载的中文问题
在坛上找的源码,但每次下文件的时候中文文件名是乱码,文件内的内容也是乱码,请高人指点一二,谢谢
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();
}
}