spring文件上传的几种方式

文件上传 传统方式
借助第三方组件实现文件上传:
UserController.java编写:

文件上传 传统方式

前端表单index.jsp代码:
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>

Insert title here

文件上传

传统方法上传文件 选择文件,
springmvc上传文件 选择文件,

运行截图:

借助第三方组件实现文件上传:

导入 写入配置pom.xml
UserController.java编写:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/file")
public String fileUpLoad(HttpServletRequest request) throws Exception {
System.out.println(“文件上传”);
//文件上传位置
String path = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(path);

if (!file.exists()) {
// 判断文件存在否,创建该文件夹
file.mkdirs();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//解析request
List<FileItem> items =upload.parseRequest(request);
//遍历
for (FileItem item : items) {
// 判断当前对象item是否是上传文件项
if (item.isFormField()) {
// true 说明为普通表单项
} else {
// 说明上传文件项
// 获取上传文件名称
String filename = item.getName();
try {
// 完成文件上传
item.write(new File(path, filename));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 删除临时文件
item.delete();
}
}
return "success";
}

}

springmvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:p=“http://www.springframework.org/schema/p” xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation=“http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd”

<!--开启注解扫描-->
<context:component-scan base-package="com.tao.*"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!--开启springmvc框架注解的支持-->
<mvc:annotation-driven/>
<!-- 配置 文件上传的支持 -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1024000000"/>
<property name="resolveLazily" value="true"/>
<property name="maxInMemorySize" value="4096"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<context:annotation-config></context:annotation-config>
</beans>

controller02.java
@Controller
@RequestMapping("/user")
public class Controller02 {
@RequestMapping("/load2")
public String fileUpload(HttpServletRequest request,MultipartFile upload)throws Exception {
System.out.println(“文件上传”);
String path=request.getSession().getServletContext().getRealPath("/upload/");

File file=new File(path);
//判断路径是否存在
if (!path.isEmpty()) {
file.mkdir();
}
//上传文件唯一名修改
String filename=upload.getOriginalFilename();
String uuid=UUID.randomUUID().toString().replace("-", "");
filename=uuid+"_"+filename;
//文件保存
upload.transferTo(new File(path,filename));
return "success";

}
}

springmvc上传文件原理图示:
[详情有道笔记]

posted @   taotooler  阅读(179)  评论(0编辑  收藏  举报  
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示