zoukankan      html  css  js  c++  java
  • 【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;
        }
    }
  • 相关阅读:
    Study Plan The TwentySecond Day
    Study Plan The Nineteenth Day
    Study Plan The TwentySeventh Day
    Study Plan The Twentieth Day
    Study Plan The TwentyFirst Day
    python实现进程的三种方式及其区别
    yum makecache
    JSONPath 表达式的使用
    oracle执行cmd的实现方法
    php daodb插入、更新与删除数据
  • 原文地址:https://www.cnblogs.com/to-red/p/11302445.html
Copyright © 2011-2022 走看看