目录
上传到指定路径
一、代码层级结构
二、文件上传接口
三、使用postman进行测试;
MultipartFile接收前端传递的文件:127.0.0.1:8082/path/uploadFile
part接收前端传递的文件:127.0.0.1:8082/path/uploadFileByRequest
接收前端传递binary 类型:127.0.0.1:8082/path/upload2
四、springboot 配置
五、文件下载接口
六、使用postman进行测试
上传至数据库
一、代码层级结构
二、文件上传接口
三、文件下载接口
四、使用postman进行测试
上传至Minio
一、代码层级结构
二、文件上传/下载接口
MinioUtils
三、使用postman进行测试
业务分析:实际开发过程中,我们经常对文件的上传和下载的功能实现,所以这也是一个程序员应该掌握的基本开发能力。所以下面我给大家分享一下文件上传和下载的三种方式,分别将文件上传到指定路径下/本地数据库/minio当中。
所有代码都以上传至压缩包资源,可以自行免费进行下载测试;
上传到指定路径
一、代码层级结构
二、文件上传接口
/*** 上传到指定路径下*/
@RestController
@Slf4j
@RequestMapping("/path")
public class UploadController {@Autowiredprivate ResourceLoader resourceLoader;private BufferedOutputStream bufferedOutputStream = null;/*** form-data 类型* form-data 类型即常用的表单提交* 两种处理参数的方式* <p>* MultipartFile 类接受前台传过来的文件* part 接收字节流*/@PostMapping("/uploadFile")public String uploadFile(@RequestParam("name") String name, @RequestPart("file1") MultipartFile file1, @RequestPart("file2") MultipartFile[] file2) throws IOException, ServletException {// 获取项目部署路径/* String appPath = request.getServletContext().getRealPath("/");// 构建上传文件的目录路径String path = appPath + "static/upload/";*///绝对路劲
// String path = "D:\\Users\\MXIN\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\";//相对路径String path = "src/main/resources/static/";//前端传递多个file(只对file2进行处理)for (MultipartFile multipartFile : file2) {
// 使用 MultipartFile 字节流保存文件fileUtil(multipartFile, String.valueOf(path));}fileUtil(file1, String.valueOf(path));return "success";}/*** part 接收字节流*/@PostMapping("/uploadFileByRequest")public String uploadFileByRequest(HttpServletRequest request) throws IOException, ServletException {// 获取项目部署路径/* String appPath = request.getServletContext().getRealPath("/");// 构建上传文件的目录路径String path = appPath + "static/upload/";*///绝对路劲String path = "D:\\Users\\MXIN\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\";// 使用 Part 接收文件字节流
// Part file1 = request.getPart("file1");
// file1.write(path + file1.getSubmittedFileName());// request.getParts() 获取的是全部参数(name,age,file1,file2),包括文件参数和非文件参数for (Part part : request.getParts()) {// 获取文件类型String contentType = part.getContentType();// 获取文件大小long size = part.getSize();// 获取文件名String submittedFileName = part.getSubmittedFileName();if (part.getContentType() != null) {//如果是文件会进行写入part.write(path + part.getSubmittedFileName());} else {// 如果是参数会获取参数(根据实际需求对参数进行处理)// 获取参数名 String name1 = part.getName();}}return "success";}public String fileUtil(MultipartFile file, String path) {if (!file.isEmpty()) {try {byte[] bytes = file.getBytes();bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path + file.getOriginalFilename())));bufferedOutputStream.write(bytes);bufferedOutputStream.close();return file.getOriginalFilename() + "上传成功";} catch (Exception e) {return file.getOriginalFilename() + "上传失败,错误信息为:" + e;}} else {return "上传得文件为空";}}}
三、使用postman进行测试;
MultipartFile接收前端传递的文件:127.0.0.1:8082/path/uploadFile
测试结果
part接收前端传递的文件:127.0.0.1:8082/path/uploadFileByRequest
测试结果
四、上传文件大小设置
由于springboot默认上传文件大小为1M,单个请求最大为10M,当文件超过1M会报错。所以可以通过配置文件限制文件上传大小。
上传大于1M报错信息为:
Resolved [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded]
所以修改配置文件(根据自己实际开发需求进行修改):
上述简单的介绍了文件上传到指定路径下的方法,下面来简单介绍一下文件下载是如何实现的。
五、文件下载接口
/*** 下载指定路径下的文件* */
@RestController
@Slf4j
@RequestMapping("/path")
public class DownloadController {/*** 文件下载 isOnline默认为false*/@GetMapping("/download")public void download(String fileName, HttpServletResponse response, boolean isOnLine) throws IOException {// 路径可以指定当前项目相对路径File file = new File("D:\\Users\\Mixi\\IdeaProjects\\springboot-uploadanddownload\\src\\main\\resources\\static\\" + fileName);if (file.exists()) {FileInputStream fileInputStream = new FileInputStream(file);ServletOutputStream outputStream = response.getOutputStream();// 获取文件扩展名String extension = fileName.substring(fileName.lastIndexOf(".") + 1);if (!isOnLine) {// 根据文件扩展名设置Content-TypeString contentType = getContentType(extension);response.setContentType(contentType);// 如果文件名为中文需要设置编码response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "utf8"));// 返回前端文件名需要添加response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");}byte[] bytes = new byte[1024];int len;while ((len = fileInputStream.read(bytes)) != -1) {outputStream.write(bytes, 0, len);}}}// 根据文件扩展名获取Content-Typeprivate String getContentType(String extension) {if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {return "image/jpeg";} else if ("png".equalsIgnoreCase(extension)) {return "image/png";} else if ("gif".equalsIgnoreCase(extension)) {return "image/gif";} else if ("txt".equalsIgnoreCase(extension)) {return "text/plain";} else if ("pdf".equalsIgnoreCase(extension)) {return "application/pdf";} else if ("doc".equalsIgnoreCase(extension) || "docx".equalsIgnoreCase(extension)) {return "application/msword";} else if ("xls".equalsIgnoreCase(extension) || "xlsx".equalsIgnoreCase(extension)) {return "application/vnd.ms-excel";} else if ("ppt".equalsIgnoreCase(extension) || "pptx".equalsIgnoreCase(extension)) {return "application/vnd.ms-powerpoint";} else if ("zip".equalsIgnoreCase(extension)) {return "application/zip";} else if ("tar".equalsIgnoreCase(extension)) {return "application/x-tar";} else if ("rar".equalsIgnoreCase(extension)) {return "application/x-rar-compressed";} else if ("gz".equalsIgnoreCase(extension)) {return "application/gzip";} else {return "application/octet-stream";}}
}
六、使用postman进行测试
isOnlie为true和false决定了是否在浏览器上在线查看。
上传至数据库
这种业务需求就是用于项目开发过程中,文件上传下载的功能用的很少,避免搭建部署文件存储的服务器,简化了部署和管理,节约成本资源。
一、代码层级结构
二、文件上传接口
UploadToDBController:
/*** 上传到数据库* */
@RestController
@Slf4j
@RequestMapping("/db")
public class UploadToDBController {@Autowiredprivate FilesService filesService;@PostMapping("/uploadFile")public Files inputFile(@RequestParam("file") MultipartFile file) {Files files = new Files();if (null != file) {String name = file.getOriginalFilename();byte[] bytes;try {bytes = file.getBytes();} catch (IOException e) {throw new RuntimeException(e);}files.setName(name);files.setFile(bytes);filesService.save(files);}return files;}
}
FilesServiceImpl:
@Overridepublic void save(Files files) {filesMapper.insert(files);}
简单代码的增删改查我就不写了哈。
三、文件下载接口
DownloadFromDBController:
/*** 从数据库下载*/
@RestController
@Slf4j
@RequestMapping("/db")
public class DownloadFromDBController {@Autowiredprivate FilesService filesService;@GetMapping("/download/{id}")public void download(HttpServletResponse response, @PathVariable("id") Integer id) {filesService.download(response,id);}
}
FilesServiceImpl:
@Overridepublic HttpServletResponse download(HttpServletResponse response, Integer id) {try {byte[] buf;Files byId = filesMapper.selectById(id);if (byId == null) {return response;}buf = byId.getFile();String suffix = FileToMultipartFile.getSuffix(byId.getName());String contentType = "";switch (Objects.requireNonNull(FileTypeEnum.getEnum(suffix))) {case DOC:contentType = "application/msword";break;case DOCX:contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";break;case PDF:contentType = "application/powerpoint";break;case JPE:case JPG:case JPEG:contentType = "image/jpeg";break;case PNG:contentType = "image/png";break;case ZIP:contentType = "application/zip";break;case TAR:contentType = "application/x-tar";break;case GZ:contentType = "application/x-gzip";break;case XLS:contentType = "application/vnd.ms-excel";break;case XLSX:contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";break;case PPT:contentType = "application/vnd.ms-powerpoint";break;case PPTX:contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";break;}// 清空response.reset();String encodedFileName = URLEncoder.encode(byId.getName().replaceAll(" ", "+"), "UTF-8");// 设置headerresponse.addHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
// response.addHeader("Content-Length", "" + byId.getFileNo());response.setCharacterEncoding("UTF-8");response.setContentType(contentType);response.getOutputStream().write(buf);// 强制输出,不然会留在内存里丢失掉response.getOutputStream().flush();return response;} catch (Exception e) {throw new RuntimeException(e);}}
四、使用postman进行测试
文件上传:
测试结果数据库:
文件下载:
测试结果:
上传至Minio
首先要在本地或者服务器上部署minio并启动才可以进行文件的上传和下载,还未安装minio的可以参考一下这篇文章:【Docker】手把手教你在windows使用Docker安装Minio[详细教程]_minio windows-CSDN博客
一、代码层级结构
二、文件上传/下载接口
/*** 文件上传、下载、删除、获取文件信息、获取文件url接口* */
@Slf4j
@RestController
@RequestMapping("/oss")
public class MinioController {@Autowiredprivate MinioUtils minioUtils;@Autowiredprivate MinioConfig minioConfig;/*** 文件上传** @param file*/@PostMapping("/upload")public String upload(@RequestParam("file") MultipartFile file) {try {//文件名String fileName = file.getOriginalFilename();String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");//类型String contentType = file.getContentType();minioUtils.uploadFile(minioConfig.getBucketName(), file, newFileName, contentType);return "上传成功,文件名:" + newFileName;} catch (Exception e) {e.printStackTrace();return "上传失败";}}/*** 删除** @param fileName*/@DeleteMapping("/")public void delete(@RequestParam("fileName") String fileName) {minioUtils.removeFile(minioConfig.getBucketName(), fileName);}/*** 获取文件信息** @param fileName* @return*/@GetMapping("/info")public String getFileStatusInfo(@RequestParam("fileName") String fileName) {return minioUtils.getFileStatusInfo(minioConfig.getBucketName(), fileName);}/*** 获取文件外链** @param fileName* @return*/@GetMapping("/url")public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {return minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);}/*** 文件下载** @param fileName* @param response*/@GetMapping("/download")public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {try {InputStream fileInputStream = minioUtils.getObject(minioConfig.getBucketName(), fileName);response.setHeader("Content-Disposition", "attachment;filename=" + fileName);response.setContentType("application/force-download");response.setCharacterEncoding("UTF-8");IOUtils.copy(fileInputStream, response.getOutputStream());} catch (Exception e) {log.error("下载失败");}}
}
MinioUtils
@Slf4j
@Component
public class MinioUtils {@Autowiredprivate MinioClient minioClient;/*** 启动SpringBoot容器的时候初始化Bucket* 如果没有Bucket则创建** @param bucketName*/public void createBucket(String bucketName) {try {if (!bucketExists(bucketName)) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());log.info("创建bucketName = {}完成!", bucketName);return;}log.info("bucketName = {}已存在!策略为:{}", bucketName, getBucketPolicy(bucketName));} catch (Exception e) {log.error("创建bucketName = {}异常!e = {}", bucketName, e);}}/*** 判断Bucket是否存在,true:存在,false:不存在** @param bucketName* @return*/@SneakyThrowspublic boolean bucketExists(String bucketName) {return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());}/*** 获得Bucket的策略** @param bucketName* @return*/@SneakyThrowspublic String getBucketPolicy(String bucketName) {return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());}/*** 获得所有Bucket列表** @return*/@SneakyThrowspublic List<Bucket> getAllBuckets() {return minioClient.listBuckets();}/*** 根据bucketName获取其相关信息** @param bucketName* @return*/@SneakyThrows(Exception.class)public Optional<Bucket> getBucket(String bucketName) {return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();}/*** 根据bucketName删除Bucket,true:删除成功; false:删除失败,文件或已不存在** @param bucketName* @throws Exception*/@SneakyThrows(Exception.class)public void removeBucket(String bucketName) {minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());}/*** 判断文件是否存在** @param bucketName* @param objectName* @return*/public boolean isObjectExist(String bucketName, String objectName) {boolean exist = true;try {minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());} catch (Exception e) {log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);exist = false;}return exist;}/*** 判断文件夹是否存在** @param bucketName* @param objectName* @return*/public boolean isFolderExist(String bucketName, String objectName) {boolean exist = false;try {Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());for (Result<Item> result : results) {Item item = result.get();if (item.isDir() && objectName.equals(item.objectName())) {exist = true;}}} catch (Exception e) {log.error("[Minio工具类]>>>> 判断文件夹是否存在,异常:", e);exist = false;}return exist;}/*** 根据文件前置查询文件** @param bucketName 存储桶* @param prefix 前缀* @param recursive 是否使用递归查询* @return MinioItem 列表*/@SneakyThrows(Exception.class)public List<Item> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {List<Item> list = new ArrayList<>();Iterable<Result<Item>> objectsIterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());if (objectsIterator != null) {for (Result<Item> o : objectsIterator) {Item item = o.get();list.add(item);}}return list;}/*** 获取文件流** @param bucketName 存储桶* @param objectName 文件名* @return 二进制流*/@SneakyThrows(Exception.class)public InputStream getObject(String bucketName, String objectName) {return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());}/*** 断点下载** @param bucketName 存储桶* @param objectName 文件名称* @param offset 起始字节的位置* @param length 要读取的长度* @return 二进制流*/@SneakyThrows(Exception.class)public InputStream getObject(String bucketName, String objectName, long offset, long length) {return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build());}/*** 获取路径下文件列表** @param bucketName 存储桶* @param prefix 文件名称* @param recursive 是否递归查找,false:模拟文件夹结构查找* @return 二进制流*/public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive) {return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());}/*** 使用MultipartFile进行文件上传** @param bucketName 存储桶* @param file 文件名* @param objectName 对象名* @param contentType 类型* @return*/@SneakyThrows(Exception.class)public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) {InputStream inputStream = file.getInputStream();return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(inputStream, inputStream.available(), -1).build());}/*** 图片上传** @param bucketName* @param imageBase64* @param imageName* @return*/public ObjectWriteResponse uploadImage(String bucketName, String imageBase64, String imageName) {if (!StringUtils.isEmpty(imageBase64)) {InputStream in = base64ToInputStream(imageBase64);String newName = System.currentTimeMillis() + "_" + imageName + ".jpg";String year = String.valueOf(new Date().getYear());String month = String.valueOf(new Date().getMonth());return uploadFile(bucketName, year + "/" + month + "/" + newName, in);}return null;}public static InputStream base64ToInputStream(String base64) {ByteArrayInputStream stream = null;try {byte[] bytes = Base64.getEncoder().encode(base64.trim().getBytes());stream = new ByteArrayInputStream(bytes);} catch (Exception e) {e.printStackTrace();}return stream;}/*** 上传本地文件** @param bucketName 存储桶* @param objectName 对象名称* @param fileName 本地文件路径* @return*/@SneakyThrows(Exception.class)public ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) {return minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());}/*** 通过流上传文件** @param bucketName 存储桶* @param objectName 文件对象* @param inputStream 文件流* @return*/@SneakyThrows(Exception.class)public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) {return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).build());}/*** 创建文件夹或目录** @param bucketName 存储桶* @param objectName 目录路径* @return*/@SneakyThrows(Exception.class)public ObjectWriteResponse createDir(String bucketName, String objectName) {return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build());}/*** 获取文件信息, 如果抛出异常则说明文件不存在** @param bucketName 存储桶* @param objectName 文件名称* @return*/@SneakyThrows(Exception.class)public String getFileStatusInfo(String bucketName, String objectName) {return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()).toString();}/*** 拷贝文件** @param bucketName 存储桶* @param objectName 文件名* @param srcBucketName 目标存储桶* @param srcObjectName 目标文件名*/@SneakyThrows(Exception.class)public ObjectWriteResponse copyFile(String bucketName, String objectName, String srcBucketName, String srcObjectName) {return minioClient.copyObject(CopyObjectArgs.builder().source(CopySource.builder().bucket(bucketName).object(objectName).build()).bucket(srcBucketName).object(srcObjectName).build());}/*** 删除文件** @param bucketName 存储桶* @param objectName 文件名称*/@SneakyThrows(Exception.class)public void removeFile(String bucketName, String objectName) {minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());}/*** 批量删除文件** @param bucketName 存储桶* @param keys 需要删除的文件列表* @return*/public void removeFiles(String bucketName, List<String> keys) {List<DeleteObject> objects = new LinkedList<>();keys.forEach(s -> {objects.add(new DeleteObject(s));try {removeFile(bucketName, s);} catch (Exception e) {log.error("[Minio工具类]>>>> 批量删除文件,异常:", e);}});}/*** 获取文件外链** @param bucketName 存储桶* @param objectName 文件名* @param expires 过期时间 <=7 秒 (外链有效时间(单位:秒))* @return url*/@SneakyThrows(Exception.class)public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();return minioClient.getPresignedObjectUrl(args);}/*** 获得文件外链** @param bucketName* @param objectName* @return url*/@SneakyThrows(Exception.class)public String getPresignedObjectUrl(String bucketName, String objectName) {GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();return minioClient.getPresignedObjectUrl(args);}/*** 将URLDecoder编码转成UTF8** @param str* @return* @throws UnsupportedEncodingException*/public String getUtf8ByURLDecoder(String str) throws UnsupportedEncodingException {String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");return URLDecoder.decode(url, "UTF-8");}
}
三、使用postman进行测试
文件上传到minio
测试结果:
从minio中下载文件
直接下载即可,这样就完成了从minio中下载指定文件,还有部分接口,如果感兴趣的小伙伴可以自行测试看下实际效果。
综上所有的代码就简单的介绍了一下Java实现文件上传和下载,希望能给你们实际开发带来一定的帮助,如果有出现问题的地方,希望指出,便于后续修改。
所有源码均以上传至https://download.csdn.net/download/m0_64210833/89233948