|
@@ -57,10 +57,10 @@ import org.springblade.business.service.IImageClassificationFileService;
|
|
|
import org.springblade.core.boot.ctrl.BladeController;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.text.DecimalFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -583,6 +583,14 @@ public class ImageClassificationFileController extends BladeController {
|
|
|
try {
|
|
|
ImageClassificationFile newFile = this.copyBeanData(fileVO, true);
|
|
|
this.setUserData(null, newFile, false);
|
|
|
+ String[] urls = fileVO.getImageUrl().split(",");
|
|
|
+ Long total = 0L;
|
|
|
+ for (String url : urls) {
|
|
|
+ long urlSize = getResourceLength(url);
|
|
|
+ total += urlSize;
|
|
|
+ }
|
|
|
+ String size = formatSize(total);
|
|
|
+ newFile.setFileSize(size);
|
|
|
//落库数据
|
|
|
return R.status(this.imageClassificationFileService.save(newFile));
|
|
|
} catch (Exception e) {
|
|
@@ -680,4 +688,39 @@ public class ImageClassificationFileController extends BladeController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param urlStr
|
|
|
+ * @return 返回Url资源大小
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public long getResourceLength(String urlStr) throws IOException {
|
|
|
+ URL url=new URL(urlStr);
|
|
|
+ URLConnection urlConnection=url.openConnection();
|
|
|
+ urlConnection.connect();
|
|
|
+ //返回响应报文头字段Content-Length的值
|
|
|
+ return urlConnection.getContentLength();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 根据字节返回文件大小
|
|
|
+ */
|
|
|
+ private String formatSize(long fileS) {
|
|
|
+ DecimalFormat df = new DecimalFormat("#.00");
|
|
|
+ String fileSizeString = "";
|
|
|
+ String wrongSize = "0B";
|
|
|
+ if (fileS == 0) {
|
|
|
+ return wrongSize;
|
|
|
+ }
|
|
|
+ if (fileS < 1024) {
|
|
|
+ fileSizeString = df.format((double) fileS) + "B";
|
|
|
+ } else if (fileS < 1048576) {
|
|
|
+ fileSizeString = df.format((double) fileS / 1024) + "KB";
|
|
|
+ } else if (fileS < 1073741824) {
|
|
|
+ fileSizeString = df.format((double) fileS / 1048576) + "MB";
|
|
|
+ } else {
|
|
|
+ fileSizeString = df.format((double) fileS / 1073741824) + "GB";
|
|
|
+ }
|
|
|
+ return fileSizeString;
|
|
|
+ }
|
|
|
+
|
|
|
}
|