69 lines
1.9 KiB
Java
69 lines
1.9 KiB
Java
package itn.com.cmm.util;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.OutputStream;
|
|
import java.net.URLEncoder;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
/**
|
|
*
|
|
* @author : 이호영
|
|
* @fileName : FileUtil.java
|
|
* @date : 2023.04.06
|
|
* @description : 파일 관련 유틸
|
|
* ===========================================================
|
|
* DATE AUTHOR NOTE
|
|
* ----------------------------------------------------------- *
|
|
* 2023.04.06 이호영 최초 생성
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
public final class FileUtil {
|
|
|
|
/**
|
|
* @methodName : downLoad
|
|
* @author : 이호영
|
|
* @date : 2023.04.06
|
|
* @description : 파일 다운로드
|
|
* @param response
|
|
* @param fileInfo
|
|
* @param fileName
|
|
* @throws Exception
|
|
*/
|
|
public static void downLoad(HttpServletResponse response, String fileInfo, String fileNameP) throws Exception {
|
|
|
|
|
|
try {
|
|
String path = fileInfo; // 경로에 접근할 때 역슬래시('\') 사용
|
|
|
|
File file = new File(path);
|
|
|
|
String fileName = "";
|
|
if(StringUtils.isNotEmpty(fileNameP))
|
|
fileName = URLEncoder.encode(fileNameP,"UTF-8").replaceAll("\\+", "%20");
|
|
else
|
|
fileName = file.getName();
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName); // 다운로드 되거나 로컬에 저장되는 용도로 쓰이는지를 알려주는 헤더
|
|
|
|
FileInputStream fileInputStream = new FileInputStream(path); // 파일 읽어오기
|
|
OutputStream out = response.getOutputStream();
|
|
|
|
int read = 0;
|
|
byte[] buffer = new byte[1024];
|
|
while ((read = fileInputStream.read(buffer)) != -1) { // 1024바이트씩 계속 읽으면서 outputStream에 저장, -1이 나오면 더이상 읽을 파일이 없음
|
|
out.write(buffer, 0, read);
|
|
}
|
|
} catch (Exception e) {
|
|
throw new Exception("download error");
|
|
}
|
|
}
|
|
|
|
|
|
}
|