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(); } }