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

SpringBoot学习6:springboot文件上传

时间:2019-02-06 15:48:28      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:max   ash   pack   static   stc   utf-8   src   ram   not   

1、编写页面uploadFile.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
</head>
<body>
<form action="uploadFile" method="post" enctype="multipart/form-data">
    <input type="file" name="myfile">
    <input type="submit" value="上传">
</form>
</body>
</html>

2、编写controller

package com.bjsxt.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/5.
 */
@RestController
public class UploadFileController {

    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    public Map<String,Object> uploadFile(MultipartFile myfile){
        Map<String,Object> returnMap=new HashMap<String,Object>();
        try{
            myfile.transferTo(new File("e:/"+myfile.getOriginalFilename()));
            returnMap.put("msg","上传成功");
        }catch (Exception e){
            e.printStackTrace();
            returnMap.put("msg","上传失败");
        }
        return returnMap;
    }

}

3、编写启动类

package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Administrator on 2019/2/5.
 */
@SpringBootApplication
public class App {

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

4、设置上传文件的大小限制

  需要添加一个springboot的配置文件,名字为application.properties,放在resource文件夹下,添加以下内容

#设置单个文件上传的最大大小
spring.http.multipart.maxFileSize=200MB
#设置一次请求上传文件的总容量的大小
spring.http.multipart.maxRequestSize=200MB

5、启动项目即可,在浏览器中访问http://localhost:8080/uploadFile.html即可

目录结构

技术图片

 

  

SpringBoot学习6:springboot文件上传

标签:max   ash   pack   static   stc   utf-8   src   ram   not   

原文地址:https://www.cnblogs.com/duanrantao/p/10353726.html

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