From fc73deb3fde28243e01fac89569a6d3b5d2781ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=B2=BE=E5=8D=8E?= <842761733@qq.com> Date: Tue, 11 Aug 2020 10:42:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=88=B0demo=E4=B8=AD=E7=9A=84=E5=8E=8B=E7=BC=A9=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=8F=8Apdf=E9=A2=84=E8=A7=88=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/utils/DownloadUtils.java | 39 +++++++++++-------- .../controller/OnlinePreviewController.java | 3 +- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java index 895f384c..1c279d91 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java @@ -56,9 +56,10 @@ public class DownloadUtils { } try { URL url = new URL(urlStr); - OutputStream os = new FileOutputStream(new File(realPath)); if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) { - saveToOutputStreamFromUrl(urlStr, os); + byte[] bytes = getBytesFromUrl(urlStr); + OutputStream os = new FileOutputStream(new File(realPath)); + saveBytesToOutStream(bytes, os); } else if (url.getProtocol() != null && "ftp".equals(url.getProtocol().toLowerCase())) { String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); @@ -88,21 +89,24 @@ public class DownloadUtils { } } - public boolean saveToOutputStreamFromUrl(String urlStr, OutputStream os) throws IOException { + public byte[] getBytesFromUrl(String urlStr) throws IOException { InputStream is = getInputStreamFromUrl(urlStr); if (is != null) { - copyStream(is, os); + return getBytesFromStream(is); } else { urlStr = URLUtil.normalize(urlStr, true, true); is = getInputStreamFromUrl(urlStr); - if (is != null) { - copyStream(is, os); - } else { - os.close(); - return false; + if (is == null) { + logger.error("文件下载异常:url:{}", urlStr); + throw new IOException("文件下载异常:url:" + urlStr); } + return getBytesFromStream(is); } - return true; + } + + public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException { + os.write(b); + os.close(); } private InputStream getInputStreamFromUrl(String urlStr) { @@ -119,14 +123,17 @@ public class DownloadUtils { } } - private void copyStream(InputStream is, OutputStream os) throws IOException { - byte[] bs = new byte[1024]; - int len; - while (-1 != (len = is.read(bs))) { - os.write(bs, 0, len); + private byte[] getBytesFromStream(InputStream is) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = is.read(buffer)) != -1) { + baos.write(buffer, 0, len); } + byte[] b = baos.toByteArray(); is.close(); - os.close(); + baos.close(); + return b; } /** diff --git a/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java b/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java index 88a768ca..e91d4829 100644 --- a/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java +++ b/jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java @@ -88,7 +88,8 @@ public class OnlinePreviewController { public void getCorsFile(String urlPath, HttpServletResponse response) { logger.info("下载跨域pdf文件url:{}", urlPath); try { - downloadUtils.saveToOutputStreamFromUrl(urlPath, response.getOutputStream()); + byte[] bytes = downloadUtils.getBytesFromUrl(urlPath); + downloadUtils.saveBytesToOutStream(bytes, response.getOutputStream()); } catch (IOException e) { logger.error("下载跨域pdf文件异常,url:{}", urlPath, e); } From 9e2962bb6250e2cb47da228bb74a91f8ab61493e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=B2=BE=E5=8D=8E?= <842761733@qq.com> Date: Tue, 11 Aug 2020 10:42:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?2.2.1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- jodconverter-web/pom.xml | 2 +- jodconverter-web/src/main/bin/startup.bat | 2 +- jodconverter-web/src/main/bin/startup.sh | 2 +- pom.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2bbc798a..58b968fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,5 +25,5 @@ ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV PATH $PATH:$JAVA_HOME/bin ENV LANG zh_CN.UTF-8 ENV LC_ALL zh_CN.UTF-8 -ENV KKFILEVIEW_BIN_FOLDER /opt/kkFileView-2.2.1-SNAPSHOT/bin -ENTRYPOINT ["java","-Dfile.encoding=UTF-8","-Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider","-Dspring.config.location=/opt/kkFileView-2.2.1-SNAPSHOT/config/application.properties","-jar","/opt/kkFileView-2.2.1-SNAPSHOT/bin/kkFileView-2.2.1-SNAPSHOT.jar"] \ No newline at end of file +ENV KKFILEVIEW_BIN_FOLDER /opt/kkFileView-2.2.1/bin +ENTRYPOINT ["java","-Dfile.encoding=UTF-8","-Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider","-Dspring.config.location=/opt/kkFileView-2.2.1/config/application.properties","-jar","/opt/kkFileView-2.2.1/bin/kkFileView-2.2.1.jar"] \ No newline at end of file diff --git a/jodconverter-web/pom.xml b/jodconverter-web/pom.xml index 55431514..bac1265a 100644 --- a/jodconverter-web/pom.xml +++ b/jodconverter-web/pom.xml @@ -12,7 +12,7 @@ cn.keking kkFileView - 2.2.1-SNAPSHOT + 2.2.1 diff --git a/jodconverter-web/src/main/bin/startup.bat b/jodconverter-web/src/main/bin/startup.bat index 9aea0f11..8eb4d413 100644 --- a/jodconverter-web/src/main/bin/startup.bat +++ b/jodconverter-web/src/main/bin/startup.bat @@ -6,4 +6,4 @@ echo Starting kkFileView... echo Please check log file in ../log/kkFileView.log for more information echo You can get help in our official homesite: https://kkFileView.keking.cn echo If this project is helpful to you, please star it on https://gitee.com/kekingcn/file-online-preview/stargazers -java -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider -Dspring.config.location=..\config\application.properties -jar kkFileView-2.2.1-SNAPSHOT.jar -> ..\log\kkFileView.log \ No newline at end of file +java -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider -Dspring.config.location=..\config\application.properties -jar kkFileView-2.2.1.jar -> ..\log\kkFileView.log \ No newline at end of file diff --git a/jodconverter-web/src/main/bin/startup.sh b/jodconverter-web/src/main/bin/startup.sh index 44365e3a..f60ea68d 100644 --- a/jodconverter-web/src/main/bin/startup.sh +++ b/jodconverter-web/src/main/bin/startup.sh @@ -29,4 +29,4 @@ echo "Starting kkFileView..." echo "Please execute ./showlog.sh to check log for more information" echo "You can get help in our official homesite: https://kkFileView.keking.cn" echo "If this project is helpful to you, please star it on https://gitee.com/kekingcn/file-online-preview/stargazers" -nohup java -Dfile.encoding=UTF-8 -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider -Dspring.config.location=../config/application.properties -jar kkFileView-2.2.1-SNAPSHOT.jar > ../log/kkFileView.log 2>&1 & +nohup java -Dfile.encoding=UTF-8 -Dsun.java2d.cmm=sun.java2d.cmm.kcms.KcmsServiceProvider -Dspring.config.location=../config/application.properties -jar kkFileView-2.2.1.jar > ../log/kkFileView.log 2>&1 & diff --git a/pom.xml b/pom.xml index a5868391..a8f1ef6a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ cn.keking filepreview - 2.2.1-SNAPSHOT + 2.2.1 jodconverter-core jodconverter-web