2
上傳1-6969875-2644-t.jpg
時,圖片正確上傳至 /home/xrcwrn/jvm/apache-tomcat-6.0.14/domains/xyz.com/ROOT/img/1-6969875-2644-t.jpg
。如何將struts 2中的照片上傳到我的public_html目錄而不是我的tomcat目錄中?
但我其實想把這個圖像上傳到/home/xrcwrn/public_html/img
文件夾中。
我使用這段代碼的Struts 2:
public String execute() {
try {
ServletContext servletContext = ServletActionContext.getServletContext();
String path =servletContext.getRealPath("/img");
System.out.println("Server path:" + path);
String filePath = servletContext.getRealPath(path);
File uploadDir = new File(filePath);
String relPath = uploadDir.getAbsolutePath();
//if the folder does not exits, creating it
if (uploadDir.exists() == false) {
uploadDir.mkdirs();
}
File fileToCreate = new File(path, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
String pt = path + "/" + getUserImageFileName();
System.out.println("image path is :" + pt);
setImagePath(pt);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
System.out.println(" **************inside image upload***********");
return SUCCESS;
}