SpringMVC-02上传文件

一、需求:

利用SpringMVC实现上传文件的功能

二、思路:

1.我们可以在SpringMVC中,通过配置一个MultipartResolver来上传文件。

2.通过MultipartFile file来接收文件,通过MultipartFile[] files接收多个文件上传。

三、参考

1.SpringMVC学习:https://how2j.cn/k/springmvc/springmvc-springmvc/615.html?p=36286

2.PDF书籍:关注公众号:ElevenKeep,回复springboot即可获得。

四、步骤

1.添加pom文件
<!-- 添加上传文件依赖 -->
		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- 非必须,可简化I/O操作 -->
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.3</version>
		</dependency>
2.JSP页面

上传页面,需要在src/main/resources/views下面新建一个upload.jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>upload page</title>
</head>
<body>
	<div class="upload">
		<form action="upload" enctype="multipart/form-data" method="post">
			<input type="file" name="file" /><br> 
			<input type="submit" value="上传">
		</form>
	</div>
</body>
</html>
3.跳转页面

添加能跳转到upload页面的ViewController。需要在MyMvcConfig.java下面添加。

// 添加转向upload页面的ViewController
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/index").setViewName("/index");
		registry.addViewController("/toUpdate").setViewName("/upload");
	}
4.MultipartResolver配置

同样,在MyMvcConfig.java下面添加。

// MultipartResolver配置
	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setMaxUploadSize(1000000);
		return multipartResolver;
	}
5.控制器(控制层)
package com.eleven.controller;

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

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller	// 在@controller注解中,返回的是字符串,或者是字符串匹配的模板名称,即直接渲染视图,与html页面配合使用的
public class UploadController {

	// @RequestMapping:是用来处理地址映射的注解
	// value:指定请求的实际地址
	// method:指定请求的方法类型,有get、post、put、delete
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	
	// 使用MultipartFile file接收上传的文件
	// @ResponseBody:将java对象转为json格式的数据。
	public @ResponseBody String upload(MultipartFile file) {	
		try {
			// 使用FileUtils.writeByteArrayToFile快速写文件到磁盘
			FileUtils.writeByteArrayToFile(new File("D:upload/" + file.getOriginalFilename()), file.getBytes());
			return "ok";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "wrong";
		}
	}

}

6.运行

访问路径: http://localhost:8080/springmvc/upload 即可。

7.效果
在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值