Hi,
FileUploadAction.java File:
package com.action;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport implements ServletRequestAware {
/**
*
*/
private static final long serialVersionUID = 1L;
private File myFile;
private
String myFileContentType;
private String myFileFileName;
private HttpServletRequest servletRequest =null;
public File getMyfile() {
return myFile;
}
public void setMyfile(File myfile) {
this.myFile = myfile;
System.out.println("file" );
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
System.out.println("content" );
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
public String execute() {
try{
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.myFileFileName);
FileUtils.copyFile(this.myFile, fileToCreate);
}catch(Exception e){
e.printStackTrace();
return INPUT;
}
return SUCCESS;
}
}
upload.jsp File
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>File Upload</title>
</head>
<body>
<s:form action="upload" method="post" enctype="multipart/form-data">
<s:file name="myFile" label="Upload Your File"></s:file>
<br/>
<s:submit label="UPLOAD"></s:submit>
</s:form>
</body>
</html>
struts.xml File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE
struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<constant name="struts.multipart.maxSize" value="1000000" />
<package name="default" extends="struts-default">
<action name="upload" class="com.action.FileUploadAction">
<interceptor-ref name="basicStack"></interceptor-ref>
<interceptor-ref name="fileUpload">
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
</struts>
struts.properties File
struts.multipart.saveDir=/tmp
In my application, filepath are not set on FileUploadAction as per my understanding.If this is a problem, then how it will set on FileUploadAction.
Please give some suggestion.
Thanks in advance.