废话不说直接上代码:
package cn.java.sys.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* 文件类
*
* @author java
*
*/
public abstract class FileUtil {
/**
* 删除文件夹(里面的文件一并删除)
*
* @param folderPath
*/
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); // 删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); // 删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除全部文件
*
* @param path
* @return
*/
private 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.exists()) {
deleteFile(temp);
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);
delFolder(path + "/" + tempList[i]);
flag = true;
}
}
return flag;
}
public static void deleteFile(String path) {
File file = new File(path);
deleteFile(file);
}
/**
* 删除某个文件,若删除不成功,尝试十次
*
* @param file
*/
public static void deleteFile(File file) {
if (file.isFile() && file.exists()) {
int tryCount = 0;
boolean result = false;
while (!result && tryCount++ < 10) {
System.gc();
result = file.delete();
}
}
}
// 获取文件的扩展名
public static String getExtension(String fileName) {
if (fileName != null && fileName.length() > 0) {
int i = fileName.lastIndexOf(".");
if (i > -1 && i < (fileName.length() - 1)) {
return fileName.substring(i + 1);
}
}
return "";
}
/**
*
* @param filePath 需要导出文件的路径
* @param response
* @return 0 失败;1 成功
*/
public static int downLoadFile(String filePath,HttpServletResponse response){
try {
String fileName = "";
if (filePath.lastIndexOf("/") > 0) {
fileName = new String(filePath.substring(
filePath.lastIndexOf("/") + 1, filePath.length()));
} else if (filePath.lastIndexOf("\\") > 0) {
fileName = new String(filePath.substring(
filePath.lastIndexOf("\\") + 1, filePath.length()));
}
File file = new File(filePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ java.net.URLEncoder.encode(fileName, "utf-8"));
response.setContentLength((int) file.length());
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int temp;
while ((temp = fis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, temp);
}
fis.close();
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public static void downLoadFile(String fileName,String path ,HttpServletResponse response) throws IOException{
File file = new File(path);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ java.net.URLEncoder.encode(fileName, "utf-8"));
response.setContentLength((int) file.length());
ServletOutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int temp;
while ((temp = fis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, temp);
}
fis.close();
}
}
public static void main(String[] args) {
System.out.println(File.separator+FileUtil.getExtension("1.gif"));
System.out.println(UUID.randomUUID().toString());
}
public String getFileEndName(String fileName) {
int i = fileName.lastIndexOf(". ");
String endName = "";
endName = fileName.substring(i + 1);
return endName;
}
/**
* 获取文件名,去掉文件后缀
* @param fileName 全文件名
* @return
*/
public static String getFileName(String fileName){
if (fileName != null && fileName.length() > 0) {
int i = fileName.lastIndexOf(".");
if (i > -1 && i < (fileName.length() - 1)) {
String s = fileName.substring(0, i);
return s;
}
}
return "";
}
/**
* 压缩文件
* @param response
* @param path 文件路径
* @throws Exception
*/
public static void compressFile(HttpServletResponse response,String path) throws Exception{
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename="
+ java.net.URLEncoder.encode("picture.zip", "utf-8"));
}
}