Java 下载功能怎么实现
程序代码:
public String excute() { String filePath = "D:\\a.txt";// 下载的文件路径 getfileAddress(filePath); try { InputStream iso = new FileInputStream(filePath); HttpServletResponse response = ServletActionContext.getResponse(); System.out.println("downloadForm******* "+response); // response.reset();//清除缓存 response.setHeader("Content-Disposition","attachment;fileName="+filePath); response.setContentType("text/html;charset=UTF-8"); ServletOutputStream out = response.getOutputStream(); byte[] content = new byte[1024]; int length =0; while((length = iso.read(content))!=-1){ out.write(content,0,length); } out.write(content); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
上面是我实现txt文件下载的一个方法,用struts2框架,重前台页面触发下载功能,提交Action,到实现方法,但是我现在不能下,报错提示说是
: Exception initializing page context
java.lang.IllegalStateException: Cannot create a session after the response has been committed
at org.apache.catalina.connector.Request.doGetSession(Request.java:2675)
上面这个报错,
HttpServletResponse response = ServletActionContext.getResponse();
这句话也报错,可能是获取不到response,response过期等信息,请问有谁做过前台的,帮忙看下,怎么解决