From 4c708f3cbd5a882dcc501c7daf03ca44bf42d215 Mon Sep 17 00:00:00 2001 From: 13540823418 <37830864+zhangxiaoxiao9527@users.noreply.github.com> Date: Sun, 6 Dec 2020 01:38:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E6=96=87=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/utils/DownloadUtils.java | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 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 1c279d91..5fc4cfc1 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java @@ -33,6 +33,7 @@ public class DownloadUtils { private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; + private static final String[] URL_PARAM_LOCALURL_START={"c:","d:","e:","f:","g:","/"}; /** * @param fileAttribute fileAttribute @@ -55,20 +56,26 @@ public class DownloadUtils { dirFile.mkdirs(); } try { - URL url = new URL(urlStr); - if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) { - byte[] bytes = getBytesFromUrl(urlStr); + if(isLocalUrl(urlStr)){ + byte[] bytes = getBytesFromLocalUrl(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); - String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); - FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); - } else { - response.setCode(1); - response.setContent(null); - response.setMsg("url不能识别url" + urlStr); + }else{ + URL url = new URL(urlStr); + if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) { + 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); + String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); + FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); + } else { + response.setCode(1); + response.setContent(null); + response.setMsg("url不能识别url" + urlStr); + } } response.setContent(realPath); response.setMsg(fileName); @@ -104,6 +111,21 @@ public class DownloadUtils { } } + public byte[] getBytesFromLocalUrl(String urlStr) throws IOException { + File file = new File(urlStr); + FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); + byte[] b = new byte[1024]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + fis.close(); + byte[] data = bos.toByteArray(); + bos.close(); + return data; + } + public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException { os.write(b); os.close(); @@ -173,4 +195,16 @@ public class DownloadUtils { } } } + + /** + * @param url url + * @return 是否是本地文件的绝对路径 + */ + private boolean isLocalUrl(String url){ + for(String str:URL_PARAM_LOCALURL_START){ + if(url.toLowerCase().startsWith(str)) + return true; + } + return false; + } } From fb09a8c00f249ea3a1fa16d8d5ef28877ff450fd Mon Sep 17 00:00:00 2001 From: 13540823418 <37830864+zhangxiaoxiao9527@users.noreply.github.com> Date: Thu, 10 Dec 2020 11:47:22 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E6=96=87=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96,=E9=87=87=E7=94=A8file=E6=A0=87=E5=87=86=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/utils/DownloadUtils.java | 58 ++++--------------- 1 file changed, 12 insertions(+), 46 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 5fc4cfc1..9bebed40 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java @@ -33,7 +33,6 @@ public class DownloadUtils { private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; - private static final String[] URL_PARAM_LOCALURL_START={"c:","d:","e:","f:","g:","/"}; /** * @param fileAttribute fileAttribute @@ -56,26 +55,20 @@ public class DownloadUtils { dirFile.mkdirs(); } try { - if(isLocalUrl(urlStr)){ - byte[] bytes = getBytesFromLocalUrl(urlStr); + URL url = new URL(urlStr); + if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) { + byte[] bytes = getBytesFromUrl(urlStr); OutputStream os = new FileOutputStream(new File(realPath)); saveBytesToOutStream(bytes, os); - }else{ - URL url = new URL(urlStr); - if (url.getProtocol() != null && url.getProtocol().toLowerCase().startsWith("http")) { - 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); - String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); - FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); - } else { - response.setCode(1); - response.setContent(null); - response.setMsg("url不能识别url" + urlStr); - } + } 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); + String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); + FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); + } else { + response.setCode(1); + response.setContent(null); + response.setMsg("url不能识别url" + urlStr); } response.setContent(realPath); response.setMsg(fileName); @@ -111,21 +104,6 @@ public class DownloadUtils { } } - public byte[] getBytesFromLocalUrl(String urlStr) throws IOException { - File file = new File(urlStr); - FileInputStream fis = new FileInputStream(file); - ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); - byte[] b = new byte[1024]; - int n; - while ((n = fis.read(b)) != -1) { - bos.write(b, 0, n); - } - fis.close(); - byte[] data = bos.toByteArray(); - bos.close(); - return data; - } - public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException { os.write(b); os.close(); @@ -195,16 +173,4 @@ public class DownloadUtils { } } } - - /** - * @param url url - * @return 是否是本地文件的绝对路径 - */ - private boolean isLocalUrl(String url){ - for(String str:URL_PARAM_LOCALURL_START){ - if(url.toLowerCase().startsWith(str)) - return true; - } - return false; - } }