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 {<!-- --> //View the total disk space size 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; } //View disk space and available memory 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; } /** *Multi-directory file compression * @param targetDir The folder to be compressed * @param compressFilePath compressed file path, if null defaults to the file name * @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("Compressed file exception!", e); return ret; } } private static boolean zipToFile(File sourceDir, String zipDirName, ZipOutputStream targetZipOut){<!-- --> boolean ret=false; if(!sourceDir.exists()){<!-- --> log.debug("Directory to be compressed" + sourceDir.getName() + "Does not exist"); 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("Start compression:{}", file.getAbsoluteFile()); ZipEntry zipEntry = new ZipEntry(zipDirName + File.separator + file.getName()); targetZipOut.putNextEntry(zipEntry); //Read the file to be compressed and write it into the compressed package 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); } //If you need to delete the source file, you need to execute the following 2 sentences //fis.close(); //fs[i].delete(); } else if (file.isDirectory()) {<!-- --> log.debug("Enter directory:{}", file.getAbsoluteFile()); zipToFile(file, zipDirName + File.separator + file.getName(), targetZipOut); } }//end for ret=true; return ret; }catch (IOException e) {<!-- --> log.error("Packaging exception!",e); return ret; } finally{<!-- --> //Close the stream try {<!-- --> if(null!=bis) bis.close(); if(null!=fis) fis.close(); } catch (IOException e) {<!-- --> log.error("Packaging closing stream exception!",e); } } } /*** * Convert String to file * @param musicInfo * @param fileName * @param filePath * @param type File type: For example: ".txt/.log" * @throwsIOException */ public static void writeToText(String musicInfo, String fileName,String filePath,String type) throws IOException {<!-- --> // Generated file path String path = filePath + fileName + type; File file = new File(path); if (!file.exists()) {<!-- --> file.getParentFile().mkdirs(); } file.createNewFile(); // write solves the problem of Chinese garbled characters // 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") is the newline symbol tempTxt + =lineTxt + System.getProperty("line.separator"); } read.close(); return tempTxt; } catch (IOException e) {<!-- --> throw new RuntimeException(e); } } /** * Download Document * @param response * @param path file path */ public static void download(HttpServletResponse response,String path) {<!-- --> log.info("Download file path" + 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("File download error"); } } }