struts2下载

来源:互联网 发布:姚明02数据统计 编辑:程序博客网 时间:2024/05/18 02:15
public String downs(){
String jbId=getEmpAndJbpm().getJbId();
String imgId=this.getRequest().getParameter("imgId");
List<ImgAndJbpms> list=imgAndJbpmService.getImgById(Long.parseLong(imgId));
ImgAndJbpms imgAndJbpms=list.get(0);
String imgfiles=imgAndJbpms.getImgUrl();
String imgNameString=imgAndJbpms.getImgName();
int index=imgfiles.lastIndexOf("/");
imgfiles=imgfiles.substring(index+1, imgfiles.length());
if (null != imgfiles && !"".equals(imgfiles)) {
try {
String path = ServletActionContext.getServletContext().getRealPath("/uploads/"+jbId+"/"+imgfiles); //服务器保存的文件地址
//设置要显示在保存窗口的文件名,如果文件名中有中文的话,则要设置字符集,否则会出现乱码。另外,要写上文件后缀名  
File file = new File(path);
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
this.getResponse().reset();
this.getResponse().addHeader("Content-Disposition","attachment;filename="+ new String(imgNameString.getBytes("utf-8"), "ISO8859-1"));
this.getResponse().addHeader("Content-Length","" + file.length()); // 设置返回的文件类型
OutputStream toClient = new BufferedOutputStream(this
.getResponse().getOutputStream()); // 得到向客户端输出二进制数据的对象
this.getResponse().setContentType("application/octet-stream; CHARSET=utf8"); // 设置返回的文件类型
toClient.write(buffer); // 输出数据
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;

}


public interface SystemConstants {
String SESN_KEY_LOGIN_EMPLOYEE = "loginEmployee";
}





0 0
原创粉丝点击