zoukankan      html  css  js  c++  java
  • 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();
        
        }
        
    }
  • 相关阅读:
    LeetCode(13) - Roman to Integer
    LeetCode(12) - Integer to Roman
    LeetCode(11) - Container With Most Water
    LeetCode(10) - Regular Expression Matching
    asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error
    DbEntry 4.2 建立关系时的一些问题
    Log4Net 日志文件分类保存
    JqGrid 隐藏水平滚动条完美解决方案
    WebSocket使用SuperWebSocket结合WindowsService实现实时消息
    LigerUI ligerComboBox 下拉框 表格 多选无效
  • 原文地址:https://www.cnblogs.com/luffystory/p/12010409.html
Copyright © 2011-2022 走看看