前言:项目可在文章末尾的百度网盘连接,自取。
前言:本项目基于ssm+maven,使用jdk1.8、tomcat8。
前言:目前项目只有上传文件这一个功能,实现将指定文件下载到指定位置。后续会扩展,比如将上传的.xls文件的数据读出并存到数据库。
需求实现-- 上传文件-- java编码&.xml配置
一、java编码
1、接口编码摘要
1.1、添加spingmvc注解,让一个java类能够处理请求
开发处理具体业务逻辑的Handler(@Controller、@Request)。
@Controller:定义类控制器类。
@RequestMapping:处理请求注解。
@Controller
@RequestMapping("/fileService")
public class FileController {}
也就是说,我们写的接口代码,是在Spring Web MVC工作流程的第五步和第六步,发挥作用的。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aYIRFXkY-1587343989908)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_140629.png)]](https://i-blog.csdnimg.cn/blog_migrate/41002f1ec61182cd6b1d30ac05526fb8.png)
在请求到达后,处理器适配器的作用就是找到请求相应的处理器。
标注了@RequestMapping的每一个方法都可以看成是一个Handler,也就是处理器。
Handler负责具体实际的请求处理。SpringMVC中Handler可以是任意形式,只要能处理请求即可。
1.2、接口的入参和出参
入参file是CommonsMultipartFile类型,并不是基本类型。需要使用@RequestParam注解进行手动映射。
(入参是基本类型的时候,直接声明形参即可。)
public Result uploadFile(@RequestParam(value = "file") CommonsMultipartFile file) {}
//若去掉@RequestParam(value = "file"),调接口会报500
出参是Result类型,有属性statusCode、message、data。
2、方法使用
2.1、File类的File(String parent, String child)构造方法
在JDK API 1.6.0中文版官方文档中,可以找到
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qgvM7Lvs-1587343989911)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_172152.png)]](https://i-blog.csdnimg.cn/blog_migrate/1f98ebdaee2a712c24450773d3bbafee.png)
大概意思就是,根据目录创建一个新File实例。
2.2、File类的mkdirs()方法
在JDK API 1.6.0中文版官方文档中,可以找到
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HFqjzwZw-1587343989914)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_171358.png)]](https://i-blog.csdnimg.cn/blog_migrate/6454fd292cd37efb8b9865774ea93e29.png)
大概意思就是,根据File实例的路径,在系统磁盘创建目录。
2.3、CommonsMultipartFile类的transferTo方法
在https://docs.spring.io/spring/docs/4.3.26.RELEASE/javadoc-api/官方文档里,可以找到
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vOl9L3JJ-1587343989918)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_153324.png)]](https://i-blog.csdnimg.cn/blog_migrate/9eeaaaa2a72be3b1ae3e8bf27b8c522e.png)
Transfer the received file to the given destination file,将接收到的文件传输到给定的目标文件。
执行transferTo()的语句后,CommonsMultipartFile类对象包含的文件就会传输到deat。
例如:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ijm3jKX0-1587343989923)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_173539_2.png)]](https://i-blog.csdnimg.cn/blog_migrate/d918c3fa68ccb18150adcfe6311310b7.png)
3、接口完整代码
备注:用到的工具类,可以在文章末尾的网盘资源里自取,或者改写。
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Result uploadFile(@RequestParam(value = "file") CommonsMultipartFile file) {
logger.info("---------------FileController: uploadFile---------------");
String statusCode = Constants.RESULT_MESSAGE_SUCCESS;
String message = "上传文件成功";
try {
//得到上传时的文件名
String uploadName = file.getOriginalFilename();
// 指定上传文件保存到的文件夹
String filePath = fileUrl + File.separator + CommUtil.getNowDateLongStr("yyyyMMdd");
//指定上传文件的名字
String fileName = "file_"+ CommUtil.getNowDateLongStr(CommUtil.pattern) + uploadName.substring(uploadName.indexOf("."), uploadName.length());
//创建File对象
File uploadFile = new File(filePath, fileName);
if (!uploadFile.exists()) {
//创建目录--给定的目标文件
uploadFile.mkdirs();
}
//将接收到的文件传输到给定的目标文件
file.transferTo(uploadFile);
} catch (Exception e) {
logger.error("上传文件异常", e);
statusCode = Constants.RESULT_MESSAGE_ERROR;
message = "文件上传失败";
}
return new Result(statusCode, message);
}
二、spingmvc的配置
1、常规配置
用postman测接口,不需要配置视图解析器,下面这两个配置就够了。
<!-- 自动注册最合适的处理器映射器、处理器适配器 -->
<mvc:annotation-driven />
<!-- 自动扫描,完成bean创建和依赖注入 -->
<context:component-scan base-package="com.sharylala">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
2、配置文件上传解析器
<!-- 配置文件上传解析器,id是固定的multipartResovler -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
三、applicationContext的配置
常规配置就行
<!--配置spring包扫描-->
<context:component-scan base-package="com.sharylala"/>
<!--将配置文件中的信息,通过spring加载到容器进行使用-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
四、web.xml的配置
常规配置就行
<!--spring框架启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc启动-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--声明应用范围(整个WEB项目)内的上下文初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/applicationContext.xml</param-value>
</context-param>
<!--在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>-->
五、测试
1、run sharylala
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X7CaMQrv-1587343989925)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_191920.png)]](https://i-blog.csdnimg.cn/blog_migrate/cbe427f9d14d5ebf43c222db40db40ec.png)
2、postman测接口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wHdoRne3-1587343989927)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_191802.png)]](https://i-blog.csdnimg.cn/blog_migrate/0aa85f3836e66c526663c1dd35cfaabc.png)
3、查看结果和tomcat日志
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l2oNpqyK-1587343989929)(F:\优快云博客草稿\pic\功能实现\1-上传文件\2020-04-19_192057.png)]](https://i-blog.csdnimg.cn/blog_migrate/18ead37061d51f36c4f6cb30cd986ee9.png)
完整项目自取
链接:https://pan.baidu.com/s/1CCBsfukJgbD1BuyF8Rc6vg
提取码:hw5s
本文介绍了如何在基于SSM的项目中实现文件上传功能,详细讲解了Java编码和Spring MVC配置过程。通过添加Spring MVC注解、使用CommonsMultipartFile处理文件、配置文件上传解析器等步骤,实现了将文件从客户端上传并保存到服务器的逻辑。同时,文章提供了项目的完整代码和测试方法。
355

被折叠的 条评论
为什么被折叠?



