使用 struts2 在 Web 应用程序中上传和下载文件
在struts2上传方法中,我可以选择上传的文件必须保存在哪里吗?我的意思是,网络中的所有示例都要求我存储在 WEB-INF 中,这肯定不是一个好主意。我希望能够将上传的文件存储在磁盘中的任何位置。
我该怎么做呢?我可以在 ServletContextAware 拦截器的帮助下做到这一点吗?
当我使用
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;
}
}
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>
我收到以下错误,
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)
我正在尝试但没有得到任何结果..这有什么问题?
In struts2 upload methods, can I choose where the uploaded file must be saved. I mean, all the examples in web ask me to store in WEB-INF which surely is not a good idea. 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 ?
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
<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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将
InputStream
写入File
,您需要FileOutputStream
。要从
File
获取InputStream
,您需要FileInputStream
。在您的代码中,您尝试使用 ServletContext#getResourceAsStream() 分配文件,但这旨在分配类路径资源。将其替换为
new FileInputStream(file)
。To write an
InputStream
to aFile
, you needFileOutputStream
.To get an
InputStream
from aFile
, you needFileInputStream
.In your code you're trying to allocate a file using
ServletContext#getResourceAsStream()
, but this is intented to allocate a classpath resource. Replace it bynew FileInputStream(file)
.我自己刚刚完成这个实施。我建议分别解决这两个问题。首先进行文件上传,然后进行文件下载。下载支持面临的一个直接问题是,您的结果配置没有
public getInputStream()
方法: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: