|
@@ -83,32 +83,37 @@ public class FileUtils {
|
|
|
if (StrUtil.isNotBlank(originalName)) {
|
|
|
String extName = FileNameUtil.extName(originalName);
|
|
|
//return StrUtil.isBlank(extName) ? sha256Hex : sha256Hex + "." + extName;
|
|
|
- return StrUtil.isBlank(extName) ? sha256Hex : DateUtil.format(new Date(), "yyyyMMDDHHmmss")+originalName;
|
|
|
+ return StrUtil.isBlank(extName) ? sha256Hex : originalName;
|
|
|
}
|
|
|
// 情况二:基于 content 计算
|
|
|
return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content));
|
|
|
}
|
|
|
|
|
|
- public static double getFileSizeMB(String fileUrl){
|
|
|
- HttpURLConnection conn = null;
|
|
|
+ public static long getFileSizeMB(String fileUrl){
|
|
|
try {
|
|
|
URL url = new URL(fileUrl);
|
|
|
- conn = (HttpURLConnection) url.openConnection();
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
conn.setRequestMethod("HEAD");
|
|
|
- int code = conn.getResponseCode();
|
|
|
- if (code != HttpURLConnection.HTTP_OK) {
|
|
|
- throw new IOException("HTTP错误码: " + code);
|
|
|
+ conn.setRequestProperty("Accept-Encoding", "identity"); // 禁用压缩
|
|
|
+
|
|
|
+ // 尝试解析Content-Length头
|
|
|
+ String contentLength = conn.getHeaderField("Content-Length");
|
|
|
+ if (contentLength != null) {
|
|
|
+ return Long.parseLong(contentLength);
|
|
|
}
|
|
|
- String length = conn.getHeaderField("Content-Length");
|
|
|
- if (length == null) {
|
|
|
- throw new IOException("Content-Length未找到");
|
|
|
+
|
|
|
+ // 处理分块传输或未知大小
|
|
|
+ if ("chunked".equalsIgnoreCase(conn.getHeaderField("Transfer-Encoding"))) {
|
|
|
+ System.out.println("分块传输编码,无法直接获取大小");
|
|
|
+ return -1;
|
|
|
}
|
|
|
- long bytes = Long.parseLong(length);
|
|
|
- return bytes / (1024.0 * 1024.0);
|
|
|
- } catch (Exception e){
|
|
|
- throw new ServiceException(GlobalErrorCodeConstants.FILE_SIZE_ERROR);
|
|
|
- } finally {
|
|
|
- if (conn != null) conn.disconnect();
|
|
|
+
|
|
|
+ conn.disconnect();
|
|
|
+ return -1; // 其他未知情况
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|