可实现文件下载功能,但会抛出异常,请哪位大侠分析一下帮我解决这个问题
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 (Unsupported
EncodingException 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:44