public class FileDownAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String fileName;
public String getFileName() throws UnsupportedEncodingException {
return new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");
}
public InputStream getDownLoadFile() throws IOException {
System.out.println("upload/"+fileName);//打桩测试乱码解决与否
//获取资源路径
return ServletActionContext.getServletContext().getResourceAsStream("upload/"+fileName);
}
@Override
public String execute() throws Exception {
System.out.println(fileName);
return "success";
}
private static final long serialVersionUID = 1L;
private String fileName;
public String getFileName() throws UnsupportedEncodingException {
return new String(fileName.getBytes("utf-8"), "ISO8859-1");
}
public void setFileName(String fileName) throws UnsupportedEncodingException {
this.fileName = new String(fileName.getBytes("ISO8859-1"), "utf-8");
}
public InputStream getDownLoadFile() throws IOException {
System.out.println("upload/"+fileName);//打桩测试乱码解决与否
//获取资源路径
return ServletActionContext.getServletContext().getResourceAsStream("upload/"+fileName);
}
@Override
public String execute() throws Exception {
System.out.println(fileName);
return "success";
}
}
关键就在于getFileName()和setFileName(String fileName)方法中的编码格式的转换,get方法中将utf-8格式的文件名用iso8859-1格式输出到浏览器,才不会在下载的时候出现下载的文件异常问题,set方法中是将ISO8859-1的编码格式的中文名转换为utf-8格式的 后台才能识别它,在服务器上找到对应的文件,返回给浏览器