<action name="download" class="com.mengfang.actions.DownloadAction">
<result name="success" type="stream">
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
public InputStream getDownloadFile()
{
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream(
"/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+"/" + fileName);
}
如果你上传到服务器之后,立刻进行下载,是可以下载的,重启服务器再点击下载的话,就会报你那个错了。
{
private static final long serialVersionUID = 6329383258366253255L;
private String fileName;
private String fileRealName;
public void setFileName()
{
// 得到请求下载的文件名
String fname = ServletActionContext.getRequest().getParameter("name");
String frealname = ServletActionContext.getRequest().getParameter("realName");
try
{
/*
* 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
* 这里使用request.setCharacterEncoding解码无效.
* 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
*/
fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
frealname= new String(frealname.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e)
{
e.printStackTrace();
}
this.fileName = fname;
this.fileRealName = frealname;
System.out.println(fileName);
System.out.println(fileRealName);
}
/*
* @getFileName 此方法对应的是struts.xml文件中的: <param
* name="contentDisposition">attachment;filename="${fileName}"</param>
* 这个属性设置的是下载工具下载文件时显示的文件名, 要想正确的显示中文文件名,我们需要对fileName再次编码
* 否则中文名文件将出现乱码,或无法下载的情况
*/
public String getFileName() throws UnsupportedEncodingException
{
fileRealName = new String(fileRealName.getBytes(), "ISO-8859-1");
return fileRealName;
} /*
* @getDownloadFile 此方法对应的是struts.xml文件中的: <param
* name="inputName">downloadFile</param> 返回下载文件的流,可以参看struts2的源码
*/ public InputStream getDownloadFile()
{
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream(
"/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+"/" + fileName);
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
{
this.setFileName();
return ServletActionContext.getServletContext().getResourceAsStream(
"/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+"/" + fileName);
}
改成:public InputStream getDownloadFile throws FileNotFoundException(){
this.setFileName();
File file = new File("/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+"/" + fileName);
InputStream is = new FileInputStream(file);
return is;
}
就行了
我也遇到这样的问题,结果打印日志路径没错误 inputStream也不为空,路径也不存在中文乱码的情况,因为不含有中文。还请楼主解惑11:29:42,362-org.xjtu.framework.modules.normalUser.action.DownloadAction-32100[http-8080-1]INFO org.xjtu.framework.modules.normalUser.action.DownloadAction-/home/RenderFarm/apache-tomcat-6.0.37/logs/
org.xjtu.framework.modules.normalUser.action.DownloadAction-32476[http-8080-1]INFO org.xjtu.framework.modules.normalUser.action.DownloadAction-renderResult不是空的
[framework]
11:29:42,762-org.apache.struts2.dispatcher.StreamResult-32500[http-8080-1] ERRORorg.apache.struts2.dispatcher.StreamResult-Can not find a java.io.InputStream with the name [renderResult] in the invocation stack. Check the <param name="inputName"> tag specified for this action
为什么需要爱通过FileInputStream封装下就可以了呢?我看其他文章都是没封装 直接就可以了,但是我在运行的时候出现异常,改成您的方法就i对了