aspose file tools*
The moose likes Struts and the fly likes Struts2 file upload to a specific path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 file upload to a specific path" Watch "Struts2 file upload to a specific path" New topic
Author

Struts2 file upload to a specific path

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
hi,

i am using file upload interceptor for file uploads.
If i mention a static path the file is getting stored in that location. if i mention the path using context.getRealPath("/"); it is storing in eclipse workshop internal .
folders.The Eclipse workspace is in D: folder. My application name is Struts2proj and the actual file should be stored in Struts2proj/Images.
but when i print the filepath it is showing in this way " D:\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Struts2proj\Images"

here is the code

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements ServletContextAware,
ServletRequestAware {
private File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;
// ServletContext context = ServletActionContext.getServletContext();
ServletContext context;

public void setServletContext(ServletContext context) {
this.context = context;
}


public String execute() {
try {

String filePath = context.getRealPath("/")+"Images";

System.out.println("Server path:" + filePath);

File fileToCreate = new File(filePath, this.userImageFileName);

FileUtils.copyFile(this.userImage, fileToCreate);


} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());

return INPUT;
}
return SUCCESS;
}

public File getUserImage() {
return userImage;
}

public void setUserImage(File userImage) {
this.userImage = userImage;
}

public String getUserImageContentType() {
return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {
this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {
return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {
this.userImageFileName = userImageFileName;
}

@Override
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;

}
}



help me out

Thanks
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485



ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts2 file upload to a specific path