Enabling the Struts 2 file upload feature
Write a new action to fetch the uploaded file information.
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
private File file;
private String contentType;
private String filename;
public void setFileUpload(File file) {
this.file = file;
}
public void setFileUploadContentType(String contentType) {
this.contentType = contentType;
}
public void setFileUploadFileName(String filename) {
this.filename = filename;
}
public String execute() {
//...
return SUCCESS;
}
}
Note:
The highlighted terms in the set
methods should exactly match the name of
the file. If the action is extended from BaseAction
, call the corresponding
super-method to set the file information. (In BaseAction
, it can only consume a
file whose name is
filename
.)
public void setFileUpload(File file) {
super.setFilename(file);
this.file
= file;
}
public void setFileUploadContentType(String contentType) {
super.setFilenameContentType(contentType);
this.contentType = contentType;
}
public void setFileUploadFileName(String filename) {
super.setFilenameFileName(filename);
this.filename = filename;
}
public String execute() {
//...
return SUCCESS;
}
}