|
@@ -284,18 +284,68 @@ public class FileUtils {
|
|
|
*/
|
|
|
public static String getFileTypeFromUrl(String fileUrl) throws IOException {
|
|
|
URL url = new URL(fileUrl);
|
|
|
- try (InputStream inputStream = url.openStream()) {
|
|
|
- Tika tika = new Tika();
|
|
|
- String mimeType = tika.detect(inputStream);
|
|
|
- if (mimeType.startsWith("image/")) {
|
|
|
+
|
|
|
+ // 设置连接属性
|
|
|
+ java.net.HttpURLConnection connection = null;
|
|
|
+ try {
|
|
|
+ // 检查是否需要设置代理
|
|
|
+ String proxyHost = System.getProperty("http.proxyHost");
|
|
|
+ String proxyPort = System.getProperty("http.proxyPort");
|
|
|
+
|
|
|
+ if (StringUtils.isNotEmpty(proxyHost) && StringUtils.isNotEmpty(proxyPort)) {
|
|
|
+ // 使用系统配置的代理
|
|
|
+ java.net.Proxy proxy = new java.net.Proxy(
|
|
|
+ java.net.Proxy.Type.HTTP,
|
|
|
+ new java.net.InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))
|
|
|
+ );
|
|
|
+ connection = (java.net.HttpURLConnection) url.openConnection(proxy);
|
|
|
+ } else {
|
|
|
+ connection = (java.net.HttpURLConnection) url.openConnection();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置连接参数
|
|
|
+ connection.setConnectTimeout(5000); // 5秒连接超时
|
|
|
+ connection.setReadTimeout(5000); // 5秒读取超时
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setDoInput(true);
|
|
|
+
|
|
|
+ // 尝试连接
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ try (InputStream inputStream = connection.getInputStream()) {
|
|
|
+ Tika tika = new Tika();
|
|
|
+ String mimeType = tika.detect(inputStream);
|
|
|
+ if (mimeType.startsWith("image/")) {
|
|
|
+ return "image";
|
|
|
+ } else if (mimeType.startsWith("audio/")) {
|
|
|
+ return "audio";
|
|
|
+ } else if (mimeType.startsWith("video/")) {
|
|
|
+ return "video";
|
|
|
+ } else {
|
|
|
+ return "other";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (java.net.SocketException e) {
|
|
|
+ // 处理网络权限错误
|
|
|
+ System.err.println("网络连接权限被拒绝: " + e.getMessage());
|
|
|
+ // 尝试从URL路径推断文件类型
|
|
|
+ String path = url.getPath();
|
|
|
+ String extension = FilenameUtils.getExtension(path).toLowerCase();
|
|
|
+
|
|
|
+ // 根据扩展名判断文件类型
|
|
|
+ if (MimeTypeUtils.isImage(extension)) {
|
|
|
return "image";
|
|
|
- } else if (mimeType.startsWith("audio/")) {
|
|
|
+ } else if (MimeTypeUtils.isAudio(extension)) {
|
|
|
return "audio";
|
|
|
- } else if (mimeType.startsWith("video/")) {
|
|
|
+ } else if (MimeTypeUtils.isVideo(extension)) {
|
|
|
return "video";
|
|
|
} else {
|
|
|
return "other";
|
|
|
}
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|