Simple File Upload of struts2

Source: Internet
Author: User

1. File Upload process:

Step 1: first use the <s: File> label of the form to receive uploaded files on the client, for example:

1 <% @ page Language = "Java" Import = "Java. util. * "pageencoding =" GBK "%> 2 <% @ taglib prefix =" S "uri ="/Struts-tags "%> 3 <HTML> 4 

Step 2: declare the three attributes in the action and declare the get and set methods.

1 filefieldname (the name must be the same as the name attribute of the <s: filr> label in the form): receives the value of wordfile in the form, that is, the actual path of the received file on the client.

Example: private file wordfile;

2 filefieldnamecontenttype: attributes of the uploaded file; example: Private string wordfilecontenttype

3 filefieldnamefilename: indicates the uploaded file name, for example, private sting wordfilefilename. For example, if the uploaded file is index. jsp, then wordfilefilename = index. jsp

Because both 2 and 3 are attributes of the uploaded file, they all carry the method defined when the <s: File> tag is uploaded.

Sample Code:

 1 package com.atguigu.struts2.FileUpload.app; 2  3 import java.io.File; 4  5 import com.opensymphony.xwork2.ActionSupport; 6  7 public class TestAction extends ActionSupport { 8  9     /**10      * 11      */12     private static final long serialVersionUID = 1L;13     private File wordFile;14     private String wordFileContentTypeString;15     private String wordFileFileName;16     /**17      * @return the wordFile18      */19     public File getWordFile() {20         return wordFile;21     }22     /**23      * @param wordFile the wordFile to set24      */25     public void setWordFile(File wordFile) {26         this.wordFile = wordFile;27     }28     /**29      * @return the wordFileContentTypeString30      */31     public String getWordFileContentTypeString() {32         return wordFileContentTypeString;33     }34     /**35      * @param wordFileContentTypeString the wordFileContentTypeString to set36      */37     public void setWordFileContentTypeString(String wordFileContentTypeString) {38         this.wordFileContentTypeString = wordFileContentTypeString;39     }40     /**41      * @return the wordFileFileName42      */43     public String getWordFileFileName() {44         return wordFileFileName;45     }46     /**47      * @param wordFileFileName the wordFileFileName to set48      */49     public void setWordFileFileName(String wordFileFileName) {50         this.wordFileFileName = wordFileFileName;51     }52     public String execute() throws Exception {53         // TODO Auto-generated method stub54         return super.execute();55     }56 }

File Upload? What is File Upload? Read the client file and write it into the service. Therefore, you must use the input and output streams.

Step 3: declare the input stream and output stream, and write the file to the specified server location:

1) first set the storage location of the file: servletcontext. getrealpath ("/files/" + wordfilefilename); set the file to the Files folder in the root directory,

Wordfilefilename: indicates the name of the file to be uploaded when the Files folder is put.

 

2) read the uploaded file and write the file to the specified location.

Fileinputstream in = new fileinputstream (PPT);/put the file into the input stream fileoutputstream out = new fileoutputstream (DIR); byte [] buffer = new byte [1024]; int Len = 0; while (LEN = in. read (buffer ))! =-1) {// read the message out of the uploaded file from the input stream. write (buffer, 0, Len); // write the uploaded file to the wordfilefilename file in the Files folder on our server}

Complete sample code:
1 servletcontext = servletactioncontext. getservletcontext (); 2 string dir = servletcontext. getrealpath ("/file/" + pptfilename); 3 fileinputstream FCM = new fileinputstream (PPT); 4 fileoutputstream Fos = new fileoutputstream (DIR ); 5 byte [] buffer = new byte [1024]; // because fileinputstream is a byte stream, a byte array should be declared to store 6 int Len = 0 bytes read from the file; 7 while (LEN = Fi. read (buffer ))! =-1) {// FCM. the read () method reads the file. If the read is not complete, the system returns-1 8 Fos if the read is complete. write (buffer, 0, Len); 9} 10 FOS. close (); 11. close ();

 

Simple File Upload of struts2

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.