zoukankan      html  css  js  c++  java
  • SpringBoot2 上传文件 上传多文件

    项目结构:

    1、单文件上传

    upload.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>upload file</title>
    </head>
    <body>
        <form action="/uploadfile/upload" method="post" enctype="multipart/form-data">
            <input type="file" name="file">
            <input type="submit" value="上传">
        </form>
    </body>
    </html>
    

    http://localhost:8080/uploadfile/upload

    package com.archibladwitwicke.springboot2.chapter03.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    
    @Controller
    @RequestMapping("/uploadfile")
    public class UploadFileController {
    
        @RequestMapping("/upload")
        public String upload() {
            return "/upload.html";
        }
    
        @PostMapping("/upload")
        @ResponseBody
        public String upload(MultipartFile file) {
            if (file.isEmpty()) {
                return "file is null";
            }
    
            String originFileName = file.getOriginalFilename();
            String destFileLocation = "C:\Users\Administrator\Desktop\" + originFileName;
            File destFile = new File(destFileLocation);
            try {
                file.transferTo(destFile);
                return "upload ok! upload file location: " + destFileLocation;
            } catch (Exception ex) {
                return "upload false! reason: " + ex.getMessage();
            }
        }
    
        @RequestMapping("/uploads")
        public String uploads() {
            return "/multiupload.html";
        }
    }
    

    2、多文件上传

    multiupload.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>upload file</title>
    </head>
    <body>
        <form action="/uploadfile/uploads" method="post" enctype="multipart/form-data">
            <input type="file" name="files"><br/>
            <input type="file" name="files"><br/>
            <input type="file" name="files"><br/>
            <input type="submit" value="上传">
        </form>
    </body>
    </html>
    

    http://localhost:8080/uploadfile/uploads

    package com.archibladwitwicke.springboot2.chapter03.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    
    @Controller
    @RequestMapping("/uploadfile")
    public class UploadFileController {
    
        @RequestMapping("/uploads")
        public String uploads() {
            return "/multiupload.html";
        }
    
        @PostMapping("/uploads")
        @ResponseBody
        public String uploads(MultipartFile[] files) {
    
            String result = null;
    
            if (files.length == 0) {
                return "file is null";
            }
    
            for (MultipartFile file : files) {
                String originFileName = file.getOriginalFilename();
                String destFileLocation = "C:\Users\Administrator\Desktop\" + originFileName;
                File destFile = new File(destFileLocation);
                try {
                    file.transferTo(destFile);
                    result += "upload ok! upload file location: " + destFileLocation;
                } catch (Exception ex) {
                    result = "upload false! reason: " + ex.getMessage();
                }
            }
    
            return result;
        }
    }
    

  • 相关阅读:
    OPENSSL库使用--AES篇
    Linux inotify功能及实现原理
    LSI RAID
    Linux下关于热插拔硬盘的指令
    最长回文字串理解(学习Manacher's algorithm)
    pat 1068 动态规划/Fina More Conis
    (二)Myeclipse中关于jdk配置,解决版本不一致问题
    (一)MyEclipse配置Tomcat,与jsp程序运行
    pat 1047 解题心得
    pat 1038 Smallest Number解题心得
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/8583090.html
Copyright © 2011-2022 走看看