springMVC文件下载工具类

package org.spring.springboot.util;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;


public class FileUtils
{
    private final static Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);

    public static void uploadFile(MultipartFile file, String newFilePath)
        throws RuntimeException
    {
        File newFile = new File(newFilePath);
        File parentFile = newFile.getParentFile();
        if (!parentFile.exists())
        {
            parentFile.mkdirs();
        }
        BufferedInputStream bin = null;// 输入流缓存流
        BufferedOutputStream bout = null;// 输出流缓存流
        try
        {
            //缓存流
            bin = new BufferedInputStream(file.getInputStream());
            bout = new BufferedOutputStream(new FileOutputStream(newFile));
            byte[] b = new byte[1024 * 5];// 缓存数组
            int len = 0;
            while ((len = bin.read(b)) != -1)
            {
                bout.write(b, 0, len);
                bout.flush();
            }
        }
        catch (FileNotFoundException e)
        {
            LOGGER.error("文件不存在", e);
            //这里抛出RuntimeException,实际时抛出自定义异常,方便在上一层捕捉自定义异常,对异常信息统一管理,并返会前台
            throw new RuntimeException("文件读取异常");
        }
        catch (IOException e)
        {
            LOGGER.error("上传文件IO异常", e);
            throw new RuntimeException("文件读取异常");
        }
        finally
        {
            try
            {
                // 关闭缓存流的时候会将输入输出流给关闭
                if (bin != null)
                {
                    bin.close();
                }
                if (bout != null)
                {
                    bout.close();
                }
            }
            catch (IOException e)
            {
                LOGGER.error("文件上传异常", e);
                throw new RuntimeException("文件读取异常");
            }
        }

    }
    
    public static void downloadFile(HttpServletResponse response, String filePath)
        throws RuntimeException
    {
        File file = new File(filePath);
        if(!file.exists()){
            throw new RuntimeException("文件不存在");
        }
        BufferedInputStream bin = null;// 输入流缓存流
        BufferedOutputStream bout = null;// 输出流缓存流
        try
        {
            //缓存流
            bin = new BufferedInputStream(new FileInputStream(file));
            bout = new BufferedOutputStream(response.getOutputStream());
            byte[] b = new byte[1024 * 5];// 缓存数组
            int len = 0;
            while ((len = bin.read(b)) != -1)
            {
                bout.write(b, 0, len);
                bout.flush();
            }
        }
        catch (FileNotFoundException e)
        {
            LOGGER.error("文件不存在", e);
            //这里抛出RuntimeException,实际时抛出自定义异常,方便在上一层捕捉自定义异常,对异常信息统一管理,并返会前台
            throw new RuntimeException("文件读取异常");
        }
        catch (IOException e)
        {
            LOGGER.error("上传文件IO异常", e);
            throw new RuntimeException("文件读取异常");
        }
        finally
        {
            try
            {
                // 关闭缓存流的时候会将输入输出流给关闭
                if (bin != null)
                {
                    bin.close();
                }
                if (bout != null)
                {
                    bout.close();
                }
            }
            catch (IOException e)
            {
                LOGGER.error("文件上传异常", e);
                throw new RuntimeException("文件读取异常");
            }
        }

    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值