|
@@ -1,13 +1,13 @@
|
|
package cn.com.qmth.examcloud.core.questions.base;
|
|
package cn.com.qmth.examcloud.core.questions.base;
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.HttpURLConnection;
|
|
|
|
+import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
@@ -23,7 +23,6 @@ import java.util.zip.ZipOutputStream;
|
|
* @company QMTH
|
|
* @company QMTH
|
|
*/
|
|
*/
|
|
public class FileDisposeUtil {
|
|
public class FileDisposeUtil {
|
|
-
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FileDisposeUtil.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(FileDisposeUtil.class);
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -34,40 +33,39 @@ public class FileDisposeUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static boolean saveUrlAs(String fileUrl, String localFilePath) {
|
|
public static boolean saveUrlAs(String fileUrl, String localFilePath) {
|
|
- HttpURLConnection connection = null;
|
|
|
|
- FileOutputStream fileOutputStream = null;
|
|
|
|
- DataOutputStream dataOutputStream = null;
|
|
|
|
- DataInputStream dataInputStream = null;
|
|
|
|
|
|
+ URL url;
|
|
|
|
+ try {
|
|
|
|
+ url = new URL(fileUrl);
|
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ HttpURLConnection connection;
|
|
try {
|
|
try {
|
|
- URL url = new URL(fileUrl);
|
|
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
- dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
|
- fileOutputStream = new FileOutputStream(localFilePath);
|
|
|
|
- dataOutputStream = new DataOutputStream(fileOutputStream);
|
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (
|
|
|
|
+ DataInputStream dataInputStream = new DataInputStream(connection.getInputStream());
|
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(localFilePath);
|
|
|
|
+ DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
|
|
|
|
+ ) {
|
|
|
|
+
|
|
byte[] buffer = new byte[4096];
|
|
byte[] buffer = new byte[4096];
|
|
- int count = 0;
|
|
|
|
|
|
+ int count;
|
|
while ((count = dataInputStream.read(buffer)) > 0) {
|
|
while ((count = dataInputStream.read(buffer)) > 0) {
|
|
dataOutputStream.write(buffer, 0, count);
|
|
dataOutputStream.write(buffer, 0, count);
|
|
}
|
|
}
|
|
|
|
+ fileOutputStream.flush();
|
|
|
|
+ dataOutputStream.flush();
|
|
return true;
|
|
return true;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
return false;
|
|
return false;
|
|
} finally {
|
|
} finally {
|
|
- try {
|
|
|
|
- if (fileOutputStream != null) {
|
|
|
|
- fileOutputStream.flush();
|
|
|
|
- fileOutputStream.close();
|
|
|
|
- }
|
|
|
|
- if (dataOutputStream != null) {
|
|
|
|
- dataOutputStream.flush();
|
|
|
|
- dataOutputStream.close();
|
|
|
|
- }
|
|
|
|
- if (dataInputStream != null) {
|
|
|
|
- dataInputStream.close();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
- }
|
|
|
|
if (connection != null) {
|
|
if (connection != null) {
|
|
connection.disconnect();
|
|
connection.disconnect();
|
|
}
|
|
}
|
|
@@ -82,10 +80,9 @@ public class FileDisposeUtil {
|
|
* @param response
|
|
* @param response
|
|
*/
|
|
*/
|
|
public static void downloadFile(String filename, String fullFilePath, HttpServletResponse response) {
|
|
public static void downloadFile(String filename, String fullFilePath, HttpServletResponse response) {
|
|
- InputStream in = null;
|
|
|
|
- OutputStream out = null;
|
|
|
|
|
|
+ try (InputStream in = new FileInputStream(fullFilePath);
|
|
|
|
+ OutputStream out = response.getOutputStream();) {
|
|
|
|
|
|
- try {
|
|
|
|
//设置编码
|
|
//设置编码
|
|
response.setCharacterEncoding("UTF-8");
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
@@ -101,33 +98,17 @@ public class FileDisposeUtil {
|
|
response.setContentType("application/octet-stream;charset=utf-8");
|
|
response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
|
|
|
//读取目标文件,通过response将目标文件写到客户端
|
|
//读取目标文件,通过response将目标文件写到客户端
|
|
- in = new FileInputStream(fullFilePath);
|
|
|
|
- out = response.getOutputStream();
|
|
|
|
-
|
|
|
|
- //写文件
|
|
|
|
byte[] buffer = new byte[4096];
|
|
byte[] buffer = new byte[4096];
|
|
- int count = 0;
|
|
|
|
|
|
+ int count;
|
|
while ((count = in.read(buffer)) > 0) {
|
|
while ((count = in.read(buffer)) > 0) {
|
|
out.write(buffer, 0, count);
|
|
out.write(buffer, 0, count);
|
|
}
|
|
}
|
|
|
|
+
|
|
response.flushBuffer();
|
|
response.flushBuffer();
|
|
- out.close();
|
|
|
|
- in.close();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
} catch (FileNotFoundException e) {
|
|
logger.error(e.getMessage(), e);
|
|
logger.error(e.getMessage(), e);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error(e.getMessage(), e);
|
|
logger.error(e.getMessage(), e);
|
|
- } finally {
|
|
|
|
- try {
|
|
|
|
- if (null != out) {
|
|
|
|
- out.close();
|
|
|
|
- }
|
|
|
|
- if (null != in) {
|
|
|
|
- in.close();
|
|
|
|
- }
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -158,67 +139,68 @@ public class FileDisposeUtil {
|
|
*/
|
|
*/
|
|
public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
|
|
public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {
|
|
logger.info("压缩" + sourceFilePath + "目录开始");
|
|
logger.info("压缩" + sourceFilePath + "目录开始");
|
|
- boolean flag = false;
|
|
|
|
|
|
+
|
|
File sourceFile = new File(sourceFilePath);
|
|
File sourceFile = new File(sourceFilePath);
|
|
- FileInputStream fis = null;
|
|
|
|
- BufferedInputStream bis = null;
|
|
|
|
- FileOutputStream fos = null;
|
|
|
|
- ZipOutputStream zos = null;
|
|
|
|
- if (sourceFile.exists() == false) {
|
|
|
|
|
|
+ if (!sourceFile.exists()) {
|
|
logger.error("待压缩的文件目录:" + sourceFilePath + "不存在.");
|
|
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++) {
|
|
|
|
- File file = sourceFiles[i];
|
|
|
|
- if (!file.isFile()) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- //创建ZIP实体,并添加进压缩包
|
|
|
|
- String fileEncode = System.getProperty("file.encoding");
|
|
|
|
- String name = new String(file.getName().getBytes(fileEncode), "UTF-8");
|
|
|
|
- ZipEntry zipEntry = new ZipEntry(name);
|
|
|
|
- zos.putNextEntry(zipEntry);
|
|
|
|
- //读取待压缩的文件并写进压缩包里
|
|
|
|
- fis = new FileInputStream(file);
|
|
|
|
- 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) {
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(bis);
|
|
|
|
- IOUtils.closeQuietly(fis);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- flag = true;
|
|
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File zipFile = new File(zipFilePath + File.separator + fileName + ".zip");
|
|
|
|
+ if (zipFile.exists()) {
|
|
|
|
+ logger.error(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ File[] sourceFiles = sourceFile.listFiles();
|
|
|
|
+ if (null == sourceFiles || sourceFiles.length < 1) {
|
|
|
|
+ logger.error("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (
|
|
|
|
+ FileOutputStream fos = new FileOutputStream(zipFile);
|
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fos);
|
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(bos);
|
|
|
|
+ ) {
|
|
|
|
+
|
|
|
|
+ byte[] bytes = new byte[1024 * 10];
|
|
|
|
+ for (int i = 0; i < sourceFiles.length; i++) {
|
|
|
|
+ File file = sourceFiles[i];
|
|
|
|
+ if (!file.isFile()) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try (
|
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(fis, 1024 * 10);
|
|
|
|
+ ) {
|
|
|
|
+ //创建ZIP实体,并添加进压缩包
|
|
|
|
+ String fileEncode = System.getProperty("file.encoding");
|
|
|
|
+ String name = new String(file.getName().getBytes(fileEncode), "UTF-8");
|
|
|
|
+
|
|
|
|
+ ZipEntry zipEntry = new ZipEntry(name);
|
|
|
|
+ zos.putNextEntry(zipEntry);
|
|
|
|
+
|
|
|
|
+ //读取待压缩的文件并写进压缩包里
|
|
|
|
+ int read;
|
|
|
|
+ while ((read = bis.read(bytes, 0, 1024 * 10)) != -1) {
|
|
|
|
+ zos.write(bytes, 0, read);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ zos.flush();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
}
|
|
}
|
|
- } catch (Exception e) {
|
|
|
|
- logger.error(e.getMessage(), e);
|
|
|
|
- } finally {
|
|
|
|
- IOUtils.closeQuietly(bis);
|
|
|
|
- IOUtils.closeQuietly(fis);
|
|
|
|
- IOUtils.closeQuietly(zos);
|
|
|
|
- IOUtils.closeQuietly(fos);
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ logger.info("压缩" + sourceFilePath + "目录完成");
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error(e.getMessage(), e);
|
|
}
|
|
}
|
|
- logger.info("压缩" + sourceFilePath + "目录完成");
|
|
|
|
- return flag;
|
|
|
|
|
|
+
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static void createDirectory(String downloadDirectory) {
|
|
public static void createDirectory(String downloadDirectory) {
|