【Java】Struts2文件上传-单文件上传,多文件上传

  • 单文件上传

表单:

    <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; // 表单中的name
    private File photo;  // 表单中的photo
    private String photoFileName; // 表单中文件的名字+FileName,如果文件属性名为myPhoto,则此处为myPhotoFileName
    private String photoContentType; // +ContentType

    public String execute() throws IOException {
        System.out.println(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; } }
  • 多文件上传

 

上传多个文件只需要将文件相关的属性,改为数组形式即可

表单:

<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>
    照片:<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 FileUploadNormal2 {
    private String name; // 表单中的name
    private File[] photo;  // 表单中的photo
    private String[] photoFileName; // 表单中文件的名字+FileName,如果文件属性名为myPhoto,则此处为myPhotoFileName
    private String[] photoContentType; // +ContentType

    public String 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;
    }
}

 

posted @ 2019-08-05 13:17  HanJunOvO  阅读(270)  评论(0编辑  收藏  举报