当前位置: 首页 > news >正文

springmvc实现文件上传

文件上传的准备

导入文件上传的jar包

 <dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>

编写文件上传的JSP页面

 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>文件上传</title>
</head>
<body>
​<h3>文件上传</h3>
​<form action="/fileupload.do" method="post" enctype="multipart/form-data">选择文件:<input type="file" name="upload" /><br/><input type="submit" value="上传" /></form>
​
</body>
</html>

springmvc传统方式文件上传

配置文件解析器对象 springmvc.xml

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
​<!--如果<url-pattern>/</url-pattern>,任何资源都会拦截--><!--配置哪些资源不被拦截<mvc:resources mapping="/js/" location="/js/**" /><mvc:resources mapping="/css/" location="/css/**" /><mvc:resources mapping="/image/" location="/image/**" />-->
​<!--配置了内容,启动Tomcat服务器的时候,就会被加载--><!--配置注解扫描--><context:component-scan base-package="com.qcbyjy" />
​<!--配置视图解析器,进行页面的跳转--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--跳转的页面的路径--><property name="prefix" value="/pages/" /><!--跳转页面的后缀名称--><property name="suffix" value=".jsp" /></bean>
​<!--配置文件上传的解析器组件。id的名称是固定,不能乱写--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--设置上传文件的总大小 8M = 8 * 1024 * 1024 --><property name="maxUploadSize" value="8388608" /></bean>
​<!--让映射器、适配器和处理器生效(默认不配置也是可以的)--><mvc:annotation-driven/>
​
</beans>

springmvc框架提供了MultipartFile对象,该对象表示上传的文件,要求变量名称必须和表单file标签的name属性名称相同。

代码如下:

 
package cn.tx.demo2;
​
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
​
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
​
/**** 老师* 文件上传*/
@Controller
public class UploadController {
​/*** 文件上传** MultipartFile upload 文件上传解析器对象解析request后,文件上传对象** @return*/@RequestMapping("/fileupload.do")public String upload(MultipartFile upload, HttpServletRequest request) throws IOException {// 把文件上传到哪个位置String realPath = request.getSession().getServletContext().getRealPath("/uploads");// 创建该文件夹File file = new File(realPath);// 判断该文件夹是否存在if(!file.exists()){// 创建文件夹file.mkdirs();}
​// 获取到上传文件的名称String filename = upload.getOriginalFilename();
​// 把文件的名称修改成为一的值 sdfs-csdf-fwer-sdfwString uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();// 唯一的值filename = uuid+"_"+filename;System.out.println("文件名称:"+filename);
​// 上传文件upload.transferTo(new File(file,filename));
​return "suc";}
​
}
​

相关文章:

  • 为什么消息队列系统不像数据库系统那样可以配置读写分离?
  • 【css】css统一设置变量
  • 03 mysql 连接
  • Linux 内核中的 security_sk_free:安全模块与 Socket 释放机制解析
  • 掌握单元测试:提升软件质量的关键步骤
  • MySQL + Elasticsearch:为什么要使用ES,使用场景与架构设计详解
  • idea spring boot 打包成可执行的 JAR包
  • SpringBoot默认并发处理(Tomcat)、项目限流详解
  • .NET高频技术点(持续更新中)
  • 软件确认报告:审查功能、评估标准及推动软件稳定高效运行
  • 【图片识别内容改名】图片指定区域OCR识别并自动重命名,批量提取图片指定内容并重命名,基于WPF和阿里云OCR识别的解决
  • 【OpenAI】Updating your OpenAI library to the latest version
  • MySQL高可用方案全攻略:选型指南与AI运维实践
  • Linux——Mysql数据库操作
  • keepalived详细笔记
  • 【react组件】矩形框选小组件,鼠标左键选中 div,键盘 ESC 清空
  • Transformer编码器+SHAP分析,模型可解释创新表达!
  • 开源AI对比--dify、n8n
  • 云原生环境下服务治理体系的构建与落地实践
  • 【计算机视觉】OpenCV实战项目: Fire-Smoke-Dataset:基于OpenCV的早期火灾检测项目深度解析
  • 王受文已任全国工商联党组成员
  • 纽约大学朗格尼医学中心的转型带来哪些启示?
  • 夜读丨古代有没有近视眼?
  • 长安汽车辟谣作为二级企业并入东风集团:将追究相关方责任
  • 诺和诺德一季度减重版司美格鲁肽收入增83%,美国市场竞争激烈下调全年业绩预期
  • 联合国秘书长吁印巴“最大程度克制”,特朗普:遗憾,希望尽快结束冲突