|
@@ -2,6 +2,7 @@ package com.qmth.teachcloud.common.util;
|
|
|
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
@@ -40,10 +41,11 @@ public class ConvertUtil {
|
|
// inputStream转outputStream
|
|
// inputStream转outputStream
|
|
public static ByteArrayOutputStream parse(final InputStream in) throws Exception {
|
|
public static ByteArrayOutputStream parse(final InputStream in) throws Exception {
|
|
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
|
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
|
- int ch;
|
|
|
|
- while ((ch = in.read()) != -1) {
|
|
|
|
- swapStream.write(ch);
|
|
|
|
- }
|
|
|
|
|
|
+// int ch;
|
|
|
|
+// while ((ch = in.read()) != -1) {
|
|
|
|
+// swapStream.write(ch);
|
|
|
|
+// }
|
|
|
|
+ IOUtils.copy(in, swapStream);
|
|
return swapStream;
|
|
return swapStream;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -54,22 +56,6 @@ public class ConvertUtil {
|
|
return new ByteArrayInputStream(baos.toByteArray());
|
|
return new ByteArrayInputStream(baos.toByteArray());
|
|
}
|
|
}
|
|
|
|
|
|
- public static void inputStream2File(InputStream is, File file) throws IOException {
|
|
|
|
- OutputStream os = null;
|
|
|
|
- try {
|
|
|
|
- os = new FileOutputStream(file);
|
|
|
|
- int len = 0;
|
|
|
|
- byte[] buffer = new byte[8192];
|
|
|
|
-
|
|
|
|
- while ((len = is.read(buffer)) != -1) {
|
|
|
|
- os.write(buffer, 0, len);
|
|
|
|
- }
|
|
|
|
- } finally {
|
|
|
|
- os.close();
|
|
|
|
- is.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 转换,解析时间
|
|
* 转换,解析时间
|
|
*
|
|
*
|
|
@@ -293,32 +279,6 @@ public class ConvertUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public File saveLocal(InputStream inputStream, String dirPath) throws IOException {
|
|
|
|
- File desFile = new File(dirPath);
|
|
|
|
- if (!desFile.getParentFile().exists()) {
|
|
|
|
- desFile.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
|
|
|
|
- }
|
|
|
|
- byte[] bytes = new byte[1024]; // 开辟一个拷贝缓冲区
|
|
|
|
- OutputStream outputStream = null;
|
|
|
|
- try {
|
|
|
|
- outputStream = new FileOutputStream(desFile);
|
|
|
|
- int length;
|
|
|
|
- while ((length = inputStream.read(bytes)) != -1) { //当读到尽头后,返回值为-1这个时候停止输出,拷贝结束
|
|
|
|
- outputStream.write(bytes, 0, length);
|
|
|
|
- }
|
|
|
|
- return desFile;
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw e;
|
|
|
|
- } finally {
|
|
|
|
- if (inputStream != null) {
|
|
|
|
- inputStream.close();
|
|
|
|
- }
|
|
|
|
- if (outputStream != null) {
|
|
|
|
- outputStream.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 判断里面参数只要有一个为空 就是true 全部不为空时 false
|
|
* 判断里面参数只要有一个为空 就是true 全部不为空时 false
|
|
*
|
|
*
|
|
@@ -340,8 +300,8 @@ public class ConvertUtil {
|
|
|
|
|
|
public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
|
|
public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
|
|
try {
|
|
try {
|
|
- byte[] buf = new byte[1024];
|
|
|
|
- int len = 0;
|
|
|
|
|
|
+// byte[] buf = new byte[1024];
|
|
|
|
+// int len = 0;
|
|
|
|
|
|
String fName = new String(fileName.getBytes(), "ISO-8859-1");
|
|
String fName = new String(fileName.getBytes(), "ISO-8859-1");
|
|
|
|
|
|
@@ -349,14 +309,15 @@ public class ConvertUtil {
|
|
response.setContentType("application/x-msdownload");
|
|
response.setContentType("application/x-msdownload");
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
|
|
|
|
- OutputStream outStream = response.getOutputStream();
|
|
|
|
|
|
+// OutputStream outStream = response.getOutputStream();
|
|
|
|
|
|
- while ((len = inputStream.read(buf)) > 0) {
|
|
|
|
- outStream.write(buf, 0, len);
|
|
|
|
- }
|
|
|
|
|
|
+// while ((len = inputStream.read(buf)) > 0) {
|
|
|
|
+// outStream.write(buf, 0, len);
|
|
|
|
+// }
|
|
|
|
|
|
|
|
+ IOUtils.copy(inputStream, response.getOutputStream());
|
|
inputStream.close();
|
|
inputStream.close();
|
|
- outStream.close();
|
|
|
|
|
|
+// outStream.close();
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
}
|
|
}
|
|
@@ -370,9 +331,10 @@ public class ConvertUtil {
|
|
response.setContentType("application/x-msdownload");
|
|
response.setContentType("application/x-msdownload");
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
|
|
|
|
- OutputStream outStream = response.getOutputStream();
|
|
|
|
- baos.writeTo(outStream);
|
|
|
|
- outStream.close();
|
|
|
|
|
|
+// OutputStream outStream = response.getOutputStream();
|
|
|
|
+// baos.writeTo(outStream);
|
|
|
|
+// outStream.close();
|
|
|
|
+ IOUtils.write(baos.toByteArray(), response.getOutputStream());
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw new Exception("下载失败");
|
|
throw new Exception("下载失败");
|
|
}
|
|
}
|
|
@@ -382,7 +344,7 @@ public class ConvertUtil {
|
|
PDDocument doc = null;
|
|
PDDocument doc = null;
|
|
ByteArrayOutputStream os = null;
|
|
ByteArrayOutputStream os = null;
|
|
InputStream stream = null;
|
|
InputStream stream = null;
|
|
- OutputStream out = null;
|
|
|
|
|
|
+// OutputStream out = null;
|
|
List<File> fileList = new ArrayList<>();
|
|
List<File> fileList = new ArrayList<>();
|
|
try {
|
|
try {
|
|
// pdf路径
|
|
// pdf路径
|
|
@@ -396,7 +358,6 @@ public class ConvertUtil {
|
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
|
|
BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
|
|
os = new ByteArrayOutputStream();
|
|
os = new ByteArrayOutputStream();
|
|
ImageIO.write(bim, "jpg", os);
|
|
ImageIO.write(bim, "jpg", os);
|
|
- byte[] dataList = os.toByteArray();
|
|
|
|
// jpg文件转出路径
|
|
// jpg文件转出路径
|
|
File file = new File(targetPath + SystemConstant.HYPHEN + (i + 1) + SystemConstant.JPG_PREFIX);
|
|
File file = new File(targetPath + SystemConstant.HYPHEN + (i + 1) + SystemConstant.JPG_PREFIX);
|
|
if (!file.getParentFile().exists()) {
|
|
if (!file.getParentFile().exists()) {
|
|
@@ -404,8 +365,9 @@ public class ConvertUtil {
|
|
file.getParentFile().mkdirs();
|
|
file.getParentFile().mkdirs();
|
|
file.createNewFile();
|
|
file.createNewFile();
|
|
}
|
|
}
|
|
- out = new FileOutputStream(file);
|
|
|
|
- out.write(dataList);
|
|
|
|
|
|
+// out = new FileOutputStream(file);
|
|
|
|
+ IOUtils.write(os.toByteArray(), new FileOutputStream(file));
|
|
|
|
+// out.write(dataList);
|
|
fileList.add(file);
|
|
fileList.add(file);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -421,9 +383,9 @@ public class ConvertUtil {
|
|
if (stream != null) {
|
|
if (stream != null) {
|
|
stream.close();
|
|
stream.close();
|
|
}
|
|
}
|
|
- if (out != null) {
|
|
|
|
- out.close();
|
|
|
|
- }
|
|
|
|
|
|
+// if (out != null) {
|
|
|
|
+// out.close();
|
|
|
|
+// }
|
|
}
|
|
}
|
|
return fileList;
|
|
return fileList;
|
|
}
|
|
}
|