在struts2上傳方法中,我可以選擇上傳文件必須保存的位置。我的意思是,網絡中的所有示例都要求我存儲在WEB-INF中,這肯定不是一個好主意。我希望能夠將上傳的文件存儲在我的磁盤中的任何位置。使用struts2在網絡應用程序中上傳文件並下載
我該怎麼做?我可以在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)
我想,但我沒有得到任何結果..什麼這是錯的嗎?