http://hi.baidu.com/8_8_8_8_8_8/blog/item/1565d173ddbdd41a8701b09d.html
// 解决struts不在页面打开下载文件
public ActionForward testExecute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
DynaValidatorForm powerForm = (DynaValidatorForm) form;
// TODO Auto-generated method stub
InputStream is=null;
OutputStream os=null;
String path = request.getParameter("path");
/**//*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/
String newpath=URLEncoder.encode(path,"utf-8");
File f = new File(servlet.getServletContext().getRealPath("")+"\\Magazine\\"+path);
byte[] b=new byte[1024];
int i=0;
try {
is = new FileInputStream(servlet.getServletContext().getRealPath("")+"\\Magazine\\"+path);
os = response.getOutputStream();
/**//*在页面上弹出一个下在窗口*/
response.setContentType("application/x-msdownload");
/**//*设置报头信息,弹出窗口中显示的文件名 newpath*/
response.setHeader("Content-Disposition", "Disposition; filename="+newpath);
//设置文件大小
response.setContentLength((int) f.length());
/**//*具体的输入输出流操作*/
while((i=is.read(b))!=-1){
os.write(b, 0, i);
i=0;
}
os.flush();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
os.close();
is.close();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
本文提供了一个解决Struts框架中页面打开下载文件时遇到的中文乱码问题的方法,通过使用URL编码技术确保文件路径正确显示并正常下载。
2052

被折叠的 条评论
为什么被折叠?



