• Spring之文件上传下载,jrebel,多文件上传


    • 文件上传,文件下载
    • jrebel&多文件上传

    1.文件上传,文件下载

    文件上传

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

    2.前端标记表单为多功能表单enctype=”mutipart/form-data“

    3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件

    4.将文件转成流,然后写到服务器(某一个硬盘)

    5.做硬盘于网络地址的映射(服务器配置)

    1. package com.zlj.web;
    2. import com.zlj.biz.StuBiz;
    3. import com.zlj.model.Stu;
    4. import com.zlj.util.PageBean;
    5. import com.zlj.util.PropertiesUtil;
    6. import org.apache.commons.io.FileUtils;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.http.HttpHeaders;
    9. import org.springframework.http.HttpStatus;
    10. import org.springframework.http.MediaType;
    11. import org.springframework.http.ResponseEntity;
    12. import org.springframework.stereotype.Controller;
    13. import org.springframework.ui.Model;
    14. import org.springframework.web.bind.annotation.PathVariable;
    15. import org.springframework.web.bind.annotation.RequestMapping;
    16. import org.springframework.web.multipart.MultipartFile;
    17. import javax.servlet.http.HttpServletRequest;
    18. import java.io.File;
    19. import java.io.IOException;
    20. import java.util.List;
    21. /**
    22. * @author zlj
    23. * @create 2023-09-08 16:55
    24. */
    25. @Controller
    26. @RequestMapping("stu")
    27. public class StuController {
    28. @Autowired
    29. private StuBiz stuBiz;
    30. // 增
    31. @RequestMapping("/add")
    32. public String add(Stu stu,HttpServletRequest request) {
    33. int i = stuBiz.insert(stu);
    34. return "redirect:list";
    35. }
    36. // 删
    37. @RequestMapping("/del/{sid}")
    38. public String del(@PathVariable("sid") Integer sid) {
    39. stuBiz.deleteByPrimaryKey(sid);
    40. return "redirect:/stu/list";
    41. }
    42. // 改
    43. @RequestMapping("/edit")
    44. public String edit(Stu stu) {
    45. stuBiz.updateByPrimaryKeySelective(stu);
    46. return "redirect:list";
    47. }
    48. //文件上传
    49. @RequestMapping("/upload")
    50. public String upload(Stu stu,MultipartFile xxx){
    51. try {
    52. // 3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
    53. // 4.将文件转成流,然后写到服务器(某一个硬盘)
    54. // 上传的图片真实的地址
    55. String dir= PropertiesUtil.getValue("dir");
    56. // 网络访问的地址
    57. String server=PropertiesUtil.getValue("server");;
    58. //文件名
    59. String filename=xxx.getOriginalFilename();
    60. System.out.println("文件名"+filename);
    61. System.out.println("文件类别"+xxx.getContentType());
    62. FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
    63. // /upload/0703.png
    64. stu.setSpic(server+filename);
    65. stuBiz.updateByPrimaryKeySelective(stu);
    66. } catch (IOException e) {
    67. e.printStackTrace();
    68. }
    69. return "redirect:list";
    70. }
    71. // 查
    72. @RequestMapping("/list")
    73. public String list(Stu stu, HttpServletRequest request) {
    74. //stu是用来接收前台传递后台的参数
    75. PageBean pageBean = new PageBean();
    76. pageBean.setRequest(request);
    77. List stus = stuBiz.ListPager(stu, pageBean);
    78. request.setAttribute("lst", stus);
    79. request.setAttribute("pageBean", pageBean);
    80. // WEB-INF/jsp/stu/list.jsp
    81. return "stu/list";
    82. }
    83. @RequestMapping(value="/download")
    84. public ResponseEntity<byte[]> download(Stu stu,HttpServletRequest req){
    85. try {
    86. //先根据文件id查询对应图片信息
    87. Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
    88. String diskPath = PropertiesUtil.getValue("dir");
    89. String reqPath = PropertiesUtil.getValue("server");
    90. // /upload/0703.png -->D:/temp/upload/0703.png
    91. String realPath = stus.getSpic().replace(reqPath,diskPath);
    92. String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
    93. //下载关键代码
    94. File file=new File(realPath);
    95. HttpHeaders headers = new HttpHeaders();//http头信息
    96. String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
    97. headers.setContentDispositionFormData("attachment", downloadFileName);
    98. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    99. //MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
    100. return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
    101. }catch (Exception e){
    102. e.printStackTrace();
    103. }
    104. return null;
    105. }
    106. //数据回显
    107. @RequestMapping("/preSave")
    108. public String preSave(Stu stu, Model model) {
    109. if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
    110. Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
    111. model.addAttribute("s", s);
    112. }
    113. return "stu/edit";
    114. }
    115. }
    1. //list.jsp
    2. <%@ page language="java" pageEncoding="UTF-8"%>
    3. <%@include file="/common/header.jsp"%>
    4. "Content-Type" content="text/html; charset=UTF-8">
    5. 学生信息
    6. "form-inline"
    7. action="${ctx}/stu/list" method="post">
    8. "form-group mb-2">
    9. "text" class="form-control-plaintext" name="same"
    10. placeholder="请输入学生姓名">
    11. <%-- "pagination" value="false" type="hidden">--%>
  • "table table-striped">
  • var="s" items="${lst }">
  • "col">学生id"col">学生姓名"col">学生年龄"col">学生图片"col">操作
    ${s.sid }${s.same }${s.sage }
  • "${s.spic}" style="height:100px;width:60px;">
  • "${pageBean }">
    1. //upload.jsp
    2. <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    3. <%@include file="/common/header.jsp"%>
    4. 学生log上传
    5. "${ctx}/stu/upload" method="post" enctype="multipart/form-data">
    6. "text" name="sid" readonly="readonly" value="${param.sid}"/>
    7. "file" name="xxx"/>
    8. "submit" value="上传图片"/>

    2.jrebel&多文件上传

    jrebel使用

    1下载插件。
    2.下载后,打开IDEA,选择File—>Settings—>Plugins—>设置按钮—>Installed Plugin from Disk(从文件夹选择已下载的插件安装)。

    3.重启IDEA
    4.选择File—>Settings—>JRebel & XRebel—>Change license

    5.安装JRebel插件后,注册地址填写网址 + 生成的GUID,邮箱随便填写,然后即可。
    http://jrebel-license.jiweichengzhu.com/{GUID}
    https://jrebel.qekang.com/{GUID}
    GUID可以使用在线GUID在线生成在线生成,然后替换{GUID}就行。
    6.下面邮箱地址可随便输入。
    7.选择我同意
    8.提交

    多文件上传

    1. <%@ page contentType="text/html; charset=UTF-8" language="java" %>
    2. <%@include file="/common/header.jsp"%>
    3. 学生log上传
    4. "${ctx}/stu/upload" method="post" enctype="multipart/form-data">
    5. "text" name="sid" readonly="readonly" value="${param.sid}"/>
    6. "file" name="xxx"/>
    7. "submit" value="上传图片"/>
    8. "post" action="${ctx}/stu/uploads" enctype="multipart/form-data">
    9. "file" name="files" multiple>
    1. package com.zlj.web;
    2. import com.zlj.biz.StuBiz;
    3. import com.zlj.model.Stu;
    4. import com.zlj.util.PageBean;
    5. import com.zlj.util.PropertiesUtil;
    6. import org.apache.commons.io.FileUtils;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.http.HttpHeaders;
    9. import org.springframework.http.HttpStatus;
    10. import org.springframework.http.MediaType;
    11. import org.springframework.http.ResponseEntity;
    12. import org.springframework.stereotype.Controller;
    13. import org.springframework.ui.Model;
    14. import org.springframework.web.bind.annotation.PathVariable;
    15. import org.springframework.web.bind.annotation.RequestMapping;
    16. import org.springframework.web.multipart.MultipartFile;
    17. import javax.servlet.http.HttpServletRequest;
    18. import java.io.File;
    19. import java.io.IOException;
    20. import java.util.List;
    21. /**
    22. * @author zlj
    23. * @create 2023-09-08 16:55
    24. */
    25. @Controller
    26. @RequestMapping("stu")
    27. public class StuController {
    28. @Autowired
    29. private StuBiz stuBiz;
    30. // 增
    31. @RequestMapping("/add")
    32. public String add(Stu stu,HttpServletRequest request) {
    33. int i = stuBiz.insert(stu);
    34. return "redirect:list";
    35. }
    36. // 删
    37. @RequestMapping("/del/{sid}")
    38. public String del(@PathVariable("sid") Integer sid) {
    39. stuBiz.deleteByPrimaryKey(sid);
    40. return "redirect:/stu/list";
    41. }
    42. // 改
    43. @RequestMapping("/edit")
    44. public String edit(Stu stu) {
    45. stuBiz.updateByPrimaryKeySelective(stu);
    46. return "redirect:list";
    47. }
    48. //文件上传
    49. @RequestMapping("/upload")
    50. public String upload(Stu stu,MultipartFile xxx){
    51. try {
    52. // 3.后端可以直接利用mutipartFile类,接受前端传递到后台的文件
    53. // 4.将文件转成流,然后写到服务器(某一个硬盘)
    54. // 上传的图片真实的地址
    55. String dir= PropertiesUtil.getValue("dir");
    56. // 网络访问的地址
    57. String server=PropertiesUtil.getValue("server");;
    58. //文件名
    59. String filename=xxx.getOriginalFilename();
    60. System.out.println("文件名"+filename);
    61. System.out.println("文件类别"+xxx.getContentType());
    62. FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
    63. // /upload/0703.png
    64. stu.setSpic(server+filename);
    65. stuBiz.updateByPrimaryKeySelective(stu);
    66. } catch (IOException e) {
    67. e.printStackTrace();
    68. }
    69. return "redirect:list";
    70. }
    71. // 查
    72. @RequestMapping("/list")
    73. public String list(Stu stu, HttpServletRequest request) {
    74. //stu是用来接收前台传递后台的参数
    75. PageBean pageBean = new PageBean();
    76. pageBean.setRequest(request);
    77. List stus = stuBiz.ListPager(stu, pageBean);
    78. request.setAttribute("lst", stus);
    79. request.setAttribute("pageBean", pageBean);
    80. // WEB-INF/jsp/stu/list.jsp
    81. return "stu/list";
    82. }
    83. //下载文件
    84. @RequestMapping(value="/download")
    85. public ResponseEntity<byte[]> download(Stu stu,HttpServletRequest req){
    86. try {
    87. //先根据文件id查询对应图片信息
    88. Stu stus = this.stuBiz.selectByPrimaryKey(stu.getSid());
    89. String diskPath = PropertiesUtil.getValue("dir");
    90. String reqPath = PropertiesUtil.getValue("server");
    91. // /upload/0703.png -->D:/temp/upload/0703.png
    92. String realPath = stus.getSpic().replace(reqPath,diskPath);
    93. String fileName = realPath.substring(realPath.lastIndexOf("/")+1);
    94. //下载关键代码
    95. File file=new File(realPath);
    96. HttpHeaders headers = new HttpHeaders();//http头信息
    97. String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码
    98. headers.setContentDispositionFormData("attachment", downloadFileName);
    99. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    100. //MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
    101. return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
    102. }catch (Exception e){
    103. e.printStackTrace();
    104. }
    105. return null;
    106. }
    107. //多文件上传
    108. @RequestMapping("/uploads")
    109. public String uploads(HttpServletRequest req, Stu stu, MultipartFile[] files){
    110. try {
    111. StringBuffer sb = new StringBuffer();
    112. for (MultipartFile file : files) {
    113. //思路:
    114. //1) 将上传图片保存到服务器中的指定位置
    115. String dir = PropertiesUtil.getValue("dir");
    116. String server = PropertiesUtil.getValue("server");
    117. String filename = file.getOriginalFilename();
    118. FileUtils.copyInputStreamToFile(file.getInputStream(),new File(dir+filename));
    119. sb.append(filename).append(",");
    120. }
    121. System.out.println(sb.toString());
    122. } catch (Exception e) {
    123. e.printStackTrace();
    124. }
    125. return "redirect:list";
    126. }
    127. //数据回显
    128. @RequestMapping("/preSave")
    129. public String preSave(Stu stu, Model model) {
    130. if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
    131. Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
    132. model.addAttribute("s", s);
    133. }
    134. return "stu/edit";
    135. }
    136. }

  • 相关阅读:
    00后测试员摸爬滚打近一年,为是否要转行或去学软件测试的学弟们总结出了以下走心建议
    DSPE-PEG-PDP,DSPE-PEG-OPSS,磷脂-聚乙二醇-巯基吡啶可减少肽的免疫原性
    c# Unity 获取时间戳
    OSIRISV4.1使用教程(最新可用版)
    基于stm32的智能药盒
    php魔术方法和反序列化漏洞
    Jmeter进阶使用指南-使用断言
    Telegram 正式引入国产小程序技术
    【shell】初识shell脚本语言
    基于Java+SpringBoot+Thymeleaf+Mysql在线购物网站商城系统设计实现
  • 原文地址:https://blog.csdn.net/weixin_73471776/article/details/132777241