SpringBoot之Springmvc的文件上传

5 篇文章 0 订阅

SpringBoot之Springmvc的文件上传

1.pom文件的配置

 <parent>
   		 <!-- springBoot的父依赖 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
   <dependencies>
       <!-- springBoot的启动器 -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
   </dependencies>
   <properties>
   		<!-- 配置jdk1.8 -->
   		<java.version>1.8</java.version>
   </properties>

### 2.springboot启动类

```java
package com.linyuan.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;

@EnableAutoConfiguration
@ComponentScan({"com.linyuan.test"})
@ServletComponentScan
public class App {
	
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

项目结构如图
在这里插入图片描述

3.FileUploadController

package com.linyuan.test.controller.fileupload;

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

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

@RestController
public class FileUploadController {
	/**
	 * @param file 改参数必须是MultipartFile,参数名必须和前端表单的name一致,springMvc会自动进行文件的封装
	 * @return
	 */
	@RequestMapping("upload")
	public Map<String, String> upload(MultipartFile file) {
		// 源文件名
		System.out.println(file.getOriginalFilename());
		// 文件大小
		System.out.println(file.getSize());
		// 保存的文件路径,该路径是Linux系统下的
		File file2 = new File("/media/lin/机械/开发工具/上传文件/"+file.getOriginalFilename());
		Map<String, String> result= new HashMap<String, String>();
		try {
			// 保存文件
			file.transferTo(file2);
			result.put("msg", "上传成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("msg", "上传失败");
		} 
		return result;
	}
}

如图
在这里插入图片描述

4.前端页面编写

表单需要以post方式提交,并且enctype=“multipart/form-data”,其中上传文件的name必须和controller中的参数名称一致。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
	<form action="/upload" method="post" enctype="multipart/form-data">
		<input type="file" name="file">
		<button type="submit">提交</button>
	</form>
</body>
</html>

如图
在这里插入图片描述

5.Application.properties

springmvc默认支持上传的文件大小比较小,所以需要配置允许上传的文件大小。

spring.http.multipart.maxFileSize=200MB
spring.http.multipart.maxRequestSize=200MB

如图
在这里插入图片描述

6.注意事项

图片中存在一些servlet和一些过滤器的类,请忽略。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值