首页上一页 1 下一页尾页 1 条记录 1/1页
尊敬的明日科技的老师
发表在JavaWeb图书答疑
2014-06-15
是否精华
是
否
版块置顶:
是
否
尊敬的明日科技的老师:
您好,我最近遇到一个用struts2实现文件下载的问题,请您给看看,源代码如下:
DownloadAction.java中:
public class DownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String inputPath;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getTargetFile() throws Exception{
InputStream in=ServletActionContext.getServletContext().getResourceAsStream(inputPath);//in值为null,不知为什么?
System.out.println(in);
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String getDownloadFileName() {
String downFileName = inputPath.substring(7);
System.out.println("文件名"+downFileName);
try {
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return downFileName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
struts.xml中:
<action name="downaction" class="action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
jsp中:
<a href="downaction?inputPath=<%=ww.getWjdz()%>"><%=ww.getWjmc() %></a>
文件地址是绝对路径,即tomcat服务器所在路径下的地址。即wjdz=realPath+"\\"+uploadFileName;存到数据库,再从数据库取地址。
改为相对地址也试过,也报同样的错,不知为什么?
报的错为:java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [targetFile]
in the invocation stack. Check the <param name="inputName"> tag specified for this action.
一直报这个错,怎么改都是这个错,请您给看看,打扰了,谢谢。
您好,我最近遇到一个用struts2实现文件下载的问题,请您给看看,源代码如下:
DownloadAction.java中:
public class DownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String inputPath;
public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public InputStream getTargetFile() throws Exception{
InputStream in=ServletActionContext.getServletContext().getResourceAsStream(inputPath);//in值为null,不知为什么?
System.out.println(in);
return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}
public String getDownloadFileName() {
String downFileName = inputPath.substring(7);
System.out.println("文件名"+downFileName);
try {
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
} catch (Exception e) {
e.printStackTrace();
}
return downFileName;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
struts.xml中:
<action name="downaction" class="action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
jsp中:
<a href="downaction?inputPath=<%=ww.getWjdz()%>"><%=ww.getWjmc() %></a>
文件地址是绝对路径,即tomcat服务器所在路径下的地址。即wjdz=realPath+"\\"+uploadFileName;存到数据库,再从数据库取地址。
改为相对地址也试过,也报同样的错,不知为什么?
报的错为:java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [targetFile]
in the invocation stack. Check the <param name="inputName"> tag specified for this action.
一直报这个错,怎么改都是这个错,请您给看看,打扰了,谢谢。