geoinfo_eGov_work/src/main/java/geoinfo/util/FileUtil.java

88 lines
2.2 KiB
Java
Raw Normal View History

2024-02-08 02:26:11 +00:00
package geoinfo.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.jfree.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
public class FileUtil {
private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
private static final int BUFF_SIZE = 2048;
/**
*
* @param file
* @param newName
* @param stordFilePath
*/
public static void writeUploadedFile(MultipartFile file, String newName, String savePath) throws Exception{
InputStream stream = null;
OutputStream bos = null;
stream = file.getInputStream();
File cFile = new File(savePath);
if (!cFile.isDirectory()) {
boolean _flag = cFile.mkdir();
if(_flag) throw new IOException("Directory creation Failed ");
}
bos = new FileOutputStream(savePath + newName);
int bytesRead = 0;
byte[] buffer = new byte[BUFF_SIZE];
while ((bytesRead = stream.read(buffer, 0, BUFF_SIZE)) != -1) {
bos.write(buffer, 0, bytesRead);
}
if (bos != null) {
try {
bos.close();
} catch (IOException ignore) {
logger.debug("error", ignore);
} catch (NumberFormatException ignore) {
logger.debug("error", ignore);
} catch (IndexOutOfBoundsException ignore) {
logger.debug("error", ignore);
} catch (Exception ignore) {
logger.debug("error", ignore);
}
}
if (stream != null) {
try {
stream.close();
} catch (IOException ignore) {
logger.debug("error", ignore);
} catch (NumberFormatException ignore) {
logger.debug("error", ignore);
} catch (IndexOutOfBoundsException ignore) {
logger.debug("error", ignore);
} catch (Exception ignore) {
logger.debug("error", ignore);
}
}
bos.close();
}
/**
*
* @param savePath
* @param fileName
*/
public static void deleteFile(String savePath, String fileName) throws Exception{
File file = new File(savePath + fileName);
if(file.exists()) file.delete();
}
}