Struts2下载遇到的问题。。。
第一种写法==============publicInputStreamgetInputStream()throwsException{try{response.setH...
第一种写法
==============
public InputStream getInputStream() throws Exception {
try {
response.setHeader("Content-Disposition", "attachment;fileName="+
java.net.URLEncoder.encode("测试.rar","UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
===============
第二种写法
public InputStream getInputStream() throws Exception {
InputStream is = new FileInputStream("d:\\test.jpg");
try {
response.setHeader("Content-Disposition", "attachment;fileName="+
java.net.URLEncoder.encode("测试.rar","UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
第一种写法:出现错误:ERROR StreamResult:34 - Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
第二种写法:抛出异常:
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:753)
----------------------------
这是什么原因? 展开
==============
public InputStream getInputStream() throws Exception {
try {
response.setHeader("Content-Disposition", "attachment;fileName="+
java.net.URLEncoder.encode("测试.rar","UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
===============
第二种写法
public InputStream getInputStream() throws Exception {
InputStream is = new FileInputStream("d:\\test.jpg");
try {
response.setHeader("Content-Disposition", "attachment;fileName="+
java.net.URLEncoder.encode("测试.rar","UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
第一种写法:出现错误:ERROR StreamResult:34 - Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
第二种写法:抛出异常:
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:753)
----------------------------
这是什么原因? 展开
2个回答
展开全部
struts-xml:
<action name="download"
class="com.test.action.DownLoadAction">
<result name="success" type="stream">
<param name="contentType">
application/octet-stream;charset=utf-8
</param>
<param name="contentDisposition">
attachment;filename=${name}
</param>
<param name="inputName">downloadFile</param>
</result>
</action>
action:
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public InputStream getDownloadFile() throws UnsupportedEncodingException {
this.name = new String(name.getBytes("ISO8859-1"), "utf-8");
System.out.println(name+"=============");
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/"+ this.getName());
}
<action name="download"
class="com.test.action.DownLoadAction">
<result name="success" type="stream">
<param name="contentType">
application/octet-stream;charset=utf-8
</param>
<param name="contentDisposition">
attachment;filename=${name}
</param>
<param name="inputName">downloadFile</param>
</result>
</action>
action:
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public InputStream getDownloadFile() throws UnsupportedEncodingException {
this.name = new String(name.getBytes("ISO8859-1"), "utf-8");
System.out.println(name+"=============");
return ServletActionContext.getServletContext().getResourceAsStream(
"/upload/"+ this.getName());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询