java批量上传文件_Spring boot 实现单个或批量文件上传功能
一:添加依赖:
org.springframework.boot
spring-boot-starter-thymeleaf
javax.servlet
jstl
org.apache.tomcat.embed
tomcat-embed-jasper
二:application.xml配置文件路径:
#配置上传文件地址
image.location.path=f:/image/
#配置文件大小限制
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
#静态页面的访问配置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
三:编写静态页面(src/main/resources下建文件夹static(static存放静态文件,比如 css、js、image…)和templates(存放静态页面)两个是同级目录),先在templates 中新建一个 uploadimg.html。
uploadimg.html
图片
四:编写Controller层:
package com.hot.analysis.controller.file;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.hot.analysis.exception.MyException;
@RestController
public class FileUploadController {
//获取配置文件的路径
@Value("${image.location.path}")
private String resourceDir;
/**
* 实现文件上传
* */
@RequestMapping(value = "/index")
public ModelAndView toIndex() {
ModelAndView mv = new ModelAndView("uploadimg");
return mv;
}
//单个文件上传
@RequestMapping("/dc/fileUpload")
@ResponseBody
public String fileUpload( MultipartFile file){
// 获取上传文件路径
String uploadPath = file.getOriginalFilename();
// 获取上传文件的后缀
String fileSuffix = uploadPath.substring(uploadPath.lastIndexOf(".") + 1, uploadPath.length());
if (fileSuffix.equals("apk")) {
uploadPath = resourceDir;
} else {
// 上传目录地址
// String uploadpath="E:/hot-manage/image/";//windows路径
uploadPath =resourceDir;// liux路劲
}
// 上传文件名
String fileName = new Date().getTime() + new Random().nextInt(100) + "." + fileSuffix;
File savefile = new File(uploadPath + fileName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
try {
file.transferTo(savefile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (fileSuffix.equals("apk")) {
return "/apk/" + fileName;
} else {
return "/image/" + fileName;
}
}
// 批量上传
@PostMapping("/dc/moreFileUpload")
public String bacthFileUpload(MultipartFile[] file) throws MyException {
StringBuffer buffer = new StringBuffer();
for (MultipartFile multipartFile : file) {
String str = fileUpload(multipartFile);
buffer.append(str);
buffer.append(",");
}
String all = buffer.substring(0, buffer.length() - 1);
return all;
}
// 删除文件
@PostMapping("/dc/deleteFile")
public String delFile(String path) {
String resultInfo = null;
int lastIndexOf = path.lastIndexOf("/");
String sb = path.substring(lastIndexOf + 1, path.length());
sb = "f:/image/" + sb;
File file = new File(sb);
if (file.exists()) {
if (file.delete()) {
resultInfo = "1-删除成功";
} else {
resultInfo = "0-删除失败";
}
} else {
resultInfo = "文件不存在!";
}
return resultInfo;
}
//文件下载相关代码
@RequestMapping("/download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "aim_test.txt";// 设置文件名,根据业务需要替换成要下载的文件名
if (fileName != null) {
//设置文件路径
String realPath = "D://aim//";
File file = new File(realPath , fileName);
if (file.exists()) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
}
测试:
成功返回路径:
查看文件夹:
总结
以上所述是小编给大家介绍的Spring boot 实现单个或批量文件上传功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
java批量上传文件_Spring boot 实现单个或批量文件上传功能相关推荐
- java批量上传文件_Spring Boot2(十四):单文件上传/下载,文件批量上传
文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...
- .vue文件_Spring Boot 2.x(十六):玩转vue文件上传
为什么使用Vue-Simple-Uploader 最近用到了Vue + Spring Boot来完成文件上传的操作,踩了一些坑,对比了一些Vue的组件,发现了一个很好用的组件--Vue-Simple- ...
- .vue文件_Spring Boot + Vue 前后端分离,两种文件上传方式总结!
在Vue.js 中,如果网络请求使用 axios ,并且使用了 ElementUI 库,那么一般来说,文件上传有两种不同的实现方案: 通过 Ajax 实现文件上传 通过 ElementUI 里边的 U ...
- springboot上传文件同时传参数_Spring Boot 系列:使用 Spring Boot 上传文件
上传文件是互联网中常常应用的场景之一,最典型的情况就是上传头像等,今天就带着带着大家做一个 Spring Boot 上传文件的小案例. 1.pom 包配置 我们使用 Spring Boot 版本 2. ...
- springboot传入json和文件_Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)...
server: port: 8088 servlet: context-path: /sid spring: mvc: view: prefix: / suffix: .html /** * 返回界面 ...
- 打包tomcat没有xml文件_Spring Boot 项目打包 War 并部署到 Tomcat
之前使用 SpringBoot 所做的开发,都是将项目打包成 Jar 的,但是如果是作为一个 Web 项目,最好是将项目打包成 War. 1. Jar 包和 War 包的区别 1.概念 jar 包:J ...
- java restful接口开发实例_Spring Boot 中 10 行代码构建 RESTful 风格应用!
点击上方"Java后端技术",选择"置顶或者星标" 你关注的就是我关心的! 作者:江南一点雨 微信公众号:牧码小子(ID:a_javaboy) 推荐阅读:10个 ...
- java前后端分离框架_Spring Boot 入门及前后端分离项目实践
本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 个部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 项目实践开发.Spring Boot ...
- springboot读取linux文件_spring\-boot以jar包方式时读取resource或是template文件 | Prayer's Laputa...
现象 以jar包方式部署系统,想读取resource或是template下面的文件时,报 File Not Found 我遇到的情况是,整个项目达成了一个包,在开发环境(windows + idea) ...
最新文章
- 为什么String中的Java hashCode()使用31作为乘数?
- Web页面布局方式小结
- 云炬Android开发报错处理教程 完美解决Android Studio maven { url ‘https://jitpack.io‘ } 无法下载问题
- mysql数据库5.7配置文件_MySQL 5.7配置文件参考
- Chirpy Zippy工具使用心得
- ES6学习(五)—数组的扩展
- 计算机验收标准和验收方法,【超详细】综合布线系统验收标准及内容
- ELK pipeline
- 对Java中字符串的进一步理解
- 流水线作业调度问题-动态规划(运用Johnson算法)
- 机器学习中最常使用的10种数据编码方式
- android 找不到 theme,android-找不到与给定名称'@ style / Theme.Holo.Light.DarkActionBar'匹配的资源...
- windowsPE系统的制作
- 故宫景点功课10:后三宫区(中)
- 【C++决赛】2019年全国高校计算机能力挑战赛决赛C++组题解
- 南通大学杏林学院计算机专业,南通大学杏林学院代码
- android模拟器没反应,Android模拟器无法正常工作
- npm run build: rimraf: command not found
- (有趣)把文字隐藏到图片中
- 微信小程序之picker选择器获取值得两种方法