springboot 上传文件
来源:互联网 发布:无缝拼图软件四宫格 编辑:程序博客网 时间:2024/05/01 11:09
//上传需要的依赖
<dependency>
<groupId>io.leopard</groupId>
<artifactId>leopard-upload</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
//实现添加用户的方法@Overridepublic void insert(User user, MultipartFile multipartFile, HttpServletRequest request) { //获取文件名 String fi = multipartFile.getOriginalFilename(); InputStream inputStream = null; try { inputStream = multipartFile.getInputStream(); } catch (IOException e) { e.printStackTrace(); } long millis = System.currentTimeMillis(); //拼接新的文件名 String newName = fi+millis; //定义物理路径 String wlpath="E:\\idea-kongjian\\cargo\\src\\main\\resources\\static"+newName; //临时路径 String pathRoot = request.getSession().getServletContext().getRealPath("/");//自动寻找target根目录 String lsPath = pathRoot+"\\images\\"+newName; //判断是否需要创建文件夹 //物理路径文件夹 File wlFile = new File("E:\\idea-kongjian\\cargo\\src\\main\\resources\\static"); //临时路径文件夹 File lsFile = new File(pathRoot+"\\images"); if(!wlFile.exists()) { wlFile.mkdir(); } if(!lsFile.exists()) { lsFile.mkdir(); } //执行读写操作 if(!multipartFile.isEmpty()) { //将文件放入物理路径 FileOutputStream wlFileOutputStream = null; try { wlFileOutputStream = new FileOutputStream(wlpath); } catch (FileNotFoundException e) { e.printStackTrace(); } //将文件放入临时路径 FileOutputStream lsFileOutputStream1 = null; try { lsFileOutputStream1 = new FileOutputStream(lsPath); } catch (FileNotFoundException e) { e.printStackTrace(); } int line = 0; try { while ((line = inputStream.read()) != -1) { //写入物理路径 wlFileOutputStream.write(line); //写入临时路径 lsFileOutputStream1.write(line); } } catch (IOException e) { e.printStackTrace(); } try { wlFileOutputStream.flush(); lsFileOutputStream1.flush(); lsFileOutputStream1.close(); wlFileOutputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } //逻辑路径 String ljPath = "../images/"+newName; user.setPortrait(ljPath); //调用方法 um.insert(user);}
需要注意的是:前台表单提交为post springboot 上传图片默认大小为2M
阅读全文
4 0
- SpringBoot文件上传
- springboot文件上传
- SpringBoot上传文件
- springboot 文件上传
- SpringBoot之文件上传
- springboot 文件上传
- SpringBoot实现文件上传
- springboot 上传文件
- Springboot 文件上传
- SpringBoot上传文件
- SpringBoot文件上传控制
- springboot上传文件
- springboot+文件上传和下载
- springboot实现多文件上传
- springboot 文件上传大小配置
- SpringBoot轻松实现文件上传
- 15.springboot实现文件上传
- SpringBoot文件上传和下载
- 11g Oracle Rac安装(基于linux6)
- 深入理解Redis(二)—单机数据库
- React Native与Android通信——Android calls JS(一)0.45
- 【云存储】对象存储管理与安全
- C++语法之异常机制
- springboot 上传文件
- BFC(2)——我已经把上次的忘了。。
- 判断ip是否能ping通
- ALEIYE 2.0发布 首创RET关键事件功能
- oracle随机数的用法:dbms_random.value()
- Apache Spark 2.2.0 中文文档
- webview最全面详解(一)了解官方文档
- CSS网页布局之传统解决方案与Flex大法
- 常见线程模型