码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 单文件和多文件上传

时间:2019-12-09 14:16:09      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:append   trace   rest   bind   origin   try   dom   submit   alt   

单、多文件上传:单文件上传使用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("\r\n");
           
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return "上传失败!";
        }
    }
    
    return result.toString();
    
    }
    
}

 

SpringBoot 单文件和多文件上传

标签:append   trace   rest   bind   origin   try   dom   submit   alt   

原文地址:https://www.cnblogs.com/luffystory/p/12010409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!