SpringBoot上传文件

  • Post author:
  • Post category:其他

yml文件配置

spring:
  servlet:
    multipart:
      max-file-size: 100MB  #单个文件大小
      max-request-size: 1024MB #总文件大小

代码

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Locale;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class UploadController {

    /**
     * 上传方式一,从请求体中获取文件信息
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("method1")
    public String upload1(HttpServletRequest request) throws IOException {

        MultipartFile file = ((StandardMultipartHttpServletRequest) request).getFile("file");
        String fileName = file.getOriginalFilename();
        String newName=UUID.randomUUID().toString()+fileName.substring(fileName.indexOf("."));

        // 获取当前操作系统
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";

        if(osName.startsWith("win")){
            path="D:\\Test\\";
        }else{
            path="/opt/test";
        }
        File saveFile=new File(path+newName);
        if(!saveFile.getParentFile().exists()){
            saveFile.getParentFile().mkdirs();
        }
        file.transferTo(saveFile);
        return saveFile.getPath();
    }

    /**
     * 上传方式2 MultipartFile 上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("method2")
    public String upload2(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String newName=UUID.randomUUID().toString()+fileName.substring(fileName.indexOf("."));
        // 获取当前操作系统
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";

        if(osName.startsWith("win")){
            path="D:\\Test\\";
        }else{
            path="/opt/test";
        }
        File saveFile=new File(path+newName);
        if(!saveFile.getParentFile().exists()){
            saveFile.getParentFile().mkdirs();
        }
        file.transferTo(saveFile);
        return saveFile.getPath();
    }

    /**
     * base64 图片上传
     * @param base64
     * @return
     */
    @PostMapping("method3")
    public String upload3(@RequestParam("base64")String base64){
        String base64Data = base64.split(",")[1];
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(base64Data);
        // 获取当前操作系统
        String osName = System.getProperties().get("os.name").toString().toLowerCase(Locale.ROOT);
        String path="";

        if(osName.startsWith("win")){
            path="D:\\Test\\";
        }else{
            path="/opt/test";
        }
        String newName=UUID.randomUUID().toString()+".png";
        File saveFile=new File(path+newName);
        if(!saveFile.getParentFile().exists()){
            saveFile.getParentFile().mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(saveFile);
            fos.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return saveFile.getPath();
    }
}


版权声明:本文为weixin_45816407原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。