文件的上传和下载都是基于io复制,只不过
文件上传是浏览器向服务器发送报文
文件下载是服务器向浏览器发送报文
提示:以下是本篇文章正文内容,下面案例可供参考
pom.xml
<dependencies> <!-- springmvc --> <dependency> <groupid>org.spr纯粹的近义词ingframework</groupid> <artifactid>spring-webmvc</artifactid> <version>5.3.13</version> </dependency> <!-- 日志 --> <dependency> <groupid>ch.qos.logback</groupid> <artifactid>logback-classic</artifactid> <version>1.2.3</version> </dependency> <!-- rvletapi --> <dependency> <groupid>javax.rvlet</groupid> <artifactid>javax.rvlet-api</artifactid> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- spring5和thymeleaf整合包 --> <dependency> <groupid>org.thymeleaf</groupid> <artifactid>thymeleaf-spring5</artifactid> <version>3.0.12.relea</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </dependency><!-- springmvc end--> <!-- 节约用水图片 导入spring坐标--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>5.3.11</version> </dependency><!-- 导入测试坐标--> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> <scope>test</scope> </dependency> <!-- 测试坐标--><!-- 导入集成测试坐标--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>5.3.13</version> </dependency> <!-- 集成测试坐标end--> <!-- 导入mysql坐标--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>8.0.24</version> </dependency> <!-- mysql坐标end--> <!-- 导入druid连接池--> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.2.8</version> </dependency> <!-- 导入druid连接池end--> <!-- 导入spring上下文--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>5.3.11</version> </dependency> <!-- 导入spring上下文end--><!-- 导入spring web开发api start--> <dependency> <groupid>javax.rvlet</groupid> <artifactid>javax.rvlet-api</artifactid> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.rvlet.jsp</groupid> <artifactid>javax.rvlet.jsp-api</artifactid> <version>2.3.3</version> </dependency> <!-- 导入spring web开发end--><!-- 导入jackson的依赖--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.12.1</version> </dependency> <!--上传功能的关键jar --> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </dependency> </dependencies>
上传功能的关键jar
web.xml
<?xml version="1.0" encoding="utf-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-爱情阶梯 电影instance" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置springmvc的编码过滤器--> <!-- springmvc中处理编码的过滤器一定要配置到其他过滤器之前,否则无效--> <!-- 字符集过滤器 --> <filter> <description>字符集过滤器</description> <filter-name>characterencodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <description>字符集编码</description> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> </filter> <!-- 字符集拦截 --> <filter-mapping> <filter-name>characterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>hiddenhttpmethodfilter</filter-name> <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenhttpmethodfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 注册前端控制器dispatcherrvlet--> <rvlet> <rvlet-name>dispatcherrvlet</rvlet-name> <rvlet-class>org.springframework.web.rvlet.dispatcherrvlet</rvlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </rvlet> <rvlet-mapping> <rvlet-name>dispatcherrvlet</rvlet-name> <url-pattern>/</url-pattern> </rvlet-mapping></web-app>
springmvc.xml
<?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/" xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:context="/d/file/titlepic/" xmlns:mvc="/d/file/titlepic/" xsi:schemalocation="http://www.springframework.org/schema/beans /d/file/titlepic/spring-beans.xsd http://www.springframework.org/schema/context /d/file/titlepic/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描组件--> <context:component-scan ba-package="com.vector"/> <!-- 配置thymeleaf视图解析器 --> <bean id="viewresolver" class="org.thymeleaf.spring5.view.thymeleafviewresolver"> <property name="order" value="1"/> <property name="characterencoding" value="utf-8"/> <property name="templateengine"> <bean class="org.thymeleaf.spring5.springtemplateengine"> <property name="templateresolver"> <bean class="org.thymeleaf.spring5.templateresolver.springresourcetemplateresolver"> <!-- 视图前缀 --> <property name="prefix" value="/web-inf/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templatemode" value="html5"/> <property name="characterencoding" value="utf-8" /> </bean> </property> </bean> </property> </bean> <!-- 处理响应中文内容乱码 --> <bean class="org.springframework.http.converter.stringhttpmessageconverter"> <property name="defaultchart" value="utf-8" /> <property name="supportedmediatypes"> <list> <value>text/html&新乡学校lt;/value> <value>application/json</value> </list> </property> </bean> <mvc:view-controller path="/" view-name="index"/> <mvc:view-controller path="/file" view-name="file"/> <mvc:default-rvlet-handler/> <mvc:annotation-driven/> <!--必须通过文件解析器的解析才能将文件转换为multipartfile对象--> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"></bean></beans>
实现页面跳转,vue文件解析,上传内容解析的关键
file.html内容demo
<body><a th:href="@{/testdown}" rel="external nofollow" >下载1.jpg</a><form th:action="@{/testup}" method="post" enctype="multipart/form-data"> 头像: <input type="file" name="photo"><br> <input type="submit" value="上传"></form></body>
package com.vector.controller;import org.springframework.beans.factory.annotation.value;import org.springframework.http.httpheaders;import org.springframework.http.httpstatus;import org.springframework.http.responentity;import org.springframework.stereotype.controller;import org.springframework.util.multivaluemap;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.multipart.multipartfile;import javax.annotation.resource;import javax.rvlet.rvletcontext;import javax.rvlet.http.httpssion;import java.io.file;import java.io.fileinputstream;import java.io.ioexception;import java.io.inputstream;import java.util.uuid;@controllerpublic class fileupanddowncontroller { @requestmapping("/testdown") public responentity<byte[]> testresponentity(httpssion ssion) throws ioexception { //获取rvletcontext对象 rvletcontext rvletcontext = ssion.getrvletcontext(); //获取服务器中文件的真实路径 string realpath = rvletcontext.getrealpath("/static/img/1.jpg"); //创建输入流 inputstream is = new fileinputstream(realpath); //创建字节数组 byte[] bytes = new byte[is.available()]; //将流读到字节数组中 is.read(bytes); //创建httpheaders对象设置响应头信息 multivaluemap<string, string> headers = new httpheaders(); //设置要下载方式以及下载文件的名字 //content-disposition 固定回复内容格式 attachment以附件形式下载 filename=1.jpg文件名 //可以将filename拼接为动态命名 headers.add("content-disposition", "attachment;filename=1.jpg"); //设置响应状态码 httpstatus statuscode = httpstatus.ok; //创建responentity对象 responentity<byte[]> responentity = new responentity<>(bytes, headers, statuscode); //关闭输入流 is.clo(); return responentity; } @requestmapping("/testup") public string testup(@value("multipartresolver") multipartfile photo,httpssion ssion) throws ioexception { //获取上传的文件的文件名 string filename = photo.getoriginalfilename(); 初一数学下册练习题 //处理文件重名问题 //重名问题是java.io中写入同一文件默认覆盖原文件内容导致图片被覆盖. //获取文件名后缀 string suffixname = filename.substring(filename.lastindexof(".")); //将uuid作为文件名 uuid是32位随机数,几乎不可能会重复 filename = uuid.randomuuid().tostring() + suffixname; //获取服务器中photo目录的路径 rvletcontext rvletcontext = ssion.getrvletcontext(); string photopath = rvletcontext.getrealpath("photo"); file file = new file(photopath); //判断服务器是否存在该路径 if(!file.exists()){ file.mkdir(); } string finalpath = photopath + file.parator + filename; //实现上传功能 photo.transferto(new file(finalpath)); return "success"; }}
下载功能测试
上传功能测试
到此这篇关于springmvc实现文件上传下载的文章就介绍到这了,更多相关springmvc文件上传下载内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 13:12:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7d41b10e2a4866b831148abd8a81f4d0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringMVC实现文件上传下载的全过程.doc
本文 PDF 下载地址:SpringMVC实现文件上传下载的全过程.pdf
留言与评论(共有 0 条评论) |