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
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 File; private String fileFileName; private String fileFileContentType; private String message = "you have uploaded the file successfully"; pu Blic 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 file FileContentType;} 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 file format you uploaded is not allowed !!! "; Return ERROR;} FileInputStream inputStream = new FileInputStream (f); FileOutputStream outputStream = new FileOutputStream (path +"/"+ this. getFileFileName (); byte [] buf = new byte [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
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
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
Insert title here<Script type = "text/javascript" src = "js/jquery. js "> </script> <script type =" text/javascript "src =" js/ajaxfileupload. js "> </script> <script type =" text/javascript "> function ajaxFileUpload () {$ (" # loading "). ajaxStart (function () {$ (this ). show () ;}) // an image is displayed when the file is uploaded. ajaxComplete (function () {$ (this ). hide () ;}); // hide the image after the file is uploaded $. ajaxFileUpload ({url: 'fileuploadaction. action ', // secureuri: false, server-side request address used for file upload, // set to false fileElementId: 'file', // id attribute of the file upload SpaceDataType: 'json', // the return value type is generally set to json success: function (data, status) // the server responds to the processing function {alert (data. message); // extract the data in the message from the json returned by the server. The message is the member variable if (typeof (data. error )! = 'Undefined') {if (data. error! = '') {Alert (data. error);} else {alert (data. message) ;}}, error: function (data, status, e) // server response failure handler {alert (e) ;}}) return false ;} </script>
Normal asynchronous submission
$. Ajax ({
Url: 'delpicture.html ',
Type: 'post ',
Data: 'proid = '+ proId +' & path = '+ path,
DataType: 'json ',
Success: function (data ){
Alert (data. message );
Window. location. reload ();
},
Error: function (data ){
Alert (data. message );
Window. location. reload ();
}
});
Success and error must be configured as follows
View my configuration. The same class uses different action names to call different methods in FileAction.
Text/html
Text/html
Text/html
Text/html