wood burning stoves 2.0*
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 file upload to a specific path" Watch "Struts2 file upload to a specific path" New topic

Struts2 file upload to a specific path

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125

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) {

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;

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


help me out

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.
It is sorta covered in the JavaRanch Style Guide.
subject: Struts2 file upload to a specific path
Similar Threads
using struts2 i want to upload image but nullpointer exception occur
upload image usinng struts2
Struts 2 Fileupload problem
Interceptor make chages which Unknown to us.
how to upload an image to a specific folder & displaying it in struts2