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

springmvc上传与下载

文件上传

结构图

在这里插入图片描述

导入依赖

<dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.8.RELEASE</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version></dependency>

web.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></init-param><!--启动顺序--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>

上传一个或多个文件

index.jsp代码

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<form method="post" action="file" enctype="multipart/form-data">上传文件:<input type="file" name="picture"><input type="submit" value="提交">
</form><hr>
<h1>多文件</h1>
<form method="post" action="files" enctype="multipart/form-data">上传文件1:<input type="file" name="pictures">上传文件2:<input type="file" name="pictures">上传文件3:<input type="file" name="pictures"><input type="submit" value="提交">
</form>
</body>
</html>

WEB-INF下的success.jsp代码

在这里插入图片描述

spring.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.tmg"></context:component-scan>
<!--    视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"/><property name="suffix" value=".jsp"/></bean>
<!--    静态资源处理器--><mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--    注解驱动--><mvc:annotation-driven></mvc:annotation-driven>
<!--    文件上传处理器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="UTF-8"></property><property name="maxUploadSize" value="10485760"></property></bean>
</beans>

FileController代码

运行的时候,浏览器页面要在WEB-INF下的index.jsp,跑完该项目时,页面是webapp下的index.jsp,所以跑完之后切换请求路径(/toFile)进入WEB-INF下的index.jsp

package com.tmg.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.IOException;@Controller
public class FileController {private static final String DIR = "D:\\picture\\";@RequestMapping("/toFile")public String text() {return "/index";}@RequestMapping("file")public String text1(MultipartFile picture) throws IOException {String filename = picture.getOriginalFilename();picture.transferTo(new File(DIR, filename));return "success";}@RequestMapping("/files")public String text2(MultipartFile[] pictures) {try {for (MultipartFile picture : pictures) {String filename = picture.getOriginalFilename();if("".equals(filename)){continue;}picture.transferTo(new File(DIR, filename));System.out.println(filename);}} catch (IOException e) {e.printStackTrace();} finally {return "success";}}}

文件下载

DownloadController代码

跑的时候记得在路径带上 file=XXX ,并在执行前指定自己下载目录

package com.tmg.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;/*** 下载控制器*/
@Controller
public class DownloadController {@RequestMapping("/download")public void download(String file,  HttpServletResponse response) throws IOException {//下载文件的路径String path = "D:\\picture\\";File downFile = new File(path+file);if(downFile.exists()){//设置浏览器下载内容类型,响应头response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition","attachment;filename="+file);//通过流发送文件Files.copy(downFile.toPath(),response.getOutputStream());}}
}

相关文章:

springmvc上传与下载

文件上传 结构图 导入依赖 <dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId…...

论文阅读笔记AI篇 —— Transformer模型理论+实战 (一)

资源地址Attention is all you need.pdf(0积分) - CSDN 第一遍阅读&#xff08;Abstract Introduction Conclusion&#xff09; Abstract中强调Transformer摒弃了循环和卷积网络结构&#xff0c;在English-to-German翻译任务中&#xff0c;BLEU得分为28.4&#xff0c; 在En…...

Linux之shell编程(BASH)

Shell编程概述&#xff08;THE bourne-again shell&#xff09; Shell名词解释(外壳&#xff0c;贝壳) Kernel Linux内核主要是为了和硬件打交道 Shell 命令解释器&#xff08;command interperter&#xff09; Shell是一个用C语言编写的程序&#xff0c;他是用户使用Lin…...

HarmonyOS—声明式UI描述

ArkTS以声明方式组合和扩展组件来描述应用程序的UI&#xff0c;同时还提供了基本的属性、事件和子组件配置方法&#xff0c;帮助开发者实现应用交互逻辑。 创建组件 根据组件构造方法的不同&#xff0c;创建组件包含有参数和无参数两种方式。 说明 创建组件时不需要new运算…...

实验笔记之——基于TUM-RGBD数据集的SplaTAM测试

之前博客对SplaTAM进行了配置&#xff0c;并对其源码进行解读。 学习笔记之——3D Gaussian SLAM&#xff0c;SplaTAM配置&#xff08;Linux&#xff09;与源码解读-CSDN博客SplaTAM全称是《SplaTAM: Splat, Track & Map 3D Gaussians for Dense RGB-D SLAM》&#xff0c;…...

SpringBoot SaToken Filter如用使用ControllerAdvice统一异常拦截

其实所有的Filter都是一样的原理 大致流程: 创建一个自定义Filter, 用于拦截所有异常此Filter正常进行后续Filter调用当调用后续Filter时, 如果发生异常, 则委托给HandlerExceptionResolver进行后续处理即可 以sa-token的SaServletFilter为例 首先注册SaToken的过滤器 pac…...

基于HarmonyOS的华为智能手表APP开发实战——Fitness_华为手表app开发

、 基于HarmonyOS的华为智能手表APP开发实战——Fitness_华为手表app开发 Excerpt 文章浏览阅读8.7k次,点赞6次,收藏43次。本文针对华为HarmonyOS智能穿戴产品(即HUAWEI WATCH 3)开发了一款运动健康类的游戏化APP——Fitness,旨在通过游戏化的方式,提升用户运动动机。_华…...

1.6用命令得到ip和域名解析<网络>

专栏导航 第五章 如何用命令得到自己的ip<本地> 第六章 用命令得到ip和域名解析<网络> ⇐ 第七章 用REST API实现dynv6脚本(上) 用折腾路由的兴趣,顺便入门shell编程。 第六章 用命令得到ip和域名解析<网络> 文章目录 专栏导航第六章 用命令得到ip和域名解…...

leetcode 399除法求值 超水带权并查集

题目 class Solution { public:int f[45];double multi[45];map<string,int>hash;int tot0;int seek(int x){if(xf[x]) return x;int faf[x];f[x]seek(fa);multi[x]*multi[fa];return f[x];}vector<double> calcEquation(vector<vector<string>>&…...

【Linux】Linux 系统编程——touch 命令

文章目录 1.命令概述2.命令格式3.常用选项4.相关描述5.参考示例 1.命令概述 在**Linux 中&#xff0c;每个文件都与时间戳相关联&#xff0c;每个文件都存储了上次访问时间、**上次修改时间和上次更改时间的信息。因此&#xff0c;每当我们创建新文件并访问或修改现有文件时&a…...

SpringBoot项目打包

1.在pom.xml中加入如下配置 <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.1.0</version><configuration><descriptorRef…...

Android Google 开机向导定制 setup wizard

Android 开机向导定制 采用 rro_overlays 机制来定制开机向导&#xff0c;定制文件如下&#xff1a; GmsSampleIntegrationOverlay$ tree . ├── Android.bp ├── AndroidManifest.xml └── res └── raw ├── wizard_script_common_flow.xml ├── wizard_script…...

OpenEL GS之深入解析视频图像处理中怎么实现错帧同步

一、什么是错帧同步? 现在移动设备的系统相机的最高帧率在 120 FPS 左右,当帧率低于 20 FPS 时,用户可以明显感觉到相机画面卡顿和延迟。我们在做相机预览和视频流处理时,对每帧图像处理时间过长(超过 30 ms)就很容易造成画面卡顿,这个场景就需要用到错帧同步方法去提升…...

MyBatis处理LIKE查询时,如何将传值中包含下划线_和百分号%等特殊字符处理成普通字符而不是SQL的单字符通配符

MySQL中&#xff0c;_和%在LIKE模糊匹配中有特殊的含义&#xff1a; 下划线 _ 在LIKE模糊匹配中表示匹配任意单个字符。百分号 % 在LIKE模糊匹配中表示匹配任意多个字符&#xff08;包括零个字符&#xff09; 如果这种字符不经过处理&#xff0c;并且你的模糊查询sql语句书写…...

Spring MVC(三) 国际化

SpringMVC 国际化 1、添加相关依赖2、配置MessageSourceBean方式一&#xff1a;ReloadableResourceBundleMessageSource方式二&#xff1a;ResourceBundleMessageSource 3、添加消息资源文件英文 messages_en.properties中文 messages_zh_CN.properties默认 messages.propertie…...

四款坚固耐用、小尺寸、1EDB9275F、1EDS5663H、1EDN9550B、1EDN7512G单通道栅极驱动器IC

1、1EDB9275F 采用DSO-8 150mil封装的单通道隔离栅极驱动器&#xff08;PG-DSO-8&#xff09; EiceDRIVER™ 1EDB 产品系列 单通道栅极驱动器IC具有3 kVrms的输入输出隔离电压额定值。 栅极驱动器系列具有6/-4 ns传输延迟精度&#xff0c;可针对具有高系统级效率的快速开关应…...

登录页添加验证码

登录页添加验证码 引入验证码页面组件&#xff1a;ValidCode.vue <template><div class"ValidCodeContent" style""><divclass"ValidCode disabled-select":style"width:${width}; height:${height}"click"refre…...

03--数据库连接池

1、数据库连接池 1.1 JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时&#xff0c;传统的模式基本是按以下步骤&#xff1a; 在主程序&#xff08;如servlet、beans&#xff09;中建立数据库连接进行sql操作断开数据库连接 这种模式开发&#xff0c;存在的问题:…...

MySQL表的基本插入查询操作详解

博学而笃志&#xff0c;切问而近思 文章目录 插入插入更新 替换查询全列查询指定列查询查询字段为表达式查询结果指定别名查询结果去重 WHERE 条件基本比较逻辑运算符使用LIKE进行模糊匹配使用IN进行多个值匹配 排序筛选分页结果更新数据删除数据截断表聚合函数COUNTSUMAVGMAXM…...

『 C++ 』红黑树RBTree详解 ( 万字 )

文章目录 &#x1f996; 红黑树概念&#x1f996; 红黑树节点的定义&#x1f996; 红黑树的插入&#x1f996; 数据插入后的调整&#x1f995; 情况一:ucnle存在且为红&#x1f995; 情况二:uncle不存在或uncle存在且为黑&#x1f995; 插入函数代码段(参考)&#x1f995; 旋转…...

MIXBOX vs MisstarTools:小米路由器插件管理工具深度对比与选择建议

MIXBOX vs MisstarTools&#xff1a;小米路由器插件生态深度解析与实战指南 当小米路由器遇上第三方插件管理工具&#xff0c;整个设备的可玩性会瞬间提升几个层级。作为长期折腾智能路由的玩家&#xff0c;我几乎试遍了市面上所有主流的小米路由器增强方案&#xff0c;其中最让…...

3分钟掌握Umi-OCR插件:打造你的专属文字识别工具箱

3分钟掌握Umi-OCR插件&#xff1a;打造你的专属文字识别工具箱 【免费下载链接】Umi-OCR_plugins Umi-OCR 插件库 项目地址: https://gitcode.com/gh_mirrors/um/Umi-OCR_plugins 还在为不同场景下的文字识别需求而烦恼吗&#xff1f;Umi-OCR插件库为你提供了完美的解决…...

Web开发中前端与Node服务中的信息安全与解决办法

Web开发中前端与Node服务中的信息安全与解决办法 input限制特殊字符和长度 漏洞描述&#xff1a; 永远不要相信用户输入的信息&#xff0c;如常规的注入脚本通过input输入之后被页面执行 整改办法 方法1&#xff1a;对于vue项目中ElementUI的el-input 和 原生input <el-in…...

好用还专业!盘点2026年备受推崇的一键生成论文工具

一天写完毕业论文在2026年已不再是天方夜谭。最新实测显示&#xff0c;一键生成论文工具正在颠覆传统写作方式&#xff0c;覆盖选题、文献、写作、降重、排版等核心场景&#xff0c;真正实现高效搞定论文&#xff0c;学生党必备神器。 一、全流程王者&#xff1a;一站式搞定论文…...

LFM2.5-1.2B-Thinking-GGUF前端面试题解析实战:模拟面试与答案生成

LFM2.5-1.2B-Thinking-GGUF前端面试题解析实战&#xff1a;模拟面试与答案生成 1. 开篇&#xff1a;AI如何改变前端面试准备方式 前端开发岗位的竞争日益激烈&#xff0c;技术面试的难度也水涨船高。传统的面试准备方式往往效率低下——求职者要么死记硬背网上的标准答案&…...

在单细胞测序数据分析中,barcodes、features和matrix是三个最核心的基础文件,它们共同构成了所有分析的基石。

在GEO&#xff08;Gene Expression Omnibus&#xff09;数据库中下载单细胞数据时&#xff0c;最常见的数据存储和提供形式主要有以下四种类型&#xff1a;10x Genomics 标准格式&#xff08;最主流&#xff09;在GEO的数据集中&#xff0c;我们通常会找到一个包含以下三个核心…...

OpenClaw配置优化:GLM-4.7-Flash模型响应速度提升

OpenClaw配置优化&#xff1a;GLM-4.7-Flash模型响应速度提升 1. 为什么需要优化GLM-4.7-Flash的响应速度 第一次用OpenClaw对接GLM-4.7-Flash模型时&#xff0c;我遇到了典型的"等待焦虑"——一个简单的文件整理任务竟然花了3分钟才返回结果。通过日志分析发现&am…...

实测2-5分钟:CogVideoX-2b生成速度与画质平衡的真实体验报告

实测2-5分钟&#xff1a;CogVideoX-2b生成速度与画质平衡的真实体验报告 1. 从文字到视频&#xff1a;CogVideoX-2b能做什么&#xff1f; 想象一下&#xff0c;你只需要输入一段文字描述&#xff0c;就能在几分钟内获得一段6秒的高清视频。这不是科幻电影里的场景&#xff0c…...

Spring Boot 3.0 + Vue 3 实战:手把手教你搭建图书管理系统(附完整源码)

Spring Boot 3.0 Vue 3 全栈实战&#xff1a;现代化图书管理系统开发指南 在当今快速发展的互联网时代&#xff0c;掌握前后端分离开发技术已成为中级开发者必备的核心竞争力。本文将带你从零开始&#xff0c;使用Spring Boot 3.0和Vue 3这两个当下最热门的技术栈&#xff0c;…...

Translumo完整指南:高效实时屏幕翻译工具解决你的多语言障碍难题

Translumo完整指南&#xff1a;高效实时屏幕翻译工具解决你的多语言障碍难题 【免费下载链接】Translumo Advanced real-time screen translator for games, hardcoded subtitles in videos, static text and etc. 项目地址: https://gitcode.com/gh_mirrors/tr/Translumo …...