前提,引入相应类,加入maven依赖。
<!-- 新增文件上传,下载依赖 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
二核心代码
package com.bh.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
/**
* 文件工具类
*
* @author zlf 2017-11-29
*/
public class FileUtil {
/**
* 移动文件2.0
*
* @param srcPath
* 源文件路径
* @param goalPath
* 目标路径
* @return
* @throws IOException
*/
public static boolean moveFile(String srcPath, String goalPath) throws IOException {
File srcFile = new File(srcPath);
FileOutputStream fos = new FileOutputStream(goalPath + File.separator + srcFile.getName());
return moveFile(srcFile, fos);
}
/**
* 移动文件2.0
*
* @param srcFile
* @param stream
* @return
*/
public static boolean moveFile(File srcFile, OutputStream stream) {
try {
FileUtils.copyFile(srcFile, stream);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 删除文件
*
* @param path
* @return
*/
public static boolean deleteFile(String path) {
File file = new File(path);
return deleteFile(file);
}
/**
* 删除文件
*
* @param file
* @return
*/
public static boolean deleteFile(File file) {
if (file.exists() && file.isFile()) {
return file.delete();
}
return false;
}
/**
* 按照指定路径创建文件夹
*
* @param path
* @return file文件目录
*/
public static File createDir(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
return file;
}
return null;
}
/**
* 查找指定路径下的所有文件方法
*
* @param dirPath
* @return 返回所有的文件对象
*/
public static File[] getFilesByDir(String dirPath) {
File dir = new File(dirPath);
return dir.listFiles();
}
/**
* 备份指定文件夹的所有文件方法
*
* @param srcPath
* @param goalPath
*/
public static void backupAllFiles(String srcPath, String goalPath) {
File[] srcFiles = getFilesByDir(srcPath);
for (File file : srcFiles) {
if (file.isDirectory() && file.getName().length() != 36) {// 去除预览文件夹
if (srcPath.endsWith(File.separator) && goalPath.endsWith(File.separator)) {
backupFolder(srcPath + file.getName(), goalPath + file.getName());
} else if (srcPath.endsWith(File.separator) && !goalPath.endsWith(File.separator)) {
backupFolder(srcPath + file.getName(), goalPath + File.separator + file.getName());
} else if (!srcPath.endsWith(File.separator) && goalPath.endsWith(File.separator)) {
backupFolder(srcPath + File.separator + file.getName(), goalPath + file.getName());
} else {
backupFolder(srcPath + File.separator + file.getName(), goalPath + File.separator + file.getName());
}
} else if (file.isFile()) {
File goalFile = null;
if (goalPath.endsWith(File.separator)) {
goalFile = new File(goalPath + file.getName());
} else {
goalFile = new File(goalPath + File.separator + file.getName());
}
if (goalFile.exists()) {
goalFile.delete();
}
try {
goalFile.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(goalFile);
moveFile(file, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
/**
* 备份文件夹
*
* @param srcPath
* @param goalPath
*/
public static void backupFolder(String srcPath, String goalPath) {
// 创建拷贝目录
File file = new File(goalPath);
if (file.exists()) {
delFolder(goalPath);
file.mkdirs();
} else {
file.mkdirs();
}
// 拷贝文件
backupAllFiles(srcPath, goalPath);
}
/**
* 根据文件名称获取到文件的后缀
*
* @param path
* @return
*/
public static String GetFileExt(String fileName) {
String ext = null;
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
ext = fileName.substring(i + 1).toLowerCase();
}
return ext;
}
/**
* 创建生成的html存放目录
*
* @param path
*/
public static void createHtmlDir(String path) {
int i = path.lastIndexOf(File.separator);
String dirPath = "";
if (i > 0 && i < path.length() - 1) {
dirPath = path.substring(0, i).toLowerCase();
}
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
/***
* 删除指定文件夹下所有文件
*
* @param path
* 文件夹相对路径
* @return
*/
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + File.separator + tempList[i]);// 先删除文件夹里面的文件
delFolder(path + File.separator + tempList[i]);// 再删除空文件夹
flag = true;
}
}
return flag;
}
/***
* 删除文件夹
*
* @param folderPath文件夹相对路径
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
}