Struts1+hibernate框架中 Java代码下载文件 页面不显示下载框
代码如下:使用的是ajax请求public void file(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
// HttpServletResponse response = ServletActionContext.getResponse();
// String na = ServletActionContext.getServletContext().getRealPath("/");
String na = request.getSession().getServletContext().getRealPath("/");
String n ="1.jpg";
String m = "图片.jpg";
InputStream ins = null;
ServletOutputStream outs = null;
try{
File file = new File(na+n);
System.out.println(file.exists());
if(file.exists()){
response.reset();
response.setContentType("application/octet-stream; charset=UTF-8");
response.setHeader("Content-disposition",
"attachment;filename=" +
URLEncoder.encode(m, "utf-8"));
response.setHeader("Content-Length", String.valueOf(file.length()));
outs = response.getOutputStream();
ins = new FileInputStream(file);
byte[] b = new byte[1024 * 10];
int len = 0;
while ((len = ins.read(b)) != -1) {
outs.write(b, 0, len);
}
//return mapping.findForward("mssys_view");
System.out.println(outs.toString());
//return null;
}
}catch(Exception e){
}finally{
try {
if (outs != null)
outs.close();
if (ins != null)
ins.close();
} catch (Exception e) {
outs = null;
ins = null;
}
}
//return "file";