aboutsummaryrefslogtreecommitdiff
path: root/juick-common
diff options
context:
space:
mode:
Diffstat (limited to 'juick-common')
-rw-r--r--juick-common/src/main/java/com/juick/server/util/HttpUtils.java36
1 files changed, 26 insertions, 10 deletions
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java
index dbdbc062..9f356aa5 100644
--- a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java
+++ b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java
@@ -22,6 +22,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -30,6 +33,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.Iterator;
import java.util.UUID;
/**
@@ -41,9 +45,15 @@ public class HttpUtils {
public static URI receiveMultiPartFile(MultipartFile attach, String tmpDir) throws IOException {
if (attach != null && !attach.isEmpty()) {
- InputStream attachmentStream = new BufferedInputStream(attach.getInputStream());
- String guessedContentType = URLConnection.guessContentTypeFromStream(attachmentStream);
- String attachmentType = attachmentTypeFromContentType(guessedContentType);
+ ImageInputStream iis = ImageIO.createImageInputStream(attach.getInputStream());
+ Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
+
+ String format = StringUtils.EMPTY;
+ while (readers.hasNext()) {
+ ImageReader read = readers.next();
+ format = read.getFormatName();
+ }
+ String attachmentType = attachmentTypeFromFormat(format);
if (attachmentType.equals("jpg") || attachmentType.equals("png")) {
String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
try {
@@ -59,17 +69,25 @@ public class HttpUtils {
return URI.create(StringUtils.EMPTY);
}
- private static String attachmentTypeFromContentType(String mime) throws IOException {
- if (mime != null && mime.equals("image/jpeg")) {
+ private static String attachmentTypeFromFormat(String format) throws IOException {
+ if (format != null && format.equals("JPEG")) {
return "jpg";
- } else if (mime != null && mime.equals("image/png")) {
+ } else if (format != null && format.equals("png")) {
return "png";
} else {
- throw new IOException("Wrong file type: " + mime);
+ throw new IOException("Wrong file type: " + format);
}
}
public static URI downloadImage(URL url, String tmpDir) throws IOException {
+ ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());
+ Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
+
+ String format = StringUtils.EMPTY;
+ while (readers.hasNext()) {
+ ImageReader read = readers.next();
+ format = read.getFormatName();
+ }
URLConnection urlConn;
try {
urlConn = url.openConnection();
@@ -79,9 +97,7 @@ public class HttpUtils {
}
try (InputStream is = new BufferedInputStream(urlConn.getInputStream())) {
- String mime = URLConnection.guessContentTypeFromStream(is);
-
- String attachmentType = attachmentTypeFromContentType(mime);
+ String attachmentType = attachmentTypeFromFormat(format);
String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
Files.copy(is, Paths.get(tmpDir, attachmentFName));