springmvc单文件上传
Posted Jason
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc单文件上传相关的知识,希望对你有一定的参考价值。
1、方式一
1.1、新建控制层
package com.xjh.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
/**
* @author xjh
* @description:
* @date 2021-05-16
*/
@RestController
@RequestMapping("/file")
public class FileUpload {
@RequestMapping("/uploadFile")
public String uploadFile(@RequestParam MultipartFile file, HttpServletRequest request) {
String name = file.getOriginalFilename();
//String baseDir = "E:\\\\upload\\\\";
String baseDir = request.getSession().getServletContext().getRealPath("upload");
File f = new File(baseDir, name);
if (!f.getParentFile().exists()) {
if (!f.getParentFile().mkdir()) {
throw new RuntimeException("目录:" + baseDir + "创建失败!");
}
}
try {
file.transferTo(new File(baseDir + File.separator + name));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
}
1.2、配置springmvc的配置文件xml
<!--声明上传请求,需引入commons-fileupload依赖-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--可上传文件的最大值,单位:字节-->
<property name="maxUploadSize" value="5120000" />
</bean>
另外为了解决上传单行文本域有乱码问题,所以还需要再web.xml中声明编码过滤器,代码如下:
<!--字符编码过滤-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3、jsp页面
<form action="file/uploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
小结:<input type="file" name="file"/>的name要与参数@RequestParam MultipartFile file名一样,如果注解指定值@RequestParam("fname")则,name要与fname一样。
2、方式二
2.1、新建控制层
@RequestMapping("/uploadFile")
public String uploadFile(MultipartHttpServletRequest request) {
MultipartFile file = request.getFile("file");
String name = file.getOriginalFilename();
String baseDir = "E:\\\\upload\\\\";
//String baseDir = request.getSession().getServletContext().getRealPath("upload");
File f = new File(baseDir, name);
if (!f.getParentFile().exists()) {
if (!f.getParentFile().mkdir()) {
throw new RuntimeException("目录:" + baseDir + "创建失败!");
}
}
try {
file.transferTo(new File(baseDir + File.separator + name));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
2.2、同1.2
2.3、同1.3
上传文件相关依赖
<!--上传组件包-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--IO流工具类包-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
以上是关于springmvc单文件上传的主要内容,如果未能解决你的问题,请参考以下文章