[Java] Struts2 file upload - single file upload, multiple file uploads

  • Single file upload

Form:

    <form action="upload1.action" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="name" id="name"><br>
        照片:<input type="file" name="photo"> <br>
        <input type="submit" value="提交">
    </form>

action:

Package com.hj.action; 

Import org.apache.commons.io.FileUtils; 

Import java.io.File;
 Import java.io.IOException; 

public  class FileUploadNormal1 {
     Private String name; // in a form name 
    Private File Photo;   // Photo form of 
    Private String photoFileName; // name of the form file + FileName, if the file property called myPhoto, then here myPhotoFileName 
    Private String photoContentType; // + ContentType 

    public String the Execute () throws IOException { 
        System. out.println ( the this.photoFileName);
        System.out.println(this.photoContentType);
        File destFile = new File("C:\\File_rec\\tmp\\"+photoFileName); // 上传到的路径
     // File destFile = new File(ServletActionContext.getServletContext().getRealPath("/upload/"+photoFileName)); //项目路径 FileUtils.copyFile(photo,destFile);
return "success"; } // getter,setter public String getName() { return name; } public void setName(String name) { this.name = name; } public File getPhoto() { return photo; } public void setPhoto(File photo) { this.photo = photo; } public String getPhotoFileName() { return photoFileName; } public void setPhotoFileName(String photoFileName) { this.photoFileName = photoFileName; } public String getPhotoContentType() { return photoContentType; } public void setPhotoContentType(String photoContentType) { this.photoContentType = photoContentType; } }
  • Multiple file uploads

 

Upload multiple files only need to file related properties, an array can be changed

Form:

<form action="upload2.action" method="post" enctype="multipart/form-data">
    姓名:<input type="text" name="name" id="name"><br>
    照片:<input type="file" name="photo"> <br>
    照片:<input type="file" name="photo"> <br>of the typethe INPUT<
    Photo:="file" name="photo"> <br>
    <input type="submit" value="提交">
</form>

Multi-file upload action:

Package com.hj.action; 

Import org.apache.commons.io.FileUtils; 

Import java.io.File;
 Import java.io.IOException; 

public  class FileUploadNormal2 {
     Private String name; // name of the form 
    Private File [] Photo;   // Photo form of 
    Private String [] photoFileName; // name of the form file + FileName, if the file property called myPhoto, then here myPhotoFileName 
    Private String [] photoContentType; // + ContentType 

    public String the Execute ( ) throws IOException {
         for ( int i = 0; i < photo.length; i++) {
            System.out.println(this.photoFileName[i]);
            System.out.println(this.photoContentType[i]);
            File destFile = new File("C:\\File_rec\\tmp\\"+photoFileName[i]);
            FileUtils.copyFile(photo[i],destFile);
        }

        return "success";
    }
    
    // getter,setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public File[] getPhoto() {
        return photo;
    }

    public void setPhoto(File[] photo) {
        this.photo = photo;
    }

    public String[] getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String[] photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String[] getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String[] photoContentType) {
        this.photoContentType = photoContentType;
    }
}

 

Guess you like

Origin www.cnblogs.com/to-red/p/11302445.html