String filePath=getRequest().getParameter("file");
String fileName = getRequest().getParameter("fileName");
String id = getRequest().getParameter("id");
filePath = getRequest().getRealPath("")+"\\"+"WordToPDF\\"+id+".pdf";
File f = new File(filePath);
if(fileName==null){
fileName=id;
}
InputStream is=null;
OutputStream os=null;
try{
/**//*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/
byte[] b=new byte[1024];
int i=0;
is = new FileInputStream(f);
os = getResponse().getOutputStream();
/**//*设置报头信息,弹出窗口中显示的文件名 newpath*/
getResponse().setCharacterEncoding("utf-8");
fileName = fileName+filePath.substring(filePath.lastIndexOf("."));
String agent = (String)getRequest().getHeader("USER-AGENT");
if(agent != null && agent.indexOf("MSIE") == -1) {
// FF
fileName = "=?UTF-8?B?" + (new String (Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
getResponse().setHeader("Content-Disposition", "attachment; filename=" + fileName);
}else {
// IE
fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
getResponse().setHeader("Content-Disposition", "attachment; filename=" + fileName);
}
//设置文件大小
getResponse().setContentLength((int) f.length());
/**//*具体的输入输出流操作*/
while((i=is.read(b))!=-1){
os.write(b, 0, i);
}
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
os.flush();
os.close();
is.close();
}
/* if(f.exists()){
f.setWritable(true);
f.delete();
} */
Struts2文件流下载
最新推荐文章于 2024-07-10 21:37:49 发布