提交 0145791c authored 作者: zhangshuang's avatar zhangshuang

文件大小

上级 ce68cb6f
...@@ -30,4 +30,6 @@ public class FileSave { ...@@ -30,4 +30,6 @@ public class FileSave {
private String fileName;//传入名称 private String fileName;//传入名称
@ApiModelProperty(name = "path",value = "文件真正存储路径",notes = "文件真正存储路径",example = "/file/uploads/1(1).zip") @ApiModelProperty(name = "path",value = "文件真正存储路径",notes = "文件真正存储路径",example = "/file/uploads/1(1).zip")
private String path;//文件存储路径 private String path;//文件存储路径
@ApiModelProperty(name = "fileSize",value = "文件大小",notes = "文件大小",example = "973kb")
private String fileSize;//文件存储路径
} }
...@@ -158,6 +158,7 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -158,6 +158,7 @@ public class FileUploadServiceImpl implements FileUploadService {
String name = mergeRequest.getName(); String name = mergeRequest.getName();
fileSave.setFileName(name);//放入文件名称 fileSave.setFileName(name);//放入文件名称
fileSave.setPath(fileAddress + "/" + name);//文件存储地址 fileSave.setPath(fileAddress + "/" + name);//文件存储地址
fileSave.setFileSize(fileSize(fileAddress + "/" + name));
FileOutputStream fileOutputStream = null; //合成后的文件 FileOutputStream fileOutputStream = null; //合成后的文件
try { try {
String savePath = rename(fileAddress + "/" + name,0); String savePath = rename(fileAddress + "/" + name,0);
...@@ -180,6 +181,8 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -180,6 +181,8 @@ public class FileUploadServiceImpl implements FileUploadService {
} }
//删除md5目录,及临时文件 //删除md5目录,及临时文件
deleteFile(new File(fileAddress + "/" + md5File)); deleteFile(new File(fileAddress + "/" + md5File));
fileOutputStream.flush();
} catch (Exception e) { } catch (Exception e) {
log.error(fileSave.getPath() + "文件保存失败"); log.error(fileSave.getPath() + "文件保存失败");
e.printStackTrace(); e.printStackTrace();
...@@ -244,4 +247,22 @@ public class FileUploadServiceImpl implements FileUploadService { ...@@ -244,4 +247,22 @@ public class FileUploadServiceImpl implements FileUploadService {
//删除空文件夹 for循环已经把上一层节点的目录清空。 //删除空文件夹 for循环已经把上一层节点的目录清空。
file.delete(); file.delete();
} }
/**
* 文件大小
*/
public static String fileSize(String path){
File file = new File(path);
long fileLength = file.length();
double length = (double) fileLength;
if(length < 1024 * 1024){
return String.format("%.2f",length/1024) + "k";
}else {
return String.format("%.2f",length/(1024 * 1024)) + "MB";
}
}
public static void main(String[] args) {
System.out.println(fileSize("D:\\Users\\ljj\\software\\ftp\\te.zip"));
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论