import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @ClassName: FileSpaceUtil
* @Description:
* @Author:
* @Date:
**/
@Slf4j
public class FileSpaceUtil {
//查看磁盘空间总大小
public static double getTotalSpace(){
File file = new File("/");
long totalSpace = file.getTotalSpace();
double space = totalSpace * 1.0 / (1024 * 1024 * 1024);
BigDecimal two = new BigDecimal(space);
double size=two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return size;
}
//查看磁盘空间可用内存
public static double getUseSpace(){
File file = new File("/");
long usableSpace = file.getUsableSpace();
double space = usableSpace*1.0/(1024 * 1024 * 1024);
BigDecimal two = new BigDecimal(space);
double size=two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return size;
}
public static double getFolderSize(File folder){
long length= FileUtils.sizeOfDirectory(folder);
BigDecimal two = new BigDecimal(length*1.0/(1024*1024*1024));
double size=two.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return size;
}
public static String getFileSize(MultipartFile file) {
long fileBytes = file.getSize();
double size;
String unit;
if (fileBytes < 1024 * 1024) {
size = fileBytes / 1024.0;
unit = "KB";
} else {
size = fileBytes / 1024.0 / 1024;
unit = "MB";
}
BigDecimal bigDecimal = new BigDecimal(size);
double v = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
return v + unit;
}
public static String getFileSize(Long fileBytes) {
double size;
String unit;
if (fileBytes < 1024 * 1024) {
size = fileBytes / 1024.0;
unit = "KB";
} else {
size = fileBytes / 1024.0 / 1024;
unit = "MB";
}
BigDecimal bigDecimal = new BigDecimal(size);
double v = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
return v + unit;
}
/**
* 多目录文件压缩
* @param targetDir 要压缩的文件夹
* @param compressFilePath 压缩文件路径,如果null默认为文件名称
* @return
*/
public static boolean zip(String targetDir, String compressFilePath){
boolean ret=false;
try{
File sourceFile = new File(targetDir);
if(StringUtils.isBlank(compressFilePath)){
compressFilePath = sourceFile.getParentFile().getAbsolutePath() + File.separator + sourceFile.getName() + ".zip";
}
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(compressFilePath));
ret=zipToFile(sourceFile, sourceFile.getName(), zipOutputStream);
zipOutputStream.close();
return ret;
}catch (Exception e){
log.error("压缩文件异常!", e);
return ret;
}
}
private static boolean zipToFile(File sourceDir, String zipDirName, ZipOutputStream targetZipOut){
boolean ret=false;
if(!sourceDir.exists()){
log.debug("待压缩的目录"+sourceDir.getName()+"不存在");
return ret;
}
File[] files = sourceDir.listFiles();
if(files == null || files.length ==0){
return ret;
}
FileInputStream fis = null;
BufferedInputStream bis = null;
byte[] byteArray = new byte[1024*10];
try{
for (File file : files) {
if (file.isFile()) {
log.debug("开始压缩:{}", file.getAbsoluteFile());
ZipEntry zipEntry = new ZipEntry(zipDirName + File.separator + file.getName());
targetZipOut.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis, 1024 * 10);
int read;
while ((read = bis.read(byteArray, 0, 1024 * 10)) != -1) {
targetZipOut.write(byteArray, 0, read);
}
//如果需要删除源文件,则需要执行下面2句
//fis.close();
//fs[i].delete();
} else if (file.isDirectory()) {
log.debug("进入目录:{}", file.getAbsoluteFile());
zipToFile(file, zipDirName + File.separator + file.getName(), targetZipOut);
}
}//end for
ret=true;
return ret;
}catch (IOException e) {
log.error("打包异常!",e);
return ret;
} finally{
//关闭流
try {
if(null!=bis) bis.close();
if(null!=fis) fis.close();
} catch (IOException e) {
log.error("打包关闭流异常!",e);
}
}
}
/***
* 将String转文件文件
* @param musicInfo
* @param fileName
* @param filePath
* @param type 文件类型:例如:“.txt/.log”
* @throws IOException
*/
public static void writeToText(String musicInfo, String fileName,String filePath,String type) throws IOException {
// 生成的文件路径
String path = filePath + fileName + type;
File file = new File(path);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
// write 解决中文乱码问题
// FileWriter fw = new FileWriter(file, true);
OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(musicInfo);
bw.flush();
bw.close();
fw.close();
}
public static String fileToString(String path){
try {
File file =new File(path);
if (!file.exists()){
throw new Error("error");
}
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf8");
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
String tempTxt = "";
while((lineTxt = bufferedReader.readLine()) != null){
// System.getProperty("line.separator") 为换行符号
tempTxt+=lineTxt + System.getProperty("line.separator");
}
read.close();
return tempTxt;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 文件下载
* @param response
* @param path 文件路径
*/
public static void download(HttpServletResponse response,String path) {
log.info("下载文件路径"+path);
String fileName=path.substring(path.lastIndexOf("/")+1,path.length());
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
File file = new File(path);
if(!file.exists()){
response.setStatus(HttpStatus.NOT_FOUND.value());
return;
}
FileInputStream fi=new FileInputStream(file);
response.setContentType("application/octet-stream; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
IoUtil.copy(fi, response.getOutputStream());
} catch (IOException e) {
log.info("文件下载错误");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240