日期:2014-05-18  浏览次数:20729 次

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