All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.nichetoolkit.rest.util.FileUtils Maven / Gradle / Ivy

The newest version!
package io.github.nichetoolkit.rest.util;

import io.github.nichetoolkit.rest.constant.UtilConstants;
import io.github.nichetoolkit.rest.error.often.FileCopyException;
import io.github.nichetoolkit.rest.error.often.FileCreateException;
import io.github.nichetoolkit.rest.helper.FileHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Optional;

/**
 * FileUtils
 * 

The file utils class.

* @author Cyan ([email protected]) * @see lombok.extern.slf4j.Slf4j * @see java.lang.SuppressWarnings * @since Jdk1.8 */ @Slf4j @SuppressWarnings("SameNameButDifferent") public class FileUtils { /** * createFile *

The create file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @return {@link java.io.File}

The create file return object is File type.

* @see java.lang.String * @see java.io.File */ public static File createFile(final String path) { try { return FileHelper.createFile(path); } catch (FileCreateException exception) { log.error("It is failed during creating file !", exception); GeneralUtils.printStackTrace(exception); } return null; } /** * createFile *

The create file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @param name {@link java.lang.String}

The name parameter is String type.

* @return {@link java.io.File}

The create file return object is File type.

* @see java.lang.String * @see java.io.File */ public static File createFile(final String path, final String name) { try { return FileHelper.createFile(path,name); } catch (FileCreateException exception) { log.error("It is failed during creating file!", exception); GeneralUtils.printStackTrace(exception); } return null; } /** * createFile *

The create file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @param nameMap {@link java.util.Map}

The name map parameter is Map type.

* @return {@link java.io.File}

The create file return object is File type.

* @see java.lang.String * @see java.util.Map * @see java.io.File */ public static File createFile(final String path, Map nameMap) { try { return FileHelper.createFile(path, nameMap); } catch (FileCreateException exception) { log.error("It is failed during creating file with nameMap!", exception); GeneralUtils.printStackTrace(exception); } return null; } /** * createFile *

The create file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @param name {@link java.lang.String}

The name parameter is String type.

* @param ext {@link java.lang.String}

The ext parameter is String type.

* @return {@link java.io.File}

The create file return object is File type.

* @see java.lang.String * @see java.io.File */ public static File createFile(final String path, final String name, final String ext) { try { return FileHelper.createFile(path,name,ext); } catch (FileCreateException exception) { log.error("It is failed during creating file!", exception); GeneralUtils.printStackTrace(exception); } return null; } /** * createFile *

The create file method.

* @param file {@link java.io.File}

The file parameter is File type.

* @return {@link java.io.File}

The create file return object is File type.

* @see java.io.File */ public static File createFile(final File file) { try { return FileHelper.createFile(file); } catch (FileCreateException exception) { log.error("It is failed during creating of file!", exception); GeneralUtils.printStackTrace(exception); } return null; } /** * copyFile *

The copy file method.

* @param srcFile {@link java.io.File}

The src file parameter is File type.

* @param targetFile {@link java.io.File}

The target file parameter is File type.

* @see java.io.File */ public static void copyFile(final File srcFile,final File targetFile) { try { FileHelper.copyFile(srcFile, targetFile); } catch (FileCopyException exception) { log.error("It is failed during copying of file!", exception); GeneralUtils.printStackTrace(exception); } } /** * copyFile *

The copy file method.

* @param srcPath {@link java.lang.String}

The src path parameter is String type.

* @param targetPath {@link java.lang.String}

The target path parameter is String type.

* @see java.lang.String */ public static void copyFile(final String srcPath,final String targetPath) { File srcFile = new File(srcPath); File targetFile = new File(targetPath); copyFile(srcFile, targetFile); } /** * deleteFile *

The delete file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @return {@link java.lang.Boolean}

The delete file return object is Boolean type.

* @see java.lang.String * @see java.lang.Boolean */ public static Boolean deleteFile(final String path) { return FileHelper.deleteFile(path); } /** * deleteFile *

The delete file method.

* @param file {@link java.io.File}

The file parameter is File type.

* @return {@link java.lang.Boolean}

The delete file return object is Boolean type.

* @see java.io.File * @see java.lang.Boolean */ public static Boolean deleteFile(final File file) { return FileHelper.deleteFile(file); } /** * clearFile *

The clear file method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @return {@link java.lang.Boolean}

The clear file return object is Boolean type.

* @see java.lang.String * @see java.lang.Boolean */ public static Boolean clearFile(final String path) { return FileHelper.clearFile(path); } /** * cacheFile *

The cache file method.

* @param cachePath {@link java.lang.String}

The cache path parameter is String type.

* @param file {@link org.springframework.web.multipart.MultipartFile}

The file parameter is MultipartFile type.

* @return {@link java.io.File}

The cache file return object is File type.

* @see java.lang.String * @see org.springframework.web.multipart.MultipartFile * @see java.io.File */ public static File cacheFile(final String cachePath, MultipartFile file) { createPath(cachePath); String originalFilename = file.getOriginalFilename(); final String path = cachePath + File.separator + originalFilename; File cacheFile = new File(path); IoStreamUtils.transfer(file,cacheFile); return cacheFile; } /** * createPath *

The create path method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @return {@link java.lang.String}

The create path return object is String type.

* @see java.lang.String * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static String createPath(final String path) { File filePath = new File(path); if (!filePath.exists()) { filePath.mkdirs(); } return filePath.getPath(); } /** * createPath *

The create path method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @param child {@link java.lang.String}

The child parameter is String type.

* @return {@link java.lang.String}

The create path return object is String type.

* @see java.lang.String * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static String createPath(final String path,final String child) { File allPath = new File(path, child); if (!allPath.exists()) { allPath.mkdirs(); } return allPath.getPath(); } /** * clear *

The clear method.

* @param root {@link java.io.File}

The root parameter is File type.

* @return boolean

The clear return object is boolean type.

* @see java.io.File */ public static boolean clear(final File root) { if (root != null && root.exists()) { if (root.isDirectory()) { File[] children = root.listFiles(); if (children != null) { for (File child : children) { clear(child); } } } return root.delete(); } return false; } /** * clear *

The clear method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @return boolean

The clear return object is boolean type.

* @see java.lang.String */ public static boolean clear(final String path) { File file = new File(path); return clear(file); } /** * delete *

The delete method.

* @param path {@link java.lang.String}

The path parameter is String type.

* @see java.lang.String * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static void delete(final String path) { File file = new File(path); if (file.exists()) { file.delete(); } } /** * delete *

The delete method.

* @param file {@link java.io.File}

The file parameter is File type.

* @see java.io.File * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static void delete(final File file) { if (file.isFile()) { file.delete(); } } /** * write *

The write method.

* @param file {@link java.io.File}

The file parameter is File type.

* @param data byte

The data parameter is byte type.

* @see java.io.File * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static void write(final File file, byte[] data) { if (!file.exists()) { file.getParentFile().mkdirs(); } try (FileOutputStream fileOutputStream = new FileOutputStream(file)){ fileOutputStream.write(data); fileOutputStream.flush(); } catch (IOException exception) { GeneralUtils.printStackTrace(exception); log.error(exception.getMessage(), exception); } } /** * read *

The read method.

* @param file {@link java.io.File}

The file parameter is File type.

* @return byte

The read return object is byte type.

* @see java.io.File * @see java.lang.SuppressWarnings */ @SuppressWarnings("ResultOfMethodCallIgnored") public static byte[] read(final File file) { long fileLength = file.length(); byte[] fileContent = new byte[(int) fileLength]; try (FileInputStream fileInputStream = new FileInputStream(file)) { fileInputStream.read(fileContent); return fileContent; } catch (Exception e) { log.error(e.getMessage(), e); return new byte[0]; } } /** * suffix *

The suffix method.

* @param originalName {@link java.lang.String}

The original name parameter is String type.

* @return {@link java.lang.String}

The suffix return object is String type.

* @see java.lang.String */ public static String suffix(final String originalName){ if(GeneralUtils.isEmpty(originalName)){ return ""; } if (!originalName.contains(".")) { return ""; } return originalName.substring(originalName.lastIndexOf(".") + 1); } /** * mediaType *

The media type method.

* @param filename {@link java.lang.String}

The filename parameter is String type.

* @return {@link org.springframework.http.MediaType}

The media type return object is MediaType type.

* @see java.lang.String * @see org.springframework.http.MediaType */ public static MediaType mediaType(final String filename){ Optional mediaTypeOptional = MediaTypeFactory.getMediaType(filename); return mediaTypeOptional.orElse(MediaType.APPLICATION_OCTET_STREAM); } /** * filename *

The filename method.

* @param originalName {@link java.lang.String}

The original name parameter is String type.

* @return {@link java.lang.String}

The filename return object is String type.

* @see java.lang.String */ public static String filename(final String originalName){ if(GeneralUtils.isEmpty(originalName)){ return ""; } if (!originalName.contains(".")) { return originalName; } return originalName.substring(0,originalName.lastIndexOf(".")); } /** * fileSize *

The file size method.

* @param fileSize {@link java.lang.Long}

The file size parameter is Long type.

* @return {@link java.lang.String}

The file size return object is String type.

* @see java.lang.Long * @see java.lang.String */ public static String fileSize(final Long fileSize) { DecimalFormat decimalFormat = new DecimalFormat("#.00"); String fileSizeString; String wrongSize = "0B"; if (fileSize == 0) { return wrongSize; } if (fileSize < 1024) { fileSizeString = decimalFormat.format((double) fileSize) + "B"; } else if (fileSize < 1048576) { fileSizeString = decimalFormat.format((double) fileSize / 1024) + "KB"; } else if (fileSize < 1073741824) { fileSizeString = decimalFormat.format((double) fileSize / 1048576) + "MB"; } else { fileSizeString = decimalFormat.format((double) fileSize / 1073741824) + "GB"; } return fileSizeString; } /** * attachment *

The attachment method.

* @param request {@link javax.servlet.http.HttpServletRequest}

The request parameter is HttpServletRequest type.

* @param response {@link javax.servlet.http.HttpServletResponse}

The response parameter is HttpServletResponse type.

* @param fileName {@link java.lang.String}

The file name parameter is String type.

* @see javax.servlet.http.HttpServletRequest * @see javax.servlet.http.HttpServletResponse * @see java.lang.String */ public static void attachment(HttpServletRequest request, HttpServletResponse response, String fileName) { String browser; try { if (request != null && request.getHeader(UtilConstants.USER_AGENT_HEADER) != null) { browser = request.getHeader(UtilConstants.USER_AGENT_HEADER); if (browser.contains("MSIE 6.0") || browser.contains("MSIE 7.0")) { // IE6, IE7 response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_CONTENT + URLEncoder.encode(fileName, StandardCharsets.ISO_8859_1.name())); } else if (browser.contains("MSIE 8.0")) { // IE8 response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_CONTENT + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); } else if (browser.contains("MSIE 9.0")) { // IE9 response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_CONTENT + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); } else if (browser.contains("Chrome")) { // Google response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_UTF_8_CONTENT + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); } else if (browser.contains("Safari")) { // Safari response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_CONTENT + URLEncoder.encode(fileName, StandardCharsets.ISO_8859_1.name())); } else { // Firefox or other response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_UTF_8_CONTENT + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); } } else { response.addHeader(UtilConstants.CONTENT_HEADER, UtilConstants.FILENAME_UTF_8_CONTENT + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); } } catch (IOException exception) { GeneralUtils.printStackTrace(exception); log.error(exception.getMessage(), exception); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy