• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Struts2 file upload to a specific path

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic