• SpringBoot 单文件和多文件上传


    单、多文件上传:单文件上传使用upload.html ,多文件上传使用uploads.html

    创建一个Springboot application, POM 中加入 spring-boot-starter-web 依赖

    upload.html

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

    uploads.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    
    <body>
    
        <form action="uploads" method="post" enctype="multipart/form-data">
            <input type="file" name="uploadFiles" value="请选择文件" multiple /> <input
                type="submit" value="上传" />
        </form>
    
    </body>
    </html>

    创建Controller 用于处理提交之后的Aciton

    package com.app;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    @RestController
    public class FileUploadController {
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        
        @PostMapping("/upload")
        public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        System.out.println(folder.getAbsolutePath());
        if(!folder.isDirectory()) {
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        System.out.println("oldName" + oldName);
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        System.out.println("new Name" + newName);
        try {
            uploadFile.transferTo(new File(folder,newName));
            String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format  + newName;
            
            return filePath;
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        
        return "上传失败!";
        }
        @PostMapping("/uploads")
        public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) {
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFiles/");
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        System.out.println(folder.getAbsolutePath());
        if(!folder.isDirectory()) {
            folder.mkdirs();
        }
        StringBuffer result = new StringBuffer();
        for(MultipartFile uploadFile: uploadFiles) {
            String oldName = uploadFile.getOriginalFilename();
            System.out.println("oldName" + oldName);
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
            System.out.println("new Name" + newName);
            try {
                uploadFile.transferTo(new File(folder,newName));
                String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFiles/" + format  + newName;
                result.append(filePath);
                result.append("
    ");
               
            } catch (IllegalStateException | IOException e) {
                e.printStackTrace();
                return "上传失败!";
            }
        }
        
        return result.toString();
        
        }
        
    }
  • 相关阅读:
    [Matlab] subplot 紧凑,减少空白
    论文
    统计学习理论
    位操作
    Pattern Recognition and Machine Learning
    HDU6862 Hexagon (2020HDU 多校 D8 H)
    VS2008 : error PRJ0002 : 错误的结果 1 (从“d:/Program Files/Microsoft Visual Studio 9.0/VC/bin/cl.exe”返回)
    static简单实例说明其用法(转)
    ArcGIS Engine开发系列:将地图导出为图片的两种方法
    按类别列出的编译器选项
  • 原文地址:https://www.cnblogs.com/luffystory/p/12010409.html
Copyright © 2020-2023  润新知