Fix failed MimeType detection (#277)
* Fix failed MimeType detection * Fix failed MimeType detection * Fix formatting
This commit is contained in:
@@ -492,7 +492,7 @@ 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"
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user