• 【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;
        }
    }
  • 相关阅读:
    解决安装Visual Studio 2012后SQL Server 2008 远程过程调用失败的问题
    解决 Visual Studio 2012 有时不能调试的问题
    WPF实现窗体最小化后小图标在右边任务栏下
    WinForm实现窗体最小化后小图标在右边任务栏下
    C# 开机启动代码
    C# ?? 操作符示例
    WPS页面设置
    PCA(主成分分析)和LDA详解
    MySQL命令行导入sql文件时出现乱码解决方案
    IKAnalyzer 独立使用 配置扩展词典
  • 原文地址:https://www.cnblogs.com/to-red/p/11302445.html
Copyright © 2020-2023  润新知