Struts 2 file upload, struts File Upload

Source: Internet
Author: User

Struts 2 file upload, struts File Upload


To obtain the original name of the uploaded File, you need to define a String type attribute. The attribute name must be *** FileName, and *** is the name of the File attribute. Similarly, to obtain the MIME type of the file, you must define a *** ContentType String attribute.

Upload a single file

Public class UploadAction extends ActionSupport {private File image; // private String imageFileName; // File name private String imageContentType; // File type public String execute () throws Exception {String realpath = ServletActionContext. getServletContext (). getRealPath ("/images"); FileOutputStream fos = null; FileInputStream FD = null; try {// create a file output stream System. out. println (getSavePath (); fos = new FileOutputStream (realpath + "\" + getImageFileName (); // create a file upload stream (FS = newFileInputStream (getImage ()); byte [] buffer = new byte [1024]; int len = 0; while (len = Fi. read (buffer)> 0) {fos. write (buffer, 0, len) ;}} catch (Exception e) {System. out. println ("File Upload Failed"); e. printStackTrace () ;}finally {close (fos, FS) ;}return SUCCESS;} public File getImage () {return image;} public void setImage (File image) {this. image = image;} public String getImageFileName () {return imageFileName;} public void setImageFileName (StringimageFileName) {this. imageFileName = imageFileName;} public String getImageContentType () {return imageContentType;} public void setImageContentType (StringimageContentType) {this. imageContentType = imageContentType ;}}


 

Upload multiple files

Import java. io. file; import java. io. IOException; import java. util. list; import org. apache. commons. io. fileUtils; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.ActionSupport; public class TagUploadListAction extends ActionSupport {private static final long serialVersionUID = 1L; private String name; // upload the set text private List of multiple files <File> upload; /// private List of multiple types of uploaded files <Stri Ng> uploadContextType; // a collection of file names for multiple uploaded files. private List <String> uploadFileName; public String getName () {return name;} public void setName (String name) {this. name = name;} public List <File> getUpload () {return upload;} public void setUpload (List <File> upload) {this. upload = upload;} public List <String> getUploadContextType () {return uploadContextType;} public voidsetUploadContextType (List <Strin G> uploadContextType) {this. uploadContextType = uploadContextType;} public List <String> getUploadFileName () {return uploadFileName;} public voidsetUploadFileName (List <String> uploadFileName) {this. uploadFileName = uploadFileName;} public String execute () {// put the uploaded file in the specified path String path = ServletActionContext. getServletContext (). getRealPath ("/WEB-INF/uploadList"); // write to the specified path File file = new Fi Le (path); // if the specified path does not exist, create if (! File. exists () {file. mkdirs () ;}// read the obtained object set cyclically and place it in the specified path for (int I = 0; I <upload. size (); I ++) {try {// list set get (I) to obtain the index FileUtils. copyFile (upload. get (I), new File (file, uploadFileName. get (I);} catch (IOException e) {// TODO Auto-generated catchblock e. printStackTrace () ;}return SUCCESS ;}}


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.