I've been trying to implement struts2.1.8 file upload and it fails in the Action with a NullPointerException.It seems like file object is not being created. Saw similar questions and answers here on Stack, tried all recommended tricks but not being successful. Maybe my problem just needs an extra pair of eyes.
Action
public class CampaignUploadFileAction extends BaseAction {
private static final Logger log = Log.getLogger(CampaignFileUploadAction.class);
private List<String> arialTables = new ArrayList<String>();
private String arialTable;
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public String execute(){
log.info(this.getClass().getName() + "-execute() start");
populateArialTables();
return SUCCESS;
}
private void populateArialTables(){
log.info(this.getClass().getName() + "-populateArilTables() start");
for(ArialDBTables table : ArialDBTables.values()){
arialTables.add(table.name());
}
}
public String uploadFile(){
log.info(this.getClass().getName() + "-uploadFile() start");
log.info("arialTable[" + getArialTable() + "]");
log.info("fileName[" + getFileUploadFileName() + "]");
log.info("filePath[" + getFileUpload().toString() + "]");
log.info("fileContentType[" + getFileUploadContentType() + "]");
return SUCCESS;
}
public List<String> getArialTables() {
return arialTables;
}
public void setArialTables(List<String> arialTables) {
this.arialTables = arialTables;
}
public String getArialTable() {
return arialTable;
}
public void setArialTable(String arialTable) {
this.arialTable = arialTable;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
}
struts.xml
<action name="fileUploadAction" class="fileUploadAction" method="uploadFile">
<interceptor-ref name="fileUpload">
<param name="maximumSize">350000000</param>
</interceptor-ref>
<interceptor-ref name="basicStack" />
<result name="success">/jsp/admin/result.jsp</result>
</action>
Html Form
<s:form enctype="multipart/form-data" action="fileUploadAction.action" method="post" namespace="/">
<table style="width:600px">
<tr>
<td>Select file to upload</td>
<td><s:file name="fileUpload"/></td>
</tr>
<tr>
<td>Select table to upload to</td>
<td><s:select name="arialTable" list="arialTables" key="arialTable" headerKey="" headerValue="---Select Table---"></s:select></td>
</tr>
</table>
<s:submit value="Upload"/>
</s:form>