|
@@ -1,203 +1,203 @@
|
|
|
-package com.qmth.cqb.utils;
|
|
|
-
|
|
|
-import java.io.BufferedInputStream;
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
-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 java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.Paths;
|
|
|
-import java.util.zip.ZipEntry;
|
|
|
-import java.util.zip.ZipOutputStream;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-
|
|
|
-
|
|
|
-/**
|
|
|
- * @author chenken
|
|
|
- * @date 2017年7月17日 上午9:36:32
|
|
|
- * @company QMTH
|
|
|
- * @description FileUtil.java
|
|
|
- */
|
|
|
-public class FileDisposeUtil {
|
|
|
-
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(FileDisposeUtil.class);
|
|
|
-
|
|
|
- public static boolean saveUrlAs(String fileUrl, String fileName) {
|
|
|
- try {
|
|
|
- URL url = new URL(fileUrl);
|
|
|
- HttpURLConnection connection = (HttpURLConnection) url
|
|
|
- .openConnection();
|
|
|
- DataInputStream in = new DataInputStream(
|
|
|
- connection.getInputStream());
|
|
|
- DataOutputStream out = new DataOutputStream(new FileOutputStream(
|
|
|
- fileName));
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int count = 0;
|
|
|
- while ((count = in.read(buffer)) > 0) {
|
|
|
- out.write(buffer, 0, count);
|
|
|
- }
|
|
|
- out.close();
|
|
|
- in.close();
|
|
|
- return true;
|
|
|
- } catch (Exception e) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- * 下载文件
|
|
|
- * @param filename 文件名称
|
|
|
- * @param fullFilePath 文件全路径
|
|
|
- * @param response
|
|
|
- */
|
|
|
- public static void downloadFile(String filename,String fullFilePath,HttpServletResponse response){
|
|
|
- //获得请求文件名
|
|
|
- //设置文件MIME类型
|
|
|
- response.setContentType(getContentType(filename));
|
|
|
- //设置Content-Disposition
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename="+filename);
|
|
|
- //读取目标文件,通过response将目标文件写到客户端
|
|
|
- //获取目标文件的绝对路径
|
|
|
- //读取文件
|
|
|
- try {
|
|
|
- InputStream in = new FileInputStream(fullFilePath);
|
|
|
- OutputStream out = response.getOutputStream();
|
|
|
- //写文件
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int count = 0;
|
|
|
- while ((count = in.read(buffer)) > 0) {
|
|
|
- out.write(buffer, 0, count);
|
|
|
- }
|
|
|
- response.flushBuffer();
|
|
|
- in.close();
|
|
|
- out.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
|
|
|
- * :压缩后存放路径
|
|
|
- * @param fileName
|
|
|
- * :压缩后文件的名称
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean fileToZip(String sourceFilePath, String zipFilePath,String fileName) {
|
|
|
- logger.info("开始压缩"+sourceFilePath+"目录");
|
|
|
- boolean flag = false;
|
|
|
- File sourceFile = new File(sourceFilePath);
|
|
|
- FileInputStream fis = null;
|
|
|
- BufferedInputStream bis = null;
|
|
|
- FileOutputStream fos = null;
|
|
|
- ZipOutputStream zos = null;
|
|
|
-
|
|
|
- if (sourceFile.exists() == false) {
|
|
|
- logger.error("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
|
- } else {
|
|
|
- try {
|
|
|
- File zipFile = new File(zipFilePath+File.separator+fileName+".zip");
|
|
|
- if (zipFile.exists()) {
|
|
|
- logger.error(zipFilePath + "目录下存在名字为:" + fileName+ ".zip" + "打包文件.");
|
|
|
- } else {
|
|
|
- File[] sourceFiles = sourceFile.listFiles();
|
|
|
- if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
- logger.error("待压缩的文件目录:" + 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++) {
|
|
|
- // 创建ZIP实体,并添加进压缩包
|
|
|
- ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
|
|
|
- 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();
|
|
|
- }
|
|
|
- flag = true;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException(e);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException(e);
|
|
|
- } finally {
|
|
|
- try {
|
|
|
- if (null != bis){
|
|
|
- bis.close();
|
|
|
- bis = null;
|
|
|
- }
|
|
|
- if (null != fis){
|
|
|
- fis.close();
|
|
|
- fis = null;
|
|
|
- }
|
|
|
- if (null != zos){
|
|
|
- zos.close();
|
|
|
- zos = null;
|
|
|
- }
|
|
|
- if (null != fos){
|
|
|
- fos.close();
|
|
|
- fos = null;
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- logger.info("压缩"+sourceFilePath+"目录完成");
|
|
|
- return flag;
|
|
|
- }
|
|
|
-
|
|
|
- public static void createDirectory(String downloadDirectory) {
|
|
|
- File directory = new File(downloadDirectory);
|
|
|
- if(directory.exists()){
|
|
|
- FileUtils.deleteQuietly(directory);
|
|
|
- }
|
|
|
- directory.mkdir();
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package com.qmth.cqb.utils;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+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 java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author chenken
|
|
|
+ * @date 2017年7月17日 上午9:36:32
|
|
|
+ * @company QMTH
|
|
|
+ * @description FileUtil.java
|
|
|
+ */
|
|
|
+public class FileDisposeUtil {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(FileDisposeUtil.class);
|
|
|
+
|
|
|
+ public static boolean saveUrlAs(String fileUrl, String fileName) {
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url
|
|
|
+ .openConnection();
|
|
|
+ DataInputStream in = new DataInputStream(
|
|
|
+ connection.getInputStream());
|
|
|
+ DataOutputStream out = new DataOutputStream(new FileOutputStream(
|
|
|
+ fileName));
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int count = 0;
|
|
|
+ while ((count = in.read(buffer)) > 0) {
|
|
|
+ out.write(buffer, 0, count);
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ in.close();
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 下载文件
|
|
|
+ * @param filename 文件名称
|
|
|
+ * @param fullFilePath 文件全路径
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public static void downloadFile(String filename,String fullFilePath,HttpServletResponse response){
|
|
|
+ //获得请求文件名
|
|
|
+ //设置文件MIME类型
|
|
|
+ response.setContentType(getContentType(filename));
|
|
|
+ //设置Content-Disposition
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename="+filename);
|
|
|
+ //读取目标文件,通过response将目标文件写到客户端
|
|
|
+ //获取目标文件的绝对路径
|
|
|
+ //读取文件
|
|
|
+ try {
|
|
|
+ InputStream in = new FileInputStream(fullFilePath);
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
+ //写文件
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int count = 0;
|
|
|
+ while ((count = in.read(buffer)) > 0) {
|
|
|
+ out.write(buffer, 0, count);
|
|
|
+ }
|
|
|
+ response.flushBuffer();
|
|
|
+ in.close();
|
|
|
+ out.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
|
|
|
+ * :压缩后存放路径
|
|
|
+ * @param fileName
|
|
|
+ * :压缩后文件的名称
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean fileToZip(String sourceFilePath, String zipFilePath,String fileName) {
|
|
|
+ logger.info("压缩"+sourceFilePath+"目录开始");
|
|
|
+ boolean flag = false;
|
|
|
+ File sourceFile = new File(sourceFilePath);
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
+ ZipOutputStream zos = null;
|
|
|
+
|
|
|
+ if (sourceFile.exists() == false) {
|
|
|
+ logger.error("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ File zipFile = new File(zipFilePath+File.separator+fileName+".zip");
|
|
|
+ if (zipFile.exists()) {
|
|
|
+ logger.error(zipFilePath + "目录下存在名字为:" + fileName+ ".zip" + "打包文件.");
|
|
|
+ } else {
|
|
|
+ File[] sourceFiles = sourceFile.listFiles();
|
|
|
+ if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
+ logger.error("待压缩的文件目录:" + 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++) {
|
|
|
+ // 创建ZIP实体,并添加进压缩包
|
|
|
+ ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (null != bis){
|
|
|
+ bis.close();
|
|
|
+ bis = null;
|
|
|
+ }
|
|
|
+ if (null != fis){
|
|
|
+ fis.close();
|
|
|
+ fis = null;
|
|
|
+ }
|
|
|
+ if (null != zos){
|
|
|
+ zos.close();
|
|
|
+ zos = null;
|
|
|
+ }
|
|
|
+ if (null != fos){
|
|
|
+ fos.close();
|
|
|
+ fos = null;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logger.info("压缩"+sourceFilePath+"目录完成");
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void createDirectory(String downloadDirectory) {
|
|
|
+ File directory = new File(downloadDirectory);
|
|
|
+ if(directory.exists()){
|
|
|
+ FileUtils.deleteQuietly(directory);
|
|
|
+ }
|
|
|
+ directory.mkdir();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|