Skip to content

Commit 986f562

Browse files
committed
引入galimatias,解决不规范文件名导致文件下载异常
1 parent 11d0441 commit 986f562

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

server/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@
201201
<scope>system</scope>
202202
<systemPath>${basedir}/lib/cpdetector-1.04.jar</systemPath>
203203
</dependency>
204+
<!-- url 规范化 -->
205+
<dependency>
206+
<groupId>io.mola.galimatias</groupId>
207+
<artifactId>galimatias</artifactId>
208+
<version>0.2.1</version>
209+
</dependency>
204210
</dependencies>
205211
<build>
206212
<resources>

server/src/main/java/cn/keking/utils/DownloadUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cn.keking.model.FileAttribute;
55
import cn.keking.model.FileType;
66
import cn.keking.model.ReturnResponse;
7+
import io.mola.galimatias.GalimatiasParseException;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

@@ -81,7 +82,6 @@ public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, Strin
8182
public static byte[] getBytesFromUrl(String urlStr) throws IOException {
8283
InputStream is = getInputStreamFromUrl(urlStr);
8384
if (is == null) {
84-
// urlStr = URLUtil.normalize(urlStr, true, true);
8585
is = getInputStreamFromUrl(urlStr);
8686
if (is == null) {
8787
logger.error("文件下载异常:url:{}", urlStr);
@@ -98,13 +98,14 @@ public static void saveBytesToOutStream(byte[] b, OutputStream os) throws IOExce
9898

9999
private static InputStream getInputStreamFromUrl(String urlStr) {
100100
try {
101-
URL url = new URL(urlStr);
101+
102+
URL url = io.mola.galimatias.URL.parse(urlStr).toJavaURL();
102103
URLConnection connection = url.openConnection();
103104
if (connection instanceof HttpURLConnection) {
104105
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
105106
}
106107
return connection.getInputStream();
107-
} catch (IOException e) {
108+
} catch (IOException | GalimatiasParseException e) {
108109
logger.warn("连接url异常:url:{}", urlStr);
109110
return null;
110111
}

0 commit comments

Comments
 (0)