可实现文件下载功能,但会抛出异常,请哪位大侠分析一下帮我解决这个问题
jsp页面:
<a class="buttonnormal" href="javascript:void(0);" onclick="window.location='${pageContext.request.contextPath }/software/download.do?software.softwareid=${softwareid}'">下载</a></td>
action层:
public String softwareDownload() {
this.service.softwareDownload(software);
return ActionRSEnum.SUCCESS.getRS();
}
service层:
@Override
public void softwareDownload(Software software) {
HttpServletResponse response = ServletActionContext.getResponse();
try {
SoftwareBean softwareBean = new SoftwareBean();
SoftwareBaseBean softwareBaseBean = new SoftwareBaseBean();
softwareBean.setSoftwareid(software.getSoftwareid().toString());
softwareBaseBean = softwareInfo(softwareBean);
String downloadPath = softwareBaseBean.getDownloadPath();
String fileName = softwareBaseBean.getFileName();
File file = new File(downloadPath);
FileInputStream in = new FileInputStream(file);
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
int len = 0;
byte buffer[] = new byte[1024];
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
action.xml配置:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="software" extends="struts-default" namespace="/software">
<action name="download" class="webui.action.SoftwareAction" method="softwareDownload">
<result name="success">
/Jsp/software/software_list.jsp
</result>
<result name="error">/Jsp/error/error.jsp</result>
</action>
</package>
</struts>
这里不需要用DAO层
我是通过前台传进去id,根据ID获得绝对路径进行下载功能的,能够实现下载功能,可就是抛出异常:
2012-08-14 16:25:33.246:WARN::Committed before 500 STREAM
2012-08-14 16:25:33.246:WARN::/software/download.do
java.lang.IllegalStateException: Committed
at org.mortbay.jetty.Response.resetBuffer(Response.java:1024)
at org.mortbay.jetty.Response.sendError(Response.java:240)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:852)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:534)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at webui.filter.FilterIsLogin.doFilter(FilterIsLogin.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
out.close之后我也没做什么啊
我不用加
response.setContentType("application/octet-stream");//设置文件类型
也可以下载jar,png等格式的文件类型啊
还有,我也并没有用到submit提交表单的,是通过访问连接实现的
我没找到flb7751415451所说的out.clear()方法啊,这个怎么用的?
[解决办法]
// 下载本地文件 public void downloadLocal(HttpServletResponse response)throws FileNotFoundException { String fileName = "1.xlsx".toString(); // 文件的默认保存名 // 读到流中 InputStream inStream = new FileInputStream("D:/1.xlsx");// 文件的存放路径 // 设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } }