昨天搞excel文件下载,下载下来的中文文件名一直乱码,但是内容没有乱码,试了网上很多方法,都没有成功,最后是一同事找到的解决办法
1.零配置,这一块没有什么
@Result(name = "success", type = "stream", params = {"inputName","downloadFile","contentType","application/vnd.ms-excel;charset=charset=ISO8859-1","contentDisposition","attachment;filename=${filename}"})
2.文件名get方法是重点
private String filename;
public void setFilename(String filename){
this.filename = filename;
}
public String getFilename() throws UnsupportedEncodingException{
String enableFileName ="=?UTF-8?B?" + (new String(Base64.encode("文件名.xls".getBytes("UTF-8")))) + "?=";
return enableFileName;
}
3.InputStream
public InputStream getDownloadFile() throws Exception {
try {
String path = "文档资料" + File.separator + "模板" + File.separator + "文件名.xls";
System.out.println(path);
InputStream in = ServletActionContext.getServletContext().getResourceAsStream("\\"+path);
return in;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}