struts2文件下载 火狐浏览器的文件名乱码问题

时间:2022-08-22 13:38:58

这是一个文件下载的action,红色部分为火狐浏览器需要特地做的事情。

@Controller
@Scope(value = "prototype")
public class FormManage_downloadAction { private String fileName;//下载文件时候的名称
private String filePath;//文件在磁盘的路径 public String execute() throws UnsupportedEncodingException {
System.out.println("下载action");
System.out.println("fileName="+fileName);
System.out.println("filePath="+filePath); HttpServletRequest request = ServletActionContext.getRequest();
String Agent = request.getHeader("User-Agent");
if (null != Agent) {
Agent = Agent.toLowerCase();
if (Agent.contains("firefox")) {
System.out.println("火狐浏览器");
fileName = new String(fileName.getBytes(), "iso8859-1");
} else {
System.out.println("非火狐浏览器");
fileName = URLEncoder.encode(fileName, "utf8");
}
}
return "success";
} public InputStream getInputStream() throws FileNotFoundException {
return new FileInputStream(filePath);
} public String getFileName() {
return fileName;
} public String getFilePath() {
return filePath;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public void setFilePath(String filePath) {
this.filePath = filePath;
}
}