|
@@ -1,17 +1,10 @@
|
|
|
package cn.com.qmth.examcloud.support.util;
|
|
|
|
|
|
-import java.io.BufferedInputStream;
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.DataInputStream;
|
|
|
-import java.io.DataOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
@@ -26,11 +19,6 @@ import java.util.zip.ZipEntry;
|
|
|
import java.util.zip.ZipFile;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
* @author chenken
|
|
@@ -39,192 +27,194 @@ import org.apache.commons.io.IOUtils;
|
|
|
* @description FileUtil.java
|
|
|
*/
|
|
|
public class FileDisposeUtil {
|
|
|
-
|
|
|
- /**
|
|
|
- * 将网络文件保存到本地
|
|
|
- * @param fileUrl 网络文件URL
|
|
|
- * @param localFilePath 例如D:/123.txt
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean saveUrlAs(String fileUrl,String localFilePath) {
|
|
|
- HttpURLConnection connection = null;
|
|
|
- FileOutputStream fileOutputStream = null;
|
|
|
- DataOutputStream dataOutputStream = null;
|
|
|
- DataInputStream dataInputStream = null;
|
|
|
- try {
|
|
|
- URL url = new URL(fileUrl);
|
|
|
- connection = (HttpURLConnection) url.openConnection();
|
|
|
- dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
- fileOutputStream = new FileOutputStream(localFilePath);
|
|
|
- dataOutputStream = new DataOutputStream(fileOutputStream);
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int count = 0;
|
|
|
- while ((count = dataInputStream.read(buffer)) > 0) {
|
|
|
- dataOutputStream.write(buffer, 0, count);
|
|
|
- }
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }finally {
|
|
|
- try {
|
|
|
- if(fileOutputStream!=null){
|
|
|
- fileOutputStream.flush();
|
|
|
- fileOutputStream.close();
|
|
|
- fileOutputStream = null;
|
|
|
- }
|
|
|
- if (dataOutputStream != null) {
|
|
|
- dataOutputStream.flush();
|
|
|
- dataOutputStream.close();
|
|
|
- dataOutputStream = null;
|
|
|
- }
|
|
|
- if (dataInputStream != null) {
|
|
|
- dataInputStream.close();
|
|
|
- dataInputStream = null;
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- if (connection != null) {
|
|
|
- connection.disconnect();
|
|
|
- connection = null;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载服务器上的文件
|
|
|
- * @param filename 文件名称
|
|
|
- * @param fullFilePath 文件全路径
|
|
|
- * @param response
|
|
|
- */
|
|
|
- public static void downloadFile(String filename,String fullFilePath,HttpServletResponse response){
|
|
|
- FileInputStream input = null;
|
|
|
- OutputStream output = null;
|
|
|
- try {
|
|
|
- //设置文件MIME类型
|
|
|
- response.setContentType(getContentType(filename));
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
|
|
|
- //读取目标文件,通过response将目标文件写到客户端
|
|
|
- input = new FileInputStream(fullFilePath);
|
|
|
- output = response.getOutputStream();
|
|
|
- //写文件
|
|
|
- byte[] b = new byte[2048];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将网络文件保存到本地
|
|
|
+ *
|
|
|
+ * @param fileUrl 网络文件URL
|
|
|
+ * @param localFilePath 例如D:/123.txt
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean saveUrlAs(String fileUrl, String localFilePath) {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ FileOutputStream fileOutputStream = null;
|
|
|
+ DataOutputStream dataOutputStream = null;
|
|
|
+ DataInputStream dataInputStream = null;
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
+ fileOutputStream = new FileOutputStream(localFilePath);
|
|
|
+ dataOutputStream = new DataOutputStream(fileOutputStream);
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int count = 0;
|
|
|
+ while ((count = dataInputStream.read(buffer)) > 0) {
|
|
|
+ dataOutputStream.write(buffer, 0, count);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (fileOutputStream != null) {
|
|
|
+ fileOutputStream.flush();
|
|
|
+ fileOutputStream.close();
|
|
|
+ fileOutputStream = null;
|
|
|
+ }
|
|
|
+ if (dataOutputStream != null) {
|
|
|
+ dataOutputStream.flush();
|
|
|
+ dataOutputStream.close();
|
|
|
+ dataOutputStream = null;
|
|
|
+ }
|
|
|
+ if (dataInputStream != null) {
|
|
|
+ dataInputStream.close();
|
|
|
+ dataInputStream = null;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ connection = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载服务器上的文件
|
|
|
+ *
|
|
|
+ * @param filename 文件名称
|
|
|
+ * @param fullFilePath 文件全路径
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public static void downloadFile(String filename, String fullFilePath, HttpServletResponse response) {
|
|
|
+ FileInputStream input = null;
|
|
|
+ OutputStream output = null;
|
|
|
+ try {
|
|
|
+ //设置文件MIME类型
|
|
|
+ response.setContentType(getContentType(filename));
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
|
|
+ //读取目标文件,通过response将目标文件写到客户端
|
|
|
+ input = new FileInputStream(fullFilePath);
|
|
|
+ output = response.getOutputStream();
|
|
|
+ //写文件
|
|
|
+ byte[] b = new byte[2048];
|
|
|
int len;
|
|
|
while ((len = input.read(b)) != -1) {
|
|
|
- output.write(b, 0, len);
|
|
|
- }
|
|
|
+ output.write(b, 0, len);
|
|
|
+ }
|
|
|
response.setHeader("Content-Length", String.valueOf(input.getChannel().size()));
|
|
|
- input.close();
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- * 获得文件MIME类型
|
|
|
- * @param filename
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getContentType(String filename){
|
|
|
- String type = null;
|
|
|
- Path path = Paths.get(filename);
|
|
|
- try {
|
|
|
- type = Files.probeContentType(path);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return type;
|
|
|
- }
|
|
|
+ input.close();
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得文件MIME类型
|
|
|
+ *
|
|
|
+ * @param filename
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getContentType(String filename) {
|
|
|
+ String type = null;
|
|
|
+ Path path = Paths.get(filename);
|
|
|
+ try {
|
|
|
+ type = Files.probeContentType(path);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return type;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
|
|
|
- *
|
|
|
- * @param sourceFilePath
|
|
|
- * :待压缩的文件夹路径
|
|
|
- * @param zipFilePath
|
|
|
- * :压缩后zip文件的存放路径
|
|
|
- * @param fileName
|
|
|
- * :zip文件的名称
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean fileToZip(String sourceFilePath, String zipFilePath,String fileName) {
|
|
|
- boolean flag = false;
|
|
|
- File sourceFile = new File(sourceFilePath);
|
|
|
- FileInputStream fis = null;
|
|
|
- BufferedInputStream bis = null;
|
|
|
- FileOutputStream fos = null;
|
|
|
- ZipOutputStream zos = null;
|
|
|
- if (sourceFile.exists() == false) {
|
|
|
- throw new RuntimeException("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
|
- } else {
|
|
|
- try {
|
|
|
- File zipFile = new File(zipFilePath+File.separator+fileName+".zip");
|
|
|
- if (zipFile.exists()) {
|
|
|
- throw new RuntimeException(zipFilePath + "目录下存在名字为:"+fileName+".zip"+"打包文件.");
|
|
|
- } else {
|
|
|
- File[] sourceFiles = sourceFile.listFiles();
|
|
|
- if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
- throw new RuntimeException("待压缩的文件目录:" + sourceFilePath+ "里面不存在文件,无需压缩.");
|
|
|
- } else {
|
|
|
- fos = new FileOutputStream(zipFile);
|
|
|
- zos = new ZipOutputStream(new BufferedOutputStream(fos));
|
|
|
- byte[] bufs = new byte[1024 * 10];
|
|
|
- for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
- try{
|
|
|
- //创建ZIP实体,并添加进压缩包
|
|
|
- String fileEncode = System.getProperty("file.encoding");
|
|
|
- String name = new String(sourceFiles[i].getName().getBytes(fileEncode),"UTF-8");
|
|
|
- ZipEntry zipEntry = new ZipEntry(name);
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
- //读取待压缩的文件并写进压缩包里
|
|
|
- fis = new FileInputStream(sourceFiles[i]);
|
|
|
- bis = new BufferedInputStream(fis, 1024 * 10);
|
|
|
- int read = 0;
|
|
|
- while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
|
|
|
- zos.write(bufs, 0, read);
|
|
|
- }
|
|
|
- zos.flush();
|
|
|
- }catch(Exception e){
|
|
|
- e.printStackTrace();
|
|
|
- }finally{
|
|
|
- IOUtils.closeQuietly(bis);
|
|
|
- IOUtils.closeQuietly(fis);
|
|
|
- }
|
|
|
- }
|
|
|
- flag = true;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- IOUtils.closeQuietly(bis);
|
|
|
- IOUtils.closeQuietly(fis);
|
|
|
- IOUtils.closeQuietly(zos);
|
|
|
- IOUtils.closeQuietly(fos);
|
|
|
- }
|
|
|
- }
|
|
|
- return flag;
|
|
|
- }
|
|
|
-
|
|
|
- public static void createDirectory(String downloadDirectory) {
|
|
|
- File directory = new File(downloadDirectory);
|
|
|
- if(!directory.exists()){
|
|
|
- directory.mkdirs();
|
|
|
- }else{
|
|
|
- FileUtils.deleteQuietly(directory);
|
|
|
- directory.mkdirs();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获得文件的byte数组
|
|
|
- * @param filePath
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public static byte[] getBytes(String filePath) throws IOException{
|
|
|
- File file = new File(filePath);
|
|
|
+ /**
|
|
|
+ * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
|
|
|
+ *
|
|
|
+ * @param sourceFilePath :待压缩的文件夹路径
|
|
|
+ * @param zipFilePath :压缩后zip文件的存放路径
|
|
|
+ * @param fileName :zip文件的名称
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
|
|
|
+ boolean flag = false;
|
|
|
+ File sourceFile = new File(sourceFilePath);
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ ZipOutputStream zos = null;
|
|
|
+ if (sourceFile.exists() == false) {
|
|
|
+ throw new RuntimeException("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ File zipFile = new File(zipFilePath + File.separator + fileName + ".zip");
|
|
|
+ if (zipFile.exists()) {
|
|
|
+ throw new RuntimeException(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
|
|
|
+ } else {
|
|
|
+ File[] sourceFiles = sourceFile.listFiles();
|
|
|
+ if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
+ throw new RuntimeException("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
|
|
|
+ } else {
|
|
|
+ fos = new FileOutputStream(zipFile);
|
|
|
+ zos = new ZipOutputStream(new BufferedOutputStream(fos));
|
|
|
+ byte[] bufs = new byte[1024 * 10];
|
|
|
+ for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
+ try {
|
|
|
+ //创建ZIP实体,并添加进压缩包
|
|
|
+ String fileEncode = System.getProperty("file.encoding");
|
|
|
+ String name = new String(sourceFiles[i].getName().getBytes(fileEncode), "UTF-8");
|
|
|
+ ZipEntry zipEntry = new ZipEntry(name);
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
+ //读取待压缩的文件并写进压缩包里
|
|
|
+ fis = new FileInputStream(sourceFiles[i]);
|
|
|
+ bis = new BufferedInputStream(fis, 1024 * 10);
|
|
|
+ int read = 0;
|
|
|
+ while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
|
|
|
+ zos.write(bufs, 0, read);
|
|
|
+ }
|
|
|
+ zos.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(bis);
|
|
|
+ IOUtils.closeQuietly(fis);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(bis);
|
|
|
+ IOUtils.closeQuietly(fis);
|
|
|
+ IOUtils.closeQuietly(zos);
|
|
|
+ IOUtils.closeQuietly(fos);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void createDirectory(String downloadDirectory) {
|
|
|
+ File directory = new File(downloadDirectory);
|
|
|
+ if (!directory.exists()) {
|
|
|
+ directory.mkdirs();
|
|
|
+ } else {
|
|
|
+ FileUtils.deleteQuietly(directory);
|
|
|
+ directory.mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得文件的byte数组
|
|
|
+ *
|
|
|
+ * @param filePath
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static byte[] getBytes(String filePath) throws IOException {
|
|
|
+ File file = new File(filePath);
|
|
|
if (!file.exists()) {
|
|
|
throw new FileNotFoundException(filePath);
|
|
|
}
|
|
@@ -251,7 +241,7 @@ public class FileDisposeUtil {
|
|
|
bos.close();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public static File createZip(String sourceFilePath, String targetFilePath) throws IOException {
|
|
|
OutputStream fos = null;
|
|
|
ZipOutputStream zos = null;
|
|
@@ -272,7 +262,7 @@ public class FileDisposeUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private static void writeZip(File file, String parentPath, ZipOutputStream zos) throws IOException {
|
|
|
if (file.exists()) {
|
|
|
ZipEntry ze = null;
|
|
@@ -327,7 +317,7 @@ public class FileDisposeUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 解压文件
|
|
|
*
|
|
@@ -336,20 +326,20 @@ public class FileDisposeUtil {
|
|
|
*/
|
|
|
public static List<File> unZip(File targetDir, File zipFile) {
|
|
|
if (targetDir == null) {
|
|
|
- throw new RuntimeException("解压目录不能为空!");
|
|
|
+ throw new RuntimeException("解压目录不能为空!");
|
|
|
}
|
|
|
|
|
|
if (zipFile == null) {
|
|
|
- throw new RuntimeException("待解压的文件不能为空!");
|
|
|
+ throw new RuntimeException("待解压的文件不能为空!");
|
|
|
}
|
|
|
|
|
|
if (!zipFile.exists()) {
|
|
|
- throw new RuntimeException("待解压的文件不存在!" + zipFile.getAbsolutePath());
|
|
|
+ throw new RuntimeException("待解压的文件不存在!" + zipFile.getAbsolutePath());
|
|
|
}
|
|
|
|
|
|
String zipName = zipFile.getName().toLowerCase();
|
|
|
if (zipFile.isDirectory() || zipName.indexOf(".zip") < 0) {
|
|
|
- throw new RuntimeException("待解压的文件格式错误!");
|
|
|
+ throw new RuntimeException("待解压的文件格式错误!");
|
|
|
}
|
|
|
|
|
|
if (!targetDir.exists()) {
|
|
@@ -382,16 +372,17 @@ public class FileDisposeUtil {
|
|
|
IOUtils.copy(is, os);
|
|
|
os.flush();
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e.getMessage(), e);
|
|
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
}
|
|
|
result.add(target);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException(e.getMessage(), e);
|
|
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
}
|