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("文件读取异常");
}
}
}
}
springMVC文件下载工具类
最新推荐文章于 2024-07-12 17:31:53 发布