Work notes 4. upload files to the server in struts2 and 4. upload files in struts2

Source: Internet
Author: User

Work notes 4. upload files to the server in struts2 and 4. upload files in struts2


This article introduces two methods: uploading files to the server

Either Form submission or Form submissionAjaxfileupload: asynchronous upload.


I. In JSP:

1. Submit Form

To complete file upload, we should set the enctype attribute of the form with 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 a file: <input type = "file" name = "upload"/> <br> <input value = "upload" type = "submit"/> </form>


2. asynchronous upload of ajaxfileupload

<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 the attachment function uploadFile () {$. ajaxFileUpload ({url: 'tenantcredit _ uploadFile. action ', // type of the server on which you process the uploaded file : 'Post', secureuri: false, fileElementId: 'fileupload', ype: 'text', success: function (data) {if (data = "true ") {alert ("File Uploaded successfully! ");} 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 "/>


2. How can I get a file in Action?

The upload attribute corresponds to the upload attribute of the previous form field, which is used to encapsulate the Request Parameters of the form field.


Action contains two attributes:
UploadFileName: name of the file to be uploaded
UploadContentType: the file type that encapsulates the uploaded file. The Action class directly encapsulates the File content of the uploaded File through the File type attribute, but this File attribute cannot obtain the File name and File type of the uploaded File, therefore, Struts2 encapsulates the uploaded file name and file type information in the file domain into the uploadFileName and uploadContentType attributes.


It can be considered that if the form contains a file field whose name attribute is xxx, the corresponding Action needs to use three attributes to encapsulate the information of the file field:

The xxx attribute of the File type encapsulates the File content corresponding to the File domain. (The upload in the File upload attribute is the prefix of the following two string attributes)
The xxxFileName attribute of the String type encapsulates the file name corresponding to the file field.
The xxxContentType attribute of the String type encapsulates the file name corresponding to the file field.


The preceding three attributes allow you to upload files more easily. Therefore, you can call the getXxx () method to obtain the file name, file type, and content.

// Encapsulate the private File upload attribute of the uploaded File domain; // encapsulate the private String uploadContentType attribute of the uploaded File type; // encapsulate the private String uploadFileName attribute of the uploaded File name; // mark the File upload result: Successful/failed 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. uploadConte NtType = 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 (realpath), uploadFileName); if (! Savefile. getParentFile (). exists () savefile. getParentFile (). mkdirs (); FileUtils. copyFile (upload, savefile); ActionContext. getContext (). put ("message", "File Uploaded successfully");} // prompt: uploaded successfully result = "true"; outPrint (response, result);} catch (Exception e) {// prompt: Upload Failed String result = "false"; outPrint (response, result);} return null ;}


3. Compare the advantages and disadvantages of the two methods:

1. verify whether the selected file is:

① Form submission method:

Disadvantage: if the file is not selected, return false after JS capture, and Form will also be submitted! You also need to determine in the Action method.

② Ajax method:

If no file is selected, return false is determined by JS, and Action is not submitted.

2. how to pass Parameters

① Form submission method-2:

Use the ModelDriven attribute to directly obtain the value of the corresponding control in the Action.

Or identify an id in JSP, and then define the get and set methods for this id in Action, you can directly get it.

② Ajax methods-3 types:

Same as ①

Or passing parameters through the url in JS

3. Receive the returned results in the Action

① Form submission method:

Put the returned results in the value stack/Session, and then retrieve them in JSP.

② Ajax method:

Same as ①

Or the JS callback function obtains the return value of Action through data.

Or the JS callback function uses data to obtain outPrint in the Action.


Based on the above comparison, we recommend that you use Ajax to upload files.





How can I upload a struts2 file?

This should not happen. The front-end is Chinese. If it is an error, the background should be garbled. I don't know if you are under windows or linux.
Hope to help you.

Javaee uses struts2 files to upload and save files. The server under the server directory is tomcat. Why does the file disappear after the computer is restarted?

First,
Files uploaded to the server are stored on the disk and the disk is a permanent storage medium. If they are not manually deleted or poisoned (this is unlikely), they will not be lost. You may have uploaded files on the server during the last running, and then you re-released the project to directly overwrite the original project, because you may not have saved your new image in your original project, it is lost directly. We recommend that you back up the uploaded file first if you have an important file to be uploaded, release a new project. Or when you release a new file, set the release option to only overwrite the old file. Do not delete all files and upload the new project,




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.