Asynchronous file upload is required when processing file uploads. The JqueryAjaxFileUploader component is used here. The server uses struts2 to process file uploads. I download the project code.
Due to project requirements, asynchronous file upload is required when processing file uploads. Here we use Jquery Ajax File Uploader this component: http://www.phpletter.com/download_project_version.php? Version_id = 6
The server uses struts2 to process file uploads.
Required environment:
Jquery. js
Ajaxfileupload. js
Jar package on which struts2 depends
And struts2-json-plugin-2.1.8.1.jar
Compile the Action for File Upload
The Code is as follows:
Package com. ajaxfile. action;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;
@ SuppressWarnings ("serial ")
Public class FileAction extends ActionSupport {
Private File file;
Private String fileFileName;
Private String fileFileContentType;
Private String message = "you have successfully uploaded the file ";
Public String getMessage (){
Return message;
}
Public void setMessage (String message ){
This. message = message;
}
Public File getFile (){
Return file;
}
Public void setFile (File file ){
This. file = file;
}
Public String getFileFileName (){
Return fileFileName;
}
Public void setFileFileName (String fileFileName ){
This. fileFileName = fileFileName;
}
Public String getFileFileContentType (){
Return fileFileContentType;
}
Public void setFileFileContentType (String fileFileContentType ){
This. fileFileContentType = fileFileContentType;
}
@ SuppressWarnings ("deprecation ")
@ Override
Public String execute () throws Exception {
String path = ServletActionContext. getRequest (). getRealPath ("/upload ");
Try {
File f = this. getFile ();
If (this. getFileFileName (). endsWith (". exe ")){
Message = "sorry, the format of the file you uploaded is not allowed !!! ";
Return ERROR;
}
FileInputStream inputStream = new FileInputStream (f );
FileOutputStream outputStream = new FileOutputStream (path + "/" + this. getFileFileName ());
Byte [] buf = new byte [1, 1024];
Int length = 0;
While (length = inputStream. read (buf ))! =-1 ){
OutputStream. write (buf, 0, length );
}
InputStream. close ();
OutputStream. flush ();
} Catch (Exception e ){
E. printStackTrace ();
Message = "sorry, File Upload Failed !!!! ";
}
Return SUCCESS;
}
}
Struts. xml
The Code is as follows:
Text/html
Text/html
Observe the result configuration in struts. xml in combination with Action.
The contentType parameter is required. Otherwise, the browser always prompts to save the returned JSON result as a file and will not submit it to ajaxfileupload for processing. This is because the default contentType of struts2 JSON Plugin is application/json, while the ajaxfileupload requires text/html.
Jsp page for File Upload
The Code is as follows:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
Insert title here