码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-08-05 13:44:02      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:pat   except   out   tmp   etc   action   数组   system   路径   

  • 单文件上传

表单:

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

 

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

标签:pat   except   out   tmp   etc   action   数组   system   路径   

原文地址:https://www.cnblogs.com/to-red/p/11302445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!