Hi
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 ?