[英]File upload and Download in a web app using struts2
In struts2 upload methods, can I choose where the uploaded file must be saved. 在struts2上载方法中,我可以选择上载文件的保存位置。 I mean, all the examples in web ask me to store in WEB-INF which surely is not a good idea.
我的意思是,Web中的所有示例都要求我将其存储在WEB-INF中,这肯定不是一个好主意。 I want to be able to store the uploaded file in any place in my disk.
我希望能够将上传的文件存储在磁盘上的任何位置。
How should i do it? 我该怎么办? Can i do it with help of ServletContextAware interceptor ?
我可以借助ServletContextAware拦截器吗?
When I use 当我使用
public class DownloadFileAction extends ActionSupport implements ServletContextAware{
//private InputStream inputStream;
private int fileid;
private ServletContext servletContext;
private FileCrud fileCrud;
private MyFile myFile;
public FileCrud getFileCrud() {
return fileCrud;
}
public void setFileCrud(FileCrud fileCrud) {
this.fileCrud = fileCrud;
}
public MyFile getMyFile() {
return myFile;
}
public void setMyFile(MyFile myFile) {
this.myFile = myFile;
}
public InputStream getInputStream(){
String homepath = "c:\\files";
String fname = null;
try{
fname=getFileCrud().getAFileName(getFileid());
}catch(Exception e){
e.printStackTrace();
}
String thePathToFile = homepath+File.separator+fname;
//File targetfile = new File(thePathToFile);
return getServletContext().getResourceAsStream(thePathToFile);
}
// public void setInputStream(InputStream inputStream) {
// this.inputStream = inputStream;
// }
public int getFileid() {
return fileid;
}
public void setFileid(int fileid) {
this.fileid = fileid;
}
public ServletContext getServletContext() {
return servletContext;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public String execute(){
return SUCCESS;
}
}
and struts xml file is 和struts xml文件是
<action name="fileDownload" class="com.projit1.file.DownloadFileAction">
<result type="stream" name="success">
<param name="inputName">inputStream</param>
<param name="contentType">application/octet-stream</param>
</result>
</action>
I get the following error 我收到以下错误
javax.servlet.ServletException: java.lang.IllegalArgumentException: 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.
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
I am trying but am not getting any results.. what is wrong in this ? 我正在尝试,但未得到任何结果。
To write an InputStream
to a File
, you need FileOutputStream
. 要将
InputStream
写入File
,需要FileOutputStream
。
To get an InputStream
from a File
, you need FileInputStream
. 要从
File
获取InputStream
,需要FileInputStream
。
In your code you're trying to allocate a file using ServletContext#getResourceAsStream()
, but this is intented to allocate a classpath resource. 在您的代码中,您尝试使用
ServletContext#getResourceAsStream()
分配文件,但这旨在分配类路径资源。 Replace it by new FileInputStream(file)
. 用
new FileInputStream(file)
替换它。
Having just finished implementing this myself. 刚刚自己完成此操作。 I suggest tackling the two problems separately.
我建议分别解决这两个问题。 Do the file uploads first then the file downloads.
先上传文件,然后下载文件。 One immediate issue with your download support is that you don't have a
public getInputStream()
method per your result configuration: 下载支持的一个直接问题是,您的结果配置没有
public getInputStream()
方法:
<param name="inputName">inputStream</param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.