① java文件下载不显示中文文件名,
//下载
response.setContentType("application/x-msdownload");
String filename = "测试.zip";
String iso_filename = SysParameter.parseGBK(filename);
response.setHeader("Content-Disposition",
"attachment;filename=" + iso_filename);
ServletOutputStream op = response.getOutputStream();
op.write(ab);
op.flush();
op.close();
SysParameter.parseGBK 方法:
// 将GBK字符转化为ISO码
public static String parseGBK(String sIn) {
if (sIn == null || sIn.equals(""))
return sIn;
try {
return new String(sIn.getBytes("GBK"), "ISO-8859-1");
} catch (UnsupportedEncodingException usex) {
return sIn;
}
}
② java如何实现下载弹出的对话框
Java实现点击下载文件的时候,弹出“另存为”对话框,选择保存位置,然后下载,代码如下:
publicvoiddownLoad(StringfilePath,HttpServletResponseresponse)
throwsException{
System.out.println("filePath"+filePath);
Filef=newFile(filePath);
if(!f.exists()){
response.sendError(404,"Filenotfound!");
return;
}
BufferedInputStreambr=newBufferedInputStream(newFileInputStream(f));
byte[]buf=newbyte[1024];
intlen=0;
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;filename="+f.getName());
OutputStreamout=response.getOutputStream();
while((len=br.read(buf))>0)out.write(buf,0,len);
br.close();
out.close();
}