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

SpringMVC实现文件上传和下载

目录

前言

一. SpringMVC文件上传

1. 配置多功能视图解析器

2. 前端代码中,将表单标记为多功能表单

3. 后端利用MultipartFile 接口,接收前端传递到后台的文件

4. 文件上传示例

1. 相关依赖:

 2. 逆向生成对应的类

3. 后端代码:

4. 前端代码:

5. 多文件上传 

二. SpringMVC文件下载

三. jrebel的使用

1. jrebel是什么?

2. jrebel的安装

3.jrebel的使用


前言

在实际的项目开发中,文件的上传和下载可以说是最常用的功能之一,例如图片的上传与下载、邮件附件的上传和下载等。本篇我们将对 Spring MVC 中的文件上传和文件下载功能进行分享。

一. SpringMVC文件上传

在 Spring MVC 中想要实现文件上传工作,需要的步骤如下:

1. 配置多功能视图解析器

Spring MVC 提供了一个名为 MultipartResolver 的文件解析器,来实现文件上传功能。MultipartResolver 本身是一个接口,我们需要通过它的实现类来完成对它的实例化工作。

MultipartResolver 接口共有两个实现类,如下表:

实现类说明依赖支持的 Servlet 版本
StandardServletMultipartResolver它是 Servlet 内置的上传功能。不需要第三方 JAR 包的支持。 仅支持 Servlet 3.0 及以上版本
CommonsMultipartResolver借助 Apache 的 commons-fileupload 来完成具体的上传操作。需要 Apache 的 commons-fileupload 等 JAR 包的支持。不仅支持 Servlet 3.0 及以上版本,还可以在比较旧的 Servlet 版本中使用。

 以上这两个 MultipartResolver 的实现类,无论使用哪一个都可以实现 Spring MVC 的文件上传功能。这里,我以 CommonsMultipartResolver 为例。

导入pom相关依赖:

<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.3</version>
</dependency>

想要在 Spring MVC 中使用 CommonsMultipartResolver 对象实现文件上传,我们需要在 Spring MVC 的配置文件中对其进行以下配置:

<!--    处理文件上传与下载--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 --><property name="defaultEncoding" value="UTF-8"></property><!-- 文件最大大小(字节) 1024*1024*50=50M--><property name="maxUploadSize" value="52428800"></property><!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常--><property name="resolveLazily" value="true"/></bean>

通过 <property> 可以对 CommonsMultipartResolver 的多个属性进行配置,其中常用的属性如下表。

属性说明
defaultEncoding上传文件的默认编码格式。
maxUploadSize上传文件的最大长度(单位为字节)。
maxInMemorySize读取文件到内存中的最大字节数。
resolveLazily判断是否要延迟解析文件。

 注意:当我们在 Spring MVC 的配置文件中对 CommonsMultipartResolver 的 Bean 进行定义时,必须指定这个 Bean 的 id 为 multipartResolver,否则就无法完成文件的解析和上传工作。

2. 前端代码中,将表单标记为多功能表单

在 Spring MVC 项目中,大多数的文件上传功能都是通过 form 表单提交到后台服务器的。

form 表单想要具有文件上传功能,其必须满足以下 3 个条件。

  • form 表单的 method 属性必须设置为 post。
  • form 表单的 enctype 属性设置为 multipart/form-data。
  • 至少提供一个 type 属性为 file 的 input 输入框。

示例代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>图片上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/clazz/upload" method="post" enctype="multipart/form-data"><label>班级编号:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/><label>班级图片:</label><input type="file" name="xxx"/><br/><input type="submit" value="上传图片"/>
</form>
</body>
</html>

当 form 表单的 enctype 属性为 multipart/form-data 时,浏览器会以二进制流的方式对表单数据进行处理,由服务端对文件上传的请求进行解析和处理。

3. 后端利用MultipartFile 接口,接收前端传递到后台的文件

controller层:

//    文件上传@RequestMapping("/upload")public String upload(clazz clazz,MultipartFile xxx){try {
//        上传的图片真实存放地址String dir = PropertiesUtil.getValue("dir");
//        网络访问地址String server = PropertiesUtil.getValue("server");String filename = xxx.getOriginalFilename();System.out.println("文件名:"+filename);String contentType = xxx.getContentType();System.out.println("文件类别:"+contentType);FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
//            更新数据库表t_struts_class图片记录clazz.setPic(server+filename);clazzBiz.updateByPrimaryKeySelective(clazz);} catch (IOException e) {e.printStackTrace();}return "redirect:clzlist";}

 在该控制器方法中包含一个 org.springframework.web.multipart.MultipartFile 接口类型的形参,该参数用来封装被上传文件的信息。MultipartFile 接口是 InputStreamSource 的子接口,该接口中提供了多个不同的方法,如下表。

名称作用
byte[] getBytes()以字节数组的形式返回文件的内容。
String getContentType()返回文件的内容类型。
InputStream getInputStream()返回一个 input 流,从中读取文件的内容。
String getName()返回请求参数的名称。
String getOriginalFillename()返回客户端提交的原始文件名称。
long getSize()返回文件的大小,单位为字节。
boolean isEmpty()判断被上传文件是否为空。
void transferTo(File destination)将上传文件保存到目标目录下。

4. 文件上传示例

1. 相关依赖:

web配置如下:

<?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_3_1.xsd"version="3.1"><display-name>Archetype Created Web Application</display-name><!-- Spring和web项目集成start --><!-- spring上下文配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-context.xml</param-value></context-param><!-- 读取Spring上下文的监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Spring和web项目集成end --><!-- 中文乱码处理 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Spring MVC servlet --><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--此参数可以不配置,默认值为:/WEB-INF/springmvc-servlet.xml--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><!--web.xml 3.0的新特性,是否支持异步--><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

 spring-mvc.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:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--1) 扫描com.xissl及子子孙孙包下的控制器(扫描范围过大,耗时)--><context:component-scan base-package="com.xissl"/><!--2) 此标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --><mvc:annotation-driven /><!--3) 创建ViewResolver视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- viewClass需要在pom中引入两个包:standard.jar and jstl.jar --><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean><!--4) 单独处理图片、样式、js等资源 --><!-- <mvc:resources location="/css/" mapping="/css/**"/><mvc:resources location="/js/" mapping="/js/**"/><mvc:resources location="WEB-INF/images/" mapping="/images/**"/>--><!--    处理文件上传与下载--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 --><property name="defaultEncoding" value="UTF-8"></property><!-- 文件最大大小(字节) 1024*1024*50=50M--><property name="maxUploadSize" value="52428800"></property><!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常--><property name="resolveLazily" value="true"/></bean><!--    处理controller层发送请求到biz,会经过切面的拦截处理--><aop:aspectj-autoproxy/>
</beans>

数据库配置文件(jdbc.properties):

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

 generatorConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration><!-- 引入配置文件 --><properties resource="jdbc.properties"/><!--指定数据库jdbc驱动jar包的位置--><classPathEntry location="D:\\maven\\mvn_repository\\mysql\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/><!-- 一个数据库一个context --><context id="infoGuardian"><!-- 注释 --><commentGenerator><property name="suppressAllComments" value="true"/><!-- 是否取消注释 --><property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 --></commentGenerator><!-- jdbc连接 --><jdbcConnection driverClass="${jdbc.driver}"connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/><!-- 类型转换 --><javaTypeResolver><!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --><property name="forceBigDecimals" value="false"/></javaTypeResolver><!-- 01 指定javaBean生成的位置 --><!-- targetPackage:指定生成的model生成所在的包名 --><!-- targetProject:指定在该项目下所在的路径  --><javaModelGenerator targetPackage="com.xissl.model"targetProject="src/main/java"><!-- 是否允许子包,即targetPackage.schemaName.tableName --><property name="enableSubPackages" value="false"/><!-- 是否对model添加构造函数 --><property name="constructorBased" value="true"/><!-- 是否针对string类型的字段在set的时候进行trim调用 --><property name="trimStrings" value="false"/><!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 --><property name="immutable" value="false"/></javaModelGenerator><!-- 02 指定sql映射文件生成的位置 --><sqlMapGenerator targetPackage="com.xissl.mapper"targetProject="src/main/java"><!-- 是否允许子包,即targetPackage.schemaName.tableName --><property name="enableSubPackages" value="false"/></sqlMapGenerator><!-- 03 生成XxxMapper接口 --><!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 --><!-- type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 --><!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --><javaClientGenerator targetPackage="com.xissl.mapper"targetProject="src/main/java" type="XMLMAPPER"><!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --><property name="enableSubPackages" value="false"/></javaClientGenerator><!-- 配置表信息 --><!-- schema即为数据库名 --><!-- tableName为对应的数据库表 --><!-- domainObjectName是要生成的实体类 --><!-- enable*ByExample是否生成 example类 --><!--<table schema="" tableName="t_book" domainObjectName="Book"--><!--enableCountByExample="false" enableDeleteByExample="false"--><!--enableSelectByExample="false" enableUpdateByExample="false">--><!--&lt;!&ndash; 忽略列,不生成bean 字段 &ndash;&gt;--><!--&lt;!&ndash; <ignoreColumn column="FRED" /> &ndash;&gt;--><!--&lt;!&ndash; 指定列的java数据类型 &ndash;&gt;--><!--&lt;!&ndash; <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> &ndash;&gt;--><!--</table>--><table schema="" tableName="t_struts_class" domainObjectName="clazz"enableCountByExample="false" enableDeleteByExample="false"enableSelectByExample="false" enableUpdateByExample="false"></table></context>
</generatorConfiguration>

 2. 逆向生成对应的类

班级实体类(clazz):

package com.xissl.model;import lombok.ToString;@ToString
public class clazz {private Integer cid;private String cname;private String cteacher;private String pic;public clazz(Integer cid, String cname, String cteacher, String pic) {this.cid = cid;this.cname = cname;this.cteacher = cteacher;this.pic = pic;}public clazz() {super();}public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public String getCteacher() {return cteacher;}public void setCteacher(String cteacher) {this.cteacher = cteacher;}public String getPic() {return pic;}public void setPic(String pic) {this.pic = pic;}
}

mapper接口:

package com.xissl.mapper;import com.xissl.model.clazz;
import org.springframework.stereotype.Repository;import java.util.List;@Repository
public interface clazzMapper {int deleteByPrimaryKey(Integer cid);int insert(clazz record);int insertSelective(clazz record);clazz selectByPrimaryKey(Integer cid);int updateByPrimaryKeySelective(clazz record);int updateByPrimaryKey(clazz record);List<clazz> listPager(clazz clazz);
}

 sql映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xissl.mapper.clazzMapper" ><resultMap id="BaseResultMap" type="com.xissl.model.clazz" ><constructor ><idArg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" /><arg column="cname" jdbcType="VARCHAR" javaType="java.lang.String" /><arg column="cteacher" jdbcType="VARCHAR" javaType="java.lang.String" /><arg column="pic" jdbcType="VARCHAR" javaType="java.lang.String" /></constructor></resultMap><sql id="Base_Column_List" >cid, cname, cteacher, pic</sql><select id="listPager" resultType="com.xissl.model.clazz" parameterType="com.xissl.model.clazz" >select<include refid="Base_Column_List" />from t_struts_class<where><if test="cname != null">and cname like concat('%',#{cname},'%')</if></where></select><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from t_struts_classwhere cid = #{cid,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from t_struts_classwhere cid = #{cid,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.xissl.model.clazz" >insert into t_struts_class (cid, cname, cteacher, pic)values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{cteacher,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR})</insert><insert id="insertSelective" parameterType="com.xissl.model.clazz" >insert into t_struts_class<trim prefix="(" suffix=")" suffixOverrides="," ><if test="cid != null" >cid,</if><if test="cname != null" >cname,</if><if test="cteacher != null" >cteacher,</if><if test="pic != null" >pic,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="cid != null" >#{cid,jdbcType=INTEGER},</if><if test="cname != null" >#{cname,jdbcType=VARCHAR},</if><if test="cteacher != null" >#{cteacher,jdbcType=VARCHAR},</if><if test="pic != null" >#{pic,jdbcType=VARCHAR},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.xissl.model.clazz" >update t_struts_class<set ><if test="cname != null" >cname = #{cname,jdbcType=VARCHAR},</if><if test="cteacher != null" >cteacher = #{cteacher,jdbcType=VARCHAR},</if><if test="pic != null" >pic = #{pic,jdbcType=VARCHAR},</if></set>where cid = #{cid,jdbcType=INTEGER}</update><update id="updateByPrimaryKey" parameterType="com.xissl.model.clazz" >update t_struts_classset cname = #{cname,jdbcType=VARCHAR},cteacher = #{cteacher,jdbcType=VARCHAR},pic = #{pic,jdbcType=VARCHAR}where cid = #{cid,jdbcType=INTEGER}</update>
</mapper>

3. 后端代码:

业务逻辑层:

package com.xissl.biz;import com.xissl.model.clazz;
import com.xissl.utils.PageBean;
import org.springframework.stereotype.Repository;import java.util.List;@Repository
public interface clazzBiz {int deleteByPrimaryKey(Integer cid);int insert(clazz record);int insertSelective(clazz record);clazz selectByPrimaryKey(Integer cid);int updateByPrimaryKeySelective(clazz record);int updateByPrimaryKey(clazz record);List<clazz> listPager(clazz clazz, PageBean pageBean);
}

package com.xissl.biz.impl;import com.xissl.biz.clazzBiz;
import com.xissl.mapper.clazzMapper;
import com.xissl.model.clazz;
import com.xissl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** @author xissl* @create 2023-09-11 8:26*/
@Service
public class clazzBizImpl implements clazzBiz {@Autowiredprivate clazzMapper clazzMapper;@Overridepublic int deleteByPrimaryKey(Integer cid) {return clazzMapper.deleteByPrimaryKey(cid);}@Overridepublic int insert(clazz record) {return clazzMapper.insert(record);}@Overridepublic int insertSelective(clazz record) {return clazzMapper.insertSelective(record);}@Overridepublic clazz selectByPrimaryKey(Integer cid) {return clazzMapper.selectByPrimaryKey(cid);}@Overridepublic int updateByPrimaryKeySelective(clazz record) {return clazzMapper.updateByPrimaryKeySelective(record);}@Overridepublic int updateByPrimaryKey(clazz record) {return clazzMapper.updateByPrimaryKey(record);}@Overridepublic List<clazz> listPager(clazz clazz, PageBean pageBean) {return clazzMapper.listPager(clazz);}}

 controller层:

package com.xissl.web;import com.xissl.biz.clazzBiz;
import com.xissl.model.clazz;
import com.xissl.utils.PageBean;
import com.xissl.utils.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
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.List;@Controller
@RequestMapping("clazz")
public class clazzController {@Autowiredprivate clazzBiz clazzBiz;//    增@RequestMapping("/add")public String add(clazz clazz){int i = clazzBiz.insertSelective(clazz);return "redirect:clzlist";}//    删@RequestMapping("/del/{cid}")public String del(@PathVariable("cid") Integer cid){clazzBiz.deleteByPrimaryKey(cid);return "redirect:/clz/clzlist";}//    改@RequestMapping("/edit")public String edit(clazz clazz){clazzBiz.updateByPrimaryKey(clazz);return "redirect:clzlist";}//    查@RequestMapping("/clzlist")public String list(clazz clazz, HttpServletRequest request){
//        clazz clazz是前台用来给后台传递参数的PageBean pageBean = new PageBean();pageBean.setRequest(request);List<clazz> clzs = clazzBiz.listPager(clazz,pageBean);request.setAttribute("list",clzs);request.setAttribute("pageBean",pageBean);return "clz/clzlist";}//    数据回显@RequestMapping("/preSave")public String preSave(clazz clazz, Model model){if(clazz != null && clazz.getCid()!=null && clazz.getCid()!=0){clazz c = clazzBiz.selectByPrimaryKey(clazz.getCid());model.addAttribute("c",c);}return "clz/clzedit";}//    文件上传@RequestMapping("/upload")public String upload(clazz clazz,MultipartFile xxx){try {
//        上传的图片真实存放地址String dir = PropertiesUtil.getValue("dir");
//        网络访问地址String server = PropertiesUtil.getValue("server");String filename = xxx.getOriginalFilename();System.out.println("文件名:"+filename);String contentType = xxx.getContentType();System.out.println("文件类别:"+contentType);FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
//            更新数据库表t_struts_class图片记录clazz.setPic(server+filename);clazzBiz.updateByPrimaryKeySelective(clazz);} catch (IOException e) {e.printStackTrace();}return "redirect:clzlist";}
}

工具类PropertiesUtil:

package com.xissl.utils;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class PropertiesUtil {public static String getValue(String key) throws IOException {Properties p = new Properties();InputStream in = PropertiesUtil.class.getResourceAsStream("/resource.properties");p.load(in);return p.getProperty(key);}}

配置文件 resource.properties:

dir=D:/temp/upload/
server=/upload/

PageController(处理页面跳转):

package com.xissl.web;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;//  处理页面跳转
@Controller
public class PageController {@RequestMapping("/page/{page}")public String toPage(@PathVariable("page") String page){return page;}@RequestMapping("/page/{dir}/{page}")public String toRePage(@PathVariable("dir") String dir,@PathVariable("page") String page){return dir + "/" + page;}
}

4. 前端代码:

 clzlist.jsp:

<%--Created by IntelliJ IDEA.User: xisslDate: 2023/9/9Time: 14:46To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><linkhref="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"rel="stylesheet"><scriptsrc="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script><title>班级列表</title><style type="text/css">.page-item input {padding: 0;width: 40px;height: 100%;text-align: center;margin: 0 6px;}.page-item input, .page-item b {line-height: 38px;float: left;font-weight: 400;}.page-item.go-input {margin: 0 10px;}</style>
</head>
<body>
<form class="form-inline"action="${pageContext.request.contextPath }/clazz/clzlist" method="post"><div class="form-group mb-2"><input type="text" class="form-control-plaintext" name="sname"placeholder="请输入班级名称"><!-- 			<input name="rows" value="20" type="hidden"> --><!-- 不想分页 --><%--        <input name="pagination" value="false" type="hidden">--%></div><button type="submit" class="btn btn-primary mb-2">查询</button><a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/clazz/preSave">新增</a>
</form><table class="table table-striped"><thead><tr><th scope="col">班级编号</th><th scope="col">班级名称</th><th scope="col">带班教员</th><th scope="col">班级logo</th><th scope="col">操作</th></tr></thead><tbody><c:forEach  var="b" items="${list }"><tr><td>${b.cid }</td><td>${b.cname }</td><td>${b.cteacher }</td><td><img src="${b.pic }" style="height: 50px;width: 40px;"></td><td><a href="${pageContext.request.contextPath }/clazz/preSave?cid=${b.cid}">修改</a><a href="${pageContext.request.contextPath }/clazz/del/${b.cid}">删除</a><a href="${pageContext.request.contextPath }/page/clz/upload?cid=${b.cid}">图片上传</a><a href="${pageContext.request.contextPath }/clazz/download?cid=${b.cid}">图片下载</a></td></tr></c:forEach></tbody>
</table>
<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
<z:page pageBean="${pageBean }"></z:page></body>
</html>

clzedit.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>编辑界面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/${empty c ? 'clazz/add' : 'clazz/edit'}" method="post">班级编号:<input type="text" name="cid" value="${s.cid }"><br>班级名称:<input type="text" name="cname" value="${s.cname }"><br>带班教员:<input type="text" name="cteacher" value="${s.cteacher }"><br>班级logo:<input type="text" name="pic" value="${s.pic }"><br><input type="submit">
</form>
</body>
</html>

upload.jsp:
 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>图片上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/clazz/upload" method="post" enctype="multipart/form-data"><label>班级编号:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/><label>班级图片:</label><input type="file" name="xxx"/><br/><input type="submit" value="上传图片"/>
</form>
</body>
</html>

5. 多文件上传 

在<input> 标签中增加一个 multiple 属性。该属性可以让我们同时选择对多个文件进行上传,即实现多文件上传功能。 

//多文件上传@RequestMapping("/uploads")public String uploads(HttpServletRequest req, clazz clazz, MultipartFile[] files){try {StringBuffer sb = new StringBuffer();for (MultipartFile cfile : files) {//思路://1) 将上传图片保存到服务器中的指定位置String dir = PropertiesUtil.getValue("dir");String server = PropertiesUtil.getValue("server");String filename = cfile.getOriginalFilename();FileUtils.copyInputStreamToFile(cfile.getInputStream(),new File(dir+filename));sb.append(filename).append(",");}System.out.println(sb.toString());} catch (Exception e) {e.printStackTrace();}return "redirect:list";}

二. SpringMVC文件下载

 文件下载的含义十分简单,它指的就是将服务器中的文件下载到本机上。

controller层:

 @RequestMapping(value="/download")public ResponseEntity<byte[]> download(clazz clazz, HttpServletRequest req){try {//先根据文件id查询对应图片信息clazz clz = this.clazzBiz.selectByPrimaryKey(clazz.getCid());String diskPath = PropertiesUtil.getValue("dir");String reqPath = PropertiesUtil.getValue("server");String realPath = clz.getPic().replace(reqPath,diskPath);String fileName = realPath.substring(realPath.lastIndexOf("/")+1);//下载关键代码File file=new File(realPath);HttpHeaders headers = new HttpHeaders();//http头信息String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码headers.setContentDispositionFormData("attachment", downloadFileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//MediaType:互联网媒介类型  contentType:具体请求中的媒体类型信息return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);}catch (Exception e){e.printStackTrace();}return null;}

三. jrebel的使用

1. jrebel是什么?

JRebel 是国外团队开发的一款收费工具,JRebel 允许开发团队在有限的时间内完成更多的任务修正更多的问题,发布更高质量的软件产品,JRebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率。
JRebel 是一款 JAVA 虚拟机插件,它使得 Java 程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响。

2. jrebel的安装

打开IDEA,选择File—>Settings—>Plugins—>在右侧选择Marketplace
在搜索框输入jrebel—>选择搜索结果—>点击Install(安装),如下图。

3.jrebel的使用

下载激活软件github github地址

1.下载后双击运行该程序ReverseProxy_windows_amd64.exe(window 64位系统)

2. jrebel启动项目

注意:一定要先打开代理ReverseProxy_windows_amd64.exe,再启动jrebel

启动就jrebel后,会弹出一个提示框让你进行激活,点击激活即可。

激活地址填写:http://127.0.0.1:8888 后面再拼接一个GUID

在线GUID地址

然后点击Activate JRebel就可以激活了

激活成功后点击Work online切换到离线状态

相关文章:

SpringMVC实现文件上传和下载

目录 前言 一. SpringMVC文件上传 1. 配置多功能视图解析器 2. 前端代码中&#xff0c;将表单标记为多功能表单 3. 后端利用MultipartFile 接口&#xff0c;接收前端传递到后台的文件 4. 文件上传示例 1. 相关依赖&#xff1a; 2. 逆向生成对应的类 3. 后端代码&#xf…...

Your build is currently configured to use Java 20.0.2 and Gradle 8.0

jdk 版本不适配 下载jdk17 https://www.oracle.com/java/technologies/downloads/#jdk17 参考 JDK17的下载安装与配置(详细教程)_keyila798的博客-CSDN博客...

栈 之 如何实现一个栈

前言 栈最鲜明的特点就是后进先出&#xff0c;一碟盘子就是类似这样的结构&#xff0c;最晚放上去的&#xff0c;可以最先拿出来。本文将介绍的是如何自己实现一个栈结构。 栈的操作 栈是一种先进后出&#xff08;Last-In-First-Out, LIFO&#xff09;的数据结构&#xff0c…...

uni-app:自带的消息提示被遮挡的解决办法(自定义消息提示框)

效果&#xff1a; 代码&#xff1a; 1、在最外层或者根组件的模板中添加一个容器元素&#xff0c;用于显示提示消息。例如&#xff1a; <div class"toast-container" v-if"toastMessage"><div class"toast-content">{{ toastMessa…...

PHP设备检验系统Dreamweaver开发mysql数据库web结构php编程计算机网页代码

一、源码特点 PHP设备检验系统是一套完善的web设计系统&#xff0c;对理解php编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。 下载地址 https://download.csdn.net/download/qq_41221322/88306259 php设备检验系统1 …...

Windows 可以使用以下快捷键打开终端(命令提示符)

Windows 可以使用以下快捷键打开终端&#xff08;命令提示符&#xff09; 使用快捷键 Win R 打开 “运行” 对话框&#xff0c;然后输入 “cmd” 并按下 Enter 键。这将打开默认的命令提示符窗口。 使用快捷键 Ctrl Shift Esc 打开任务管理器&#xff0c;然后在 “文件” …...

Netty编程面试题

1.Netty 是什么&#xff1f; Netty是 一个异步事件驱动的网络应用程序框架&#xff0c;用于快速开发可维护的高性能协议服务器和客户端。Netty是基于nio的&#xff0c;它封装了jdk的nio&#xff0c;让我们使用起来更加方法灵活。 2.Netty 的特点是什么&#xff1f; 高并发&a…...

math_review

topics mathmatics supreme and optimumNorm and Linear producttopology of R*Continuious Function supreme and optimum Def 1: 非空有界集合必有上确界 common norm (1) x ∈ \in ∈ Rn, ||x||2 x 1 2 x 2 2 . . . x n 2 \sqrt {x_1^2x_2^2...x_n^2} x12​x22​.…...

肖sir__设计测试用例方法之场景法04_(黑盒测试)

设计测试用例方法之场景法 1、场景法主要是针对测试场景类型的&#xff0c;顾也称场景流程分析法。 2、流程分析是将软件系统的某个流程看成路径&#xff0c;用路径分析的方法来设计测试用例。根据流程的顺序依次进行组合&#xff0c;使得流程的各个分支能走到。 举例说明&…...

plt函数显示图片 在图片上画边界框 边界框坐标转换

一.读取图片并显示图片 %matplotlib inline import torch from d2l import torch as d2l读取图片 image_path ../data/images/cat_dog_new.jpg # 创建画板 figure d2l.set_figsize() image d2l.plt.imread(image_path) d2l.plt.imshow(image);二.给出一个(x左上角,y左上角,…...

运行期获得文件名和行号

探索动态日志模块的实现 最初的目标是创建一个通用的日志模块, 它具有基本的日志输出功能并支持重定向. 这样, 如果需要更换日志模块, 可以轻松实现. 最初的构想是通过函数重定向, 即使用 dlsym 来重定向所有函数以实现打印功能. 然而, 这种方法引发了一个问题, 即无法正确获…...

数组操作UNIAPP

字符串转数组 let string "12345,56789" string.split(,) // [12345,56789] 数组转字符串 let array ["123","456"] array.join(",") // "123,456" 数组元素删除 let array [123,456] // 删除起始下标为1&#xff0…...

MySQL——无法打开MySQL8.0软件安装包或者安装过程中失败,如何解决?

在运行MySQL8.0软件安装包之前&#xff0c;用户需要确保系统中已经安装了.Net Framework相关软件&#xff0c;如果缺少此软件&#xff0c;将不能正常地安装MySQL8.0软件。 解决方案&#xff1a;到这个地址 https://www.microsoft.com/en-us/download/details.aspx?id42642…...

DB2存储过程如何编写和执行

db2执行文件参数&#xff1a; -t 表示语句使用默认的语句终结符——分号&#xff1b;   -v 表示使用冗长模式&#xff0c;这样 DB2 会显示每一条正在执行命令的信息&#xff1b;   -f 表示其后就是脚本文件&#xff1b;   -z表示其后的信息记录文件用于记录屏幕的输出&am…...

SpringBoot + FFmpeg实现一个简单的M3U8切片转码系统

简介 在本文中&#xff0c;我们将使用SpringBoot和FFmpeg来实现一个简单的M3U8切片转码系统。M3U8是一种常用的视频流媒体播放列表格式&#xff0c;而FFmpeg则是一个强大的音视频处理工具。 技术栈 SpringBoot&#xff1a;一个基于Spring框架的快速开发平台。FFmpeg&#xf…...

SpringCloud(35):Nacos 服务发现快速入门

本小节,我们将演示如何使用Spring Cloud Alibaba Nacos Discovery为Spring cloud 应用程序与 Nacos 的无缝集成。 通过一些原生的spring cloud注解,我们可以快速来实现Spring cloud微服务的服务发现机制,并使用Nacos Server作为服务发现中心,统一管理所有微服务。 1 Spring…...

OSPF实验:配置与检测全网互通

文章目录 一、实验背景与目的二、实验拓扑三、实验需求四、实验解法1. 配置 IP 地址2. 按照图示分区域配置 OSPF &#xff0c;实现全网互通3. 检查是否全网互通 摘要&#xff1a; 本篇文章介绍了一个 OSPF&#xff08;Open Shortest Path First&#xff09;实验&#xff0c;旨在…...

常见的五种设计模式

https://www.runoob.com/design-pattern/factory-pattern.html 单例模式 **意图&#xff1a;**保证一个类仅有一个实例&#xff0c;并提供一个访问它的全局访问点。 **主要解决&#xff1a;**一个全局使用的类频繁地创建与销毁。 **何时使用&#xff1a;**当您想控制实例数目…...

pandas读取一个 文件夹下所有excel文件

我这边有个需求&#xff0c;是要求汇总一个文件夹所有的excel文件&#xff0c; 其中有.xls和 .xlsx文件&#xff0c;同时还excel文件中的数据可能还不一致&#xff0c;会有表头数据不一样需要一起汇总。 首先先遍历子文件夹并读取Excel文件&#xff1a; 使用os库来遍历包含子文…...

Python网页请求超时如何解决

在进行网络爬虫项目时&#xff0c;我们经常需要发送大量的请求来获取所需的数据。然而&#xff0c;由于网络环境的不稳定性&#xff0c;请求可能会因为超时而失败。请求超时可能导致数据获取不完整&#xff0c;影响爬虫的效率和准确性。此外&#xff0c;频繁的请求超时可能会被…...

突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合

强化学习&#xff08;Reinforcement Learning, RL&#xff09;是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程&#xff0c;然后使用强化学习的Actor-Critic机制&#xff08;中文译作“知行互动”机制&#xff09;&#xff0c;逐步迭代求解…...

iPhone密码忘记了办?iPhoneUnlocker,iPhone解锁工具Aiseesoft iPhone Unlocker 高级注册版​分享

平时用 iPhone 的时候&#xff0c;难免会碰到解锁的麻烦事。比如密码忘了、人脸识别 / 指纹识别突然不灵&#xff0c;或者买了二手 iPhone 却被原来的 iCloud 账号锁住&#xff0c;这时候就需要靠谱的解锁工具来帮忙了。Aiseesoft iPhone Unlocker 就是专门解决这些问题的软件&…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序

一、开发准备 ​​环境搭建​​&#xff1a; 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 ​​项目创建​​&#xff1a; File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

oracle与MySQL数据库之间数据同步的技术要点

Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异&#xff0c;它们的数据同步要求既要保持数据的准确性和一致性&#xff0c;又要处理好性能问题。以下是一些主要的技术要点&#xff1a; 数据结构差异 数据类型差异&#xff…...

CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云

目前已有本地上传、阿里云OSS上传、腾讯云COS上传、七牛云上传扩展 扩展入口文件 文件目录 crmeb\services\upload\Upload.php namespace crmeb\services\upload;use crmeb\basic\BaseManager; use think\facade\Config;/*** Class Upload* package crmeb\services\upload* …...

3-11单元格区域边界定位(End属性)学习笔记

返回一个Range 对象&#xff0c;只读。该对象代表包含源区域的区域上端下端左端右端的最后一个单元格。等同于按键 End 向上键(End(xlUp))、End向下键(End(xlDown))、End向左键(End(xlToLeft)End向右键(End(xlToRight)) 注意&#xff1a;它移动的位置必须是相连的有内容的单元格…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...