Struts2中文件下载出现java.lang.IllegalStateException异常如何解决
Struts2中文件下载出现java.lang.IllegalStateException异常如何解决
java代码:
package com.elong.oa.web; import java.io.File; import java.io.InputStream; import java.io.UnsupportedEncodingException; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.Action; /** * @author Administrator * */ /** * @author Administrator * */ public class FileDownLoadAction extends BaseAction { private String fileName; public void setFileName(String fileName) { try { this.fileName = new String(fileName.getBytes("ISO-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getFileName() throws Exception { //System.out.println(fileName.substring(fileName.lastIndexOf("/"),fileName.length())+"__name"); return new String(fileName.substring(fileName.lastIndexOf("\")+1,fileName.length()).getBytes("utf-8"),"ISO-8859-1"); } public InputStream getInputStream() { return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName); } public String execute(){ System.out.println("=========="); System.out.println("/"+fileName); System.out.println(ServletActionContext.getServletContext().getResourceAsStream("/" + fileName)); return "success"; } }
<action name="FileDownload" class="com.elong.oa.web.FileDownLoadAction"> <result type="stream" name="success"> <param name="contentType">text/plain</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">8192</param> </result> </action>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误的详细信息帖出来