宿迁网站建设怎么收费,软件开发外包平台,电脑系统下载官方网站,诚信企业品牌网站建设目录
一.SpringMvc文件上传
1.导入依赖#xff08;在pom.xml中#xff09;
2.配置文件上传解析器#xff08;在spring-mvc.xml中#xff09;
3.前端标记多功能表单#xff08;构建一个jsp界面来操作#xff09;
4.将文件写出流#xff0c;然后写入服务器
5.配置映…目录
一.SpringMvc文件上传
1.导入依赖在pom.xml中
2.配置文件上传解析器在spring-mvc.xml中
3.前端标记多功能表单构建一个jsp界面来操作
4.将文件写出流然后写入服务器
5.配置映射地址硬盘和网络地址的映射
5.1硬盘路径
5.2服务器路径
编辑
6.在resource包咯构建一个类用于映射
7.效果图形式
二.文件下载
1.编写方法用于图片下载
2.在list.jsp增加一个图片下载的点击事件
3.展示效果图
三..jrebel的使用
1.安装jrebel插件
2.打开代理ReverseProxy_windows_amd64.exe顺序不能错
3.jrebel启动项目
4.启动时要输入UUID
5.设置jrebel离线不需要打开打理
6.再次jrebel启动项目即可运行
四.多文件上传
1.编写用于多文件上传的语句
2.在upload.jsp增加用于多文件上传的from表单
3.运行结果 前言
小编详细的向读者展示了如在新建一个Maven项目的情况下去搭建一个Springmvc mybatismaven集成然后实现SpringMvc的CRUD,以及对于效果图的展示本次小编带来的的关于文件的上传以及下载
一.SpringMvc文件上传
1.导入依赖在pom.xml中
dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.3.3/version
/dependency
2.配置文件上传解析器在spring-mvc.xml中
bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver!-- 必须和用户JSP 的pageEncoding属性一致以便正确解析表单的内容 --property namedefaultEncoding valueUTF-8/property!-- 文件最大大小(字节) 1024*1024*5050M--property namemaxUploadSize value52428800/property!--resolveLazily属性启用是为了推迟文件解析以便捕获文件大小异常--property nameresolveLazily valuetrue//bean 3.前端标记多功能表单构建一个jsp界面来操作
%--Created by IntelliJ IDEA.User: lzzxqDate: 2023/9/9Time: 14:26To change this template use File | Settings | File Templates.
--%
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitle书籍头像上传/title
/head
bodyform action${pageContext.request.contextPath}/book/upload methodpost enctypemultipart/form-datalabel书籍编号/labelinput typetext namebid readonlyreadonly value${param.bid}/br/label书籍图片/labelinput typefile namexxx/br/input typesubmit value上传图片/
/form/body
/html4.将文件写出流然后写入服务器
后端利用muiltpartFile类接收前端传递到后端的文件
RequestMapping(/upload)//头像上传public String upload(Book book,MultipartFile xxx){try {//上传的图片存放地址String dirPropertiesUtil.getValue(dir);
// 网络访问地址String serverPropertiesUtil.getValue(server);String filename xxx.getOriginalFilename();System.out.println(文件名:filename);System.out.println(文件类型:xxx.getContentType());FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dirfilename));//修改字段名的属性book.setBname(serverfilename);bookBiz.updateByPrimaryKeySelective(book);} catch (IOException e) {e.printStackTrace();}return redirect:list;}
5.配置映射地址硬盘和网络地址的映射
5.1硬盘路径 5.2服务器路径 6.在resource包咯构建一个类用于映射 7.效果图形式 二.文件下载
1.编写方法用于图片下载
//文件下载RequestMapping(value/download)public ResponseEntitybyte[] download(Book book, HttpServletRequest req){try {//先根据文件id查询对应图片信息Book bkthis.bookBiz.selectByPrimaryKey(book.getBid());String diskPath PropertiesUtil.getValue(dir);String reqPath PropertiesUtil.getValue(server);String realPath bk.getBname().replace(reqPath,diskPath);String fileName realPath.substring(realPath.lastIndexOf(/)1);//下载关键代码File filenew 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 ResponseEntitybyte[](FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);}catch (Exception e){e.printStackTrace();}return null;}
2.在list.jsp增加一个图片下载的点击事件 a href${pageContext.request.contextPath }/book/download?bid${b.bid}图片下载/a
3.展示效果图 三..jrebel的使用
1.安装jrebel插件 2.打开代理ReverseProxy_windows_amd64.exe顺序不能错 3.jrebel启动项目 4.启动时要输入UUID
第一行输入 http://127.0.0.1:8888/GUID
其次在浏览器输入
GUID online erstellen Kostenloses Tool, um global eindeutige Nummern (GUIDs) zu generierenhttps://www.guidgen.com/ 在将GUID赋值GUID到网址前面
5.设置jrebel离线不需要打开打理 6.再次jrebel启动项目即可运行
四.多文件上传
1.编写用于多文件上传的语句
//多文件上传RequestMapping(/uploads)public String uploads(HttpServletRequest req, Book book, 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(dirfilename));sb.append(filename).append(,);}System.out.println(sb.toString());} catch (Exception e) {e.printStackTrace();}return redirect:list;}
2.在upload.jsp增加用于多文件上传的from表单
form methodpost action${pageContext.request.contextPath}/book/uploads enctypemultipart/form-datainput typefile namefiles multiplebutton typesubmit上传/button/form
3.运行结果