Browse Source

优化下载文件

pull/17/MERGE
RuoYi 6 years ago
parent
commit
d5d7cd6592
  1. 90
      src/main/java/com/ruoyi/common/utils/FileUtils.java

90
src/main/java/com/ruoyi/common/utils/FileUtils.java

@ -0,0 +1,90 @@
package com.ruoyi.common.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
/**
* 文件处理工具类
*
* @author ruoyi
*/
public class FileUtils
{
/**
* 输出指定文件的byte数组
*
* @param filename 文件
* @return
*/
public static void writeBytes(String filePath, OutputStream os) throws IOException
{
FileInputStream fis = null;
try
{
File file = new File(filePath);
if (!file.exists())
{
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
byte[] b = new byte[1024];
int length;
while ((length = fis.read(b)) > 0)
{
os.write(b, 0, length);
}
}
catch (IOException e)
{
throw e;
}
finally
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
/**
* @Desc 删除文件
* @param filePath 文件
* @return
*/
public static boolean deleteFile(String filePath)
{
boolean flag = false;
File file = new File(filePath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists())
{
file.delete();
flag = true;
}
return flag;
}
}
Loading…
Cancel
Save