spring boot文件上传

spring boot文件上传

单文件上传

1.templates目录下新建file.html上传文件的页面

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 inlines="text">文件上传</h1>
    <form action="/fileUpload" method="post" enctype="multipart/form-data">
        <p>选择文件: <input type="file" name="fileName"/></p>
        <p><input type="submit" value="提交"/></p>
    </form>
</body>
</html>

2.新建FileController类,控制器代码

package demo.demospringmvc.controller;

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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class FileController {

/*获取file.html页面*/
    @GetMapping("/file")
    public String file(){
        return "file";
    }


/*实现上传单个文件*/
    @PostMapping("/fileUpload")
    @ResponseBody
 	//public String fileUpload(HttpServletRequest request){
    public String fileUpload(@RequestParam("fileName") MultipartFile file){
   
        //MultipartFile file = ((MultipartHttpServletRequest)request).getFile("fileName");
        if(file.isEmpty()){
            return "false";
        }

        String filename = file.getOriginalFilename();
        System.out.println(filename + "-------->"+ file.getSize());

        String path = "D:/files";
        File dest = new File(path+"/"+filename);

        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }

        try {
            file.transferTo(dest);
        } catch (IOException e) {
            return "false";
        }
        return "true";
    }

    
}

spring boot默认上传文件最大为2M.

解决:添加文件配置bean
1.直接在启动类中添加

@SpringBootApplication
public class DemospringmvcApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemospringmvcApplication.class, args);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
/*设置单文件上传最大*/
        factory.setMaxFileSize("100MB");
/*设置上传总数据最大*/
        //factory.setMaxRequestSize(maxRequestSize);
        return factory.createMultipartConfig();
    }
}

2.新建java配置类添加bean

/*config.java*/
@Configuration
public class config {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("100MB");
        //factory.setMaxRequestSize(maxRequestSize);
        return factory.createMultipartConfig();
    }
}

多文件上传

类似:1.templates目录下新建multifile.html多上传文件的页面

<!DOCTYPE html>
<html >
<head>
    <meta charset="UTF-8" />
    <title>Insert title here</title>
</head>
<body>
<h1>文件上传</h1>
<form action="/multifileUpload" method="post" enctype="multipart/form-data" >
    <p>选择文件1: <input type="file" name="fileName"/></p>
    <p>选择文件2: <input type="file" name="fileName"/></p>
    <p>选择文件3: <input type="file" name="fileName"/></p>
    <p><input type="submit" value="提交"/></p>
</form>
</body>
</html>

2.controller中添加方法映射

package demo.demospringmvc.controller;

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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class FileController {

	/*.
		.
		.
		.
		.

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

    @PostMapping("/multifileUpload")
    @ResponseBody
    public  String multifileUpload(@RequestParam("fileName") List<MultipartFile> files){
        if(files.isEmpty()){
            return  "false";
        }

        String path = "D:/files";

        for (MultipartFile file:files ) {
            String filename = file.getOriginalFilename();
            System.out.println(filename + "-------->"+ file.getSize());

            if(file.isEmpty()){
                return "false";
            }else{
                File dest = new File(path+"/"+filename);

                if(!dest.getParentFile().exists()){
                    dest.getParentFile().mkdir();
                }

                try {
                    file.transferTo(dest);
                } catch (IOException e) {
                    return "false";
                }
            }
        }
        return "true";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值