struts2 下载与上传 圆满解决中文乱码
一、下载
1 、配置文件
<!-- 确认 候选人基本信息 --> < action name = "hxrBaseInfo_*" class = "hxrBaseInfoAction" method = "{1}" > < result name = "down" type = "stream" > < param name = "contentType" > application/octet -stream;charset =ISO8859-1 </ param > < param name = "inputName" > inputStream </ param > <!-- 使用经过转码的文件名作为下载文件名, downloadFileName 属性 对应 action 类中的方法 getDownloadFileName() --> < param name = "contentDisposition" > attachment;filename="${downloadFileName}" </ param > < param name = "bufferSize" > 5120 </ param > </ result > </ action > |
2 、 action
public class HxrBaseInfoAction ……………… // 导出下载使用 private String fileName ; // 下载文件名 private InputStream inputStream ; // 下载文件使用流 /** * 下载方法 * @return * @throws Exception */ public String exportZip() throws Exception { return "down" ; //1 、调用 getInputStream() 在 Stream 中给文件 //2 、调用 getDownloadFileName 取得下载名 解决了中文乱码 // 如果是前台传过来的中文乱码,还要在 } public InputStream getInputStream() { try { this .setFileName( 可以给定“中文名” ); this . inputStream = new FileInputStream( this . fileName ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return this . inputStream ; }
public void setInputStream(InputStream inputStream) ……………… /** * 指定下载文件名称 解决了中文乱码 可见配置文件中对应 ${downloadFileName} * @return */ public String getDownloadFileName(){ String fn = "" ; try { String temp = this .getFileName(). ……………… ; fn = new String(temp.getBytes(), "ISO8859-1" ); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return fn; } 解决传参是“中文”时的乱码。为里对应下面 js 请求下载 public void setFileName(String fileName) { try { this . fileName = URLDecoder.decode (fileName, "utf-8" ); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Js get 传值 — 》乱码解决方法 var url = $.url.encode(" 候选人授权书 - 太和鼎信 ") ; // 解决中文件名乱码 window.open("downloadOrUpload_down.action?fileName="+$.url.encode(url)+".pdf","_self"); |
二、上传
1、action
//文件上传
private File uploadFile;//得到上传的文件
private String uploadFileContentType;//得到文件的类型
private String uploadFileFileName;//得到文件的名称
getter setter 省略
/***
* 接收上传文件
* @return
* @throws Exception
*/
public String upload() throws Exception {
try {
//String realpath = ServletActionContext.getServletContext().getRealPath("/downloadFiles/upload");
String realpath = (String) Constant.getValue("hxr.baseInfo.upFiles");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
//取得扩展名
String fileExtName = uploadFileFileName.substring(uploadFileFileName.indexOf(".")+1);
//生成新的文件名
String newFileName = UUID.randomUUID().toString()+"."+fileExtName;
FileUtils.copyFile(uploadFile, new File(file, newFileName));
//生成缩略图 支持生成//bmp,jpg,rar,doc,pdf,zip,tif
String filterStr = "bmp,jpg,tif";
String littleImgRelePath = ServletActionContext.getServletContext().getRealPath("/downloadFiles/littleImg");
String littleImgPath = ""; //小图路径
if(filterStr.indexOf(fileExtName)>-1) {
// TODO 调用 高立收 方法生成 小图
ScaleImage.saveImageAsJpg(realpath+newFileName, littleImgRelePath+"/"+newFileName, 440, 400);
//ScaleImage.saveImageAsJpg(realpath+"lilteImage/",);
littleImgPath = "downloadFiles/littleImg/"+ newFileName;
}
// session中注册 文件
Map<String, Boolean> fileMap = (Map<String, Boolean>) this.getRequest().getSession().getAttribute("fliesMap");
fileMap.put(realpath+newFileName, false); //原图注册
fileMap.put(littleImgRelePath+"/"+newFileName, false); //缩略图注册
if(!"".equals(littleImgPath)) fileMap.put(littleImgPath, false);
this.returnString("{success:true,filePath:'"+realpath+newFileName+"',fileName:'"+uploadFileFileName+"',littleImgPath:'"+littleImgPath+"'}", "UTF-8");
} catch (Exception e) {
e.printStackTrace();
this.returnString("{success:false}", "UTF-8");
}
return null;
}