上传:

package com.yonyougov.dmp.amserver.utils;import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;/*** @Description TODO  文件上传工具类* @Author yitianRen* @Date 2019/10/10 10:51* @Version 1.0**/
public class FileUploadUtils {private static Logger logger = LoggerFactory.getLogger(FileDownloadUtils.class);/*** @Author: yitianRen* @Description:* @Date: 16:00 2019/10/10* @Param: file:文件 rootDir:上传跟路径* @Return:*/public static String upLoadFile(MultipartFile file, String rootDir) {logger.info("传入的文件参数:{}", JSON.toJSONString(file, true));if (Objects.isNull(file) || file.isEmpty()) {logger.info("文件为空");return "文件为空,请重新上传";}try {byte[] bytes = file.getBytes();Path path = Paths.get(rootDir + file.getOriginalFilename());//如果没有files文件夹,则创建if (!Files.isWritable(path)) {Files.createDirectories(Paths.get(rootDir));}//文件写入指定路径Files.write(path, bytes);logger.debug("文件写入成功...");return "文件上传成功";} catch (IOException e) {e.printStackTrace();return "后端异常...";}}}

下载:

package com.yonyougov.dmp.amserver.utils;import javax.servlet.http.HttpServletResponse;
import java.io.*;/*** @Description TODO 下载文件工具类* @Author yitianRen* @Date 2019/10/9 10:03* @Version 1.0**/
public class FileDownloadUtils {/*** @Author: yitianRen* @Description: 文件下载,输入文件名和文件路径* @Date: 14:03 2019/10/9* @Param:* @Return:*/public static void downloadFile(HttpServletResponse response, String fileName, String path){fileName="测试.txt";path="D:\\测试.txt";if (fileName != null) {//设置文件路径File file = new File(path);if (file.exists()) {response.setHeader("content-type", "application/octet-stream");response.setContentType("application/octet-stream");try {response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}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);}} 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();}}}}}}}

备注:springboot上传文件出现异常问题java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?  查询得知是配置文件问题,我在***Controller层加入一下代码解决:

@Beanpublic CommonsMultipartResolver multipartResolver() {CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();multipartResolver.setDefaultEncoding("UTF-8");multipartResolver.setMaxUploadSize(104857600);return multipartResolver;}

java文件上传下载工具类FileUtils相关推荐

  1. JAVA 文件上传下载工具类

    JAVA 文件上传下载工具类 import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org. ...

  2. 华为云OBS文件上传下载工具类

    Java-华为云OBS文件上传下载工具类 文章目录 Java-华为云OBS文件上传下载工具类 1.华为云obs文件上传下载 2.文件流转MultipartFile 3.File转换为Multipart ...

  3. springboot+minio文件上传下载工具类

    引入依赖 <dependency><groupId>io.minio</groupId><artifactId>minio</artifactId ...

  4. 阿里云OSS文件上传下载工具类

    引入依赖 <dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss ...

  5. springboot上传下载文件(4)--上传下载工具类(已封装)

    因为在做毕设,发现之前的搭建ftp文件服务器,通过ftp协议无法操作虚拟机临时文件,又因为ftp文件服务器搭建的比较麻烦:而 hadoop的HDFS虽然可以实现,但我这里用不到那么复杂的:所以我封装了 ...

  6. 2021-10-14 ContextType(MIME) 与 Java文件上传/下载

    ContextType(MIME) 与 Java文件上传/下载 ContextType(MIME) Text Image Audio Video Application Multipart 和 Mes ...

  7. Java文件上传下载

    文件上传下载 Java文件上传和下载对于刚接触Java没多久的老铁们来说可能是一个技术难点.如果看过我前两篇文章的老铁肯定就知道,这次肯定又是一个工具类,废话少说我们直接附上代码. package c ...

  8. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:宇的季节 cnblogs.com/chenkeyu/p/80 ...

  9. java 文件 上传 下载

    QuFileUtil web_uploader.html <!DOCTYPE html> <html lang="en"> <head> < ...

最新文章

  1. python找零钱_Python递归 - 找零钱
  2. 【NLP】到目前为止,机器学习与自然语言处理相遇的那些事
  3. 在MacOS下使用Fiddler抓包
  4. 如何理解 SAP UI5 的 sap.ui.define 函数
  5. Java笔记-多线程协调及ReentrantLock的使用
  6. 从SQL过渡至MongoDB查询对照表
  7. winfrom下,如何实现类似QQ右下角弹出新闻
  8. python窗体应用程序无阻塞_当GUI应用程序运行时,imshow阻塞线程
  9. matlab深度DoF图像修复,windows10系统运行dnf提示client.exe损坏的图像解决方法
  10. 软件设计模式学习笔记(八)
  11. 九款个人知识管理工具介绍
  12. python字符串、数字
  13. FreeSurfer和FSL的安装和使用(脑部图像去除头骨+对图像和label同时进行仿射对齐)教程
  14. 如何把img格式转换成vmdk格式
  15. Web3对于我们普通人意味着什么?
  16. 【字符串】字符串逆序
  17. 上海社保金额精确计算公式
  18. 前端项目开发中碰到的坑、移动端兼容性问题
  19. matlab axis坐标轴相关设置详解
  20. scipy更新后imread,imresize函数被启用,替换方案

热门文章

  1. 网站顶部的跑马灯特效代码
  2. Matlab简易版美图秀秀(GUI界面实现)
  3. enicode字体反爬,大厂使用的反爬技术,结合OCR处理页面源代码
  4. opera收银学习(一)
  5. ubuntu20.04下安装网易云音乐,打开网易云音乐出现无法连接网络,播放不了音乐,不知道什么原因?????
  6. matlab实现低通、高通、带通、带阻滤波
  7. 完整实例实现QQ表情的发送和接收
  8. [Depricated]适用coremail邮件系统,第三方客户端绑定校园邮箱(南邮、河海,以iOS邮件为例)
  9. Vue详解及综合案例
  10. stm32实现呼吸灯