From e4be013505c569548e5c9d76c49b7279a81ddb3a Mon Sep 17 00:00:00 2001 From: M*C*O Date: Tue, 7 Jul 2020 15:37:15 +0200 Subject: [PATCH] Fix failed MimeType detection (#277) * Fix failed MimeType detection * Fix failed MimeType detection * Fix formatting --- src/com/sheepit/client/Server.java | 4 ++-- src/com/sheepit/client/Utils.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/com/sheepit/client/Server.java b/src/com/sheepit/client/Server.java index b741af8..43b0603 100644 --- a/src/com/sheepit/client/Server.java +++ b/src/com/sheepit/client/Server.java @@ -492,8 +492,8 @@ public class Server extends Thread { this.log.debug(checkpoint, "Server::HTTPSendFile(" + surl + "," + file1 + ")"); try { - String fileMimeType = Files.probeContentType(Paths.get(file1)); - + String fileMimeType = Utils.findMimeType(file1); + MediaType MEDIA_TYPE = MediaType.parse(fileMimeType); // e.g. "image/png" RequestBody uploadContent = new MultipartBody.Builder().setType(MultipartBody.FORM) diff --git a/src/com/sheepit/client/Utils.java b/src/com/sheepit/client/Utils.java index cb28cb1..2d7daee 100644 --- a/src/com/sheepit/client/Utils.java +++ b/src/com/sheepit/client/Utils.java @@ -19,11 +19,14 @@ package com.sheepit.client; +import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; +import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Paths; import java.security.DigestInputStream; @@ -212,4 +215,17 @@ public class Utils { return false; } + + public static String findMimeType(String file) throws IOException { + String mimeType = Files.probeContentType(Paths.get(file)); + if (mimeType == null) { + InputStream stream = new BufferedInputStream(new FileInputStream(file)); + mimeType = URLConnection.guessContentTypeFromStream(stream); + } + if (mimeType == null) { + mimeType = URLConnection.guessContentTypeFromName(file); + } + + return mimeType; + } }