Work notes 4.struts2 uploading files to the server

Source: Internet
Author: User


This article describes two ways to upload a file to the server:

One is to submit form forms, and the other is to ajaxfileupload asynchronous uploads.


First, the JSP:

1. Submit Form Form

In order to complete the file upload, we should set the Enctype property of the form containing the two form fields to Multipart/form-data.

<form action= "Uploadfiles_fourinsurancefirstupload.action" method= "post" enctype= "Multipart/form-data" > File title: <input type= "text" name= "title"/><br> Select File: <input type= "file" name= "upload"/><br>< Input value= "upload" type= "submit"/></form>


2,ajaxfileupload asynchronous upload

      <script src= "${pagecontext.request.contextpath}/script/jquery.js" type= "Text/javascript" ></script>      <script src= "${pagecontext.request.contextpath}/script/ajaxfileupload.js" type= "Text/javascript" >< /script><pre name= "Code" class= "JavaScript" >            <script type= "Text/javascript" language= "JavaScript" >//Upload attachment function uploadfile () {$.ajaxfileupload ({url: ' tenantcredit_uploadfile.action ',//the server you are working on to upload the file type: ' Post ') , Secureuri:false,fileelementid: ' FileUpload ', DataType: ' Text ', success:function (data) {if (data = = "true") {alert ("Upload File succeeded! ");} else {alert ("Upload failed!

" );}}})} </script> <input type= "file" id= "fileUpload" name= "Upload" value= "upload"/> <input type= "button" id= "Btnuploadfile" onclick= "Javascript:uploadfile ();" Value= "Save Record"/>


How do I get the file in action?

The Upload property corresponds to the Upload property of the previous form field, respectively. The number of request parameters used to encapsulate the form field.


There are two properties included in the action:
Uploadfilename: Encapsulates the file name of the uploaded file
Uploadcontenttype: Encapsulates the file type of the uploaded file.

The action class directly encapsulates the file contents of the uploaded file directly through the file Type property, but this file property cannot get the filename and file type of the uploaded file. So Struts2 encapsulates the upload file name and file type information that is included in the file domain directly into the Uploadfilename and Uploadcontenttype properties.


Be able to think that if you include a file field with the name attribute xxx in the form, the corresponding action needs to use three properties to encapsulate the file domain information:

The XXX property of type file encapsulates the corresponding file contents of the file domain. (The upload in the File upload attribute in the text is the prefix of the following two string properties)
The Xxxfilename property of type string encapsulates the file name of the corresponding file in the file domain.
The Xxxcontenttype property of type string encapsulates the file name of the corresponding file in the file domain.


The above three properties make it easier to upload files, so you can get the file name, file type, and file contents of the uploaded file directly by calling the GetXXX () method.

Encapsulates the properties of the uploaded file domain private file upload;//encapsulates the properties of the file type private string uploadcontenttype;//The property of the upload file name private string uploadfilename;//tag Upload file result: Success/failure private String Result;public file Getupload () {return upload;} public void Setupload (File upload) {this.upload = upload;} Public String Getuploadcontenttype () {return uploadcontenttype;} public void Setuploadcontenttype (String uploadcontenttype) {this.uploadcontenttype = Uploadcontenttype;} Public String Getuploadfilename () {return uploadfilename;} public void Setuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename;} Public String GetResult () {return result;} public void Setresult (String result) {This.result = result;} Upload the attachment public String UploadFile () {try {String Realpath = Servletactioncontext.getservletcontext (). Getrealpath ("/data" ); if (upload! = null) {File SaveFile = new file (new file (Realpath), uploadfilename), if (!savefile.getparentfile (). Exists ( )) Savefile.getparentfile (). Mkdirs (); Fileutils.copyfile (upload, savefile); ActionContext.getcontext (). Put ("message", "File Upload succeeded");} Hint: successful upload result = "true"; outprint (response, result);} catch (Exception e) {//Hint: Upload failed string result = "false"; Outprint (response, result);} return null;}


Third, compare the advantages and disadvantages of the above two methods:

1. Verify that the file is selected:

① Submit Form Mode:

Disadvantage: If the file is not selected, after capturing with JS, return false will also be submitted form! You also need to make a decision in the action method.

②ajax Way:

If the file is not selected, use JS to determine return false, the action will not be submitted

2. How to pass the number of parameters

① Submit Form Mode-2 kinds:

Use the Modeldriven property to get the value of the corresponding control directly in the action

Or a JSP identifies an ID, and the GET, set method that defines the ID in the action can be directly taken.

②ajax Way-3 kinds:

With ①

or through the URL of JS to participate in the way

3. Receive the returned result from the action

① Submit Form Mode:

Put the return result in the value stack/session, and then take out the JSP.

②ajax Way:

With ①

Or JS callback function to get the return value of the action via data.

or JS callback function to get action in outprint via data


synthesis of the above comparison. recommend uploading files using AJAX methods




Work notes 4.struts2 uploading files to the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.