当前位置: 首页 > 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; 旋转…...

基于算法竞赛的c++编程(28)结构体的进阶应用

结构体的嵌套与复杂数据组织 在C中&#xff0c;结构体可以嵌套使用&#xff0c;形成更复杂的数据结构。例如&#xff0c;可以通过嵌套结构体描述多层级数据关系&#xff1a; struct Address {string city;string street;int zipCode; };struct Employee {string name;int id;…...

idea大量爆红问题解决

问题描述 在学习和工作中&#xff0c;idea是程序员不可缺少的一个工具&#xff0c;但是突然在有些时候就会出现大量爆红的问题&#xff0c;发现无法跳转&#xff0c;无论是关机重启或者是替换root都无法解决 就是如上所展示的问题&#xff0c;但是程序依然可以启动。 问题解决…...

进程地址空间(比特课总结)

一、进程地址空间 1. 环境变量 1 &#xff09;⽤户级环境变量与系统级环境变量 全局属性&#xff1a;环境变量具有全局属性&#xff0c;会被⼦进程继承。例如当bash启动⼦进程时&#xff0c;环 境变量会⾃动传递给⼦进程。 本地变量限制&#xff1a;本地变量只在当前进程(ba…...

椭圆曲线密码学(ECC)

一、ECC算法概述 椭圆曲线密码学&#xff08;Elliptic Curve Cryptography&#xff09;是基于椭圆曲线数学理论的公钥密码系统&#xff0c;由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA&#xff0c;ECC在相同安全强度下密钥更短&#xff08;256位ECC ≈ 3072位RSA…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

以下是对华为 HarmonyOS NETX 5属性动画(ArkTS)文档的结构化整理,通过层级标题、表格和代码块提升可读性:

一、属性动画概述NETX 作用&#xff1a;实现组件通用属性的渐变过渡效果&#xff0c;提升用户体验。支持属性&#xff1a;width、height、backgroundColor、opacity、scale、rotate、translate等。注意事项&#xff1a; 布局类属性&#xff08;如宽高&#xff09;变化时&#…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

【JVM】- 内存结构

引言 JVM&#xff1a;Java Virtual Machine 定义&#xff1a;Java虚拟机&#xff0c;Java二进制字节码的运行环境好处&#xff1a; 一次编写&#xff0c;到处运行自动内存管理&#xff0c;垃圾回收的功能数组下标越界检查&#xff08;会抛异常&#xff0c;不会覆盖到其他代码…...

电脑插入多块移动硬盘后经常出现卡顿和蓝屏

当电脑在插入多块移动硬盘后频繁出现卡顿和蓝屏问题时&#xff0c;可能涉及硬件资源冲突、驱动兼容性、供电不足或系统设置等多方面原因。以下是逐步排查和解决方案&#xff1a; 1. 检查电源供电问题 问题原因&#xff1a;多块移动硬盘同时运行可能导致USB接口供电不足&#x…...

Python爬虫(一):爬虫伪装

一、网站防爬机制概述 在当今互联网环境中&#xff0c;具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类&#xff1a; 身份验证机制&#xff1a;直接将未经授权的爬虫阻挡在外反爬技术体系&#xff1a;通过各种技术手段增加爬虫获取数据的难度…...