springboot中文件上传的controller与配置的文件上传的最大容量

2023-09-18

package com.hh.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-18 16:28:24
 */
@Slf4j
@Controller
public class FormController {

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


    @PostMapping("/upload")
    public String upload(@RequestParam("username")String username,
                       @RequestParam("email")String email,
                       @RequestPart("headerImg")MultipartFile headerImg,
                       @RequestPart("photos")MultipartFile[] photos) throws IOException {
        log.info("username{},email{},headerImg{},photos{}",
                username,email,headerImg,photos.length);

        if(!headerImg.isEmpty()){
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("E:\\下载\\cache\\"+originalFilename));
        }

        if(photos.length>0){
            for (MultipartFile photo : photos) {
                if(!photo.isEmpty()){
                    String originalName = photo.getOriginalFilename();
                    photo.transferTo(new File("E:\\下载\\cache\\"+originalName));
                }
            }
        }

        return "main";

    }
}

application.properties

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB

form_validation.html

<form role="form" class="form-horizontal adminex-form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
    <div class="form-group has-success">
        <label class="col-lg-2 control-label">用户名</label>
        <div class="col-lg-10">
            <input type="text" placeholder="" name="username" id="f-name" class="form-control">
            <p class="help-block">Successfully done</p>
        </div>
    </div>
    <div class="form-group has-warning">
        <label class="col-lg-2 control-label">邮箱</label>
        <div class="col-lg-10">
            <input type="email" placeholder="" name="email" id="email2" class="form-control">
        </div>
    </div>

    <div class="form-group has-warning">
        <label class="col-lg-2 control-label">头像</label>
        <div class="col-lg-10">
            <input type="file" placeholder="" name="headerImg" id="headerImg" class="form-control">
        </div>
    </div>

    <div class="form-group has-warning">
        <label class="col-lg-2 control-label">生活照</label>
        <div class="col-lg-10">
            <input type="file" placeholder="" name="photos" multiple id="photos" class="form-control">
        </div>
    </div>

    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button class="btn btn-primary" type="submit">提交</button>
        </div>
    </div>
</form>

 

posted @ 2023-09-18 16:53  努力是一种常态  阅读(84)  评论(0编辑  收藏  举报