springboot 接收POST请求,并上传文件

新建工程,只需要引入 web 和 thymeleaf 两个jar就行。

参考:https://blog.csdn.net/nece001/article/details/106301721

 

只需要建4个文件(用的IDE是Netbeans)

先建三个HTML文件,用作模板:

失败页面:

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>上传失败</div>
    </body>
</html>

成功页面:

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>上传成功</div>
        文件标题:[[${title}]]
    </body>
</html>

文件上传页:

<!DOCTYPE html>
<html>
    <head>
        <title>Upload Form</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="/save">
            单个上传
            <div>标题:<input type="text" name="title"></div>
            <div>选择:<input type="file" name="upfile"></div>
            <input type="submit" value="上传">
        </form>
        
        
        <form method="post" enctype="multipart/form-data" action="/save-all">
            上传多个文件
            <div>标题:<input type="text" name="title"></div>
            <div>选择1:<input type="file" name="upfiles"></div>
            <div>选择2:<input type="file" name="upfiles"></div>
            <div>选择3:<input type="file" name="upfiles"></div>
            <input type="submit" value="上传">
        </form>
    </body>
</html>

控制器类:

package com.example.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

    String base = "D:\\test\\java\\1\\upload\\"; // 不要使用项目内的目录

    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }

    @PostMapping("/save")
    public String save(MultipartFile upfile, String title, Map<String, Object> map) throws IOException {

        map.put("title", title);
        if (upfile.getSize() > 0) {
            String origName = upfile.getOriginalFilename();
            String saveName = UUID.randomUUID() + origName;
            File file = new File(base + saveName);
            upfile.transferTo(file);
            
            return "success";
        }
        return "failure";
    }

    @PostMapping("/save-all")
    public String saveAll(MultipartFile[] upfiles, String title, Map<String, Object> map) throws IOException {

        map.put("title", title);
        int saveCount = 0;
        if (upfiles.length > 0) {
            MultipartFile upfile;
            for (int i = 0; i < upfiles.length; i++) {
                upfile = upfiles[i];

                if (upfile.getSize() > 0) {
                    saveCount++;
                    String origName = upfile.getOriginalFilename();
                    String saveName = UUID.randomUUID() + origName;
                    File file = new File(base + saveName);
                    upfile.transferTo(file);
                }
            }
        }

        return saveCount > 0 ? "success" : "failure";
    }
}

增加图片缩略功能:

引入jar包

<dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
  <version>0.4.11</version>
</dependency>

控制器添加代码:

package com.example.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadController {

    String base = "D:\\test\\java\\1\\upload\\";

    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }

    @PostMapping("/save")
    public String save(MultipartFile upfile, String title, Map<String, Object> map) throws IOException {

        map.put("title", title);
        if (upfile.getSize() > 0) {
            String origName = upfile.getOriginalFilename();
            String saveName = UUID.randomUUID() + origName;
            File file = new File(base + saveName);
            upfile.transferTo(file);
            
            File thumb = new File(base+"thumb_"+origName);
            Thumbnails.of(file)
                .size(200,200)
                .outputQuality(0.8f)
                .toFile(thumb);
            
            return "success";
        }
        return "failure";
    }

    @PostMapping("/save-all")
    public String saveAll(MultipartFile[] upfiles, String title, Map<String, Object> map) throws IOException {

        map.put("title", title);
        int saveCount = 0;
        if (upfiles.length > 0) {
            MultipartFile upfile;
            for (int i = 0; i < upfiles.length; i++) {
                upfile = upfiles[i];

                if (upfile.getSize() > 0) {
                    saveCount++;
                    String origName = upfile.getOriginalFilename();
                    String saveName = UUID.randomUUID() + origName;
                    File file = new File(base + saveName);
                    upfile.transferTo(file);
                }
            }
        }

        return saveCount > 0 ? "success" : "failure";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值