package com.chint.tools;
import java.io.*;
/**
*
*
* @vsrsion 1.0
**/
public class FileDownloadUtil {
/**
* 지정된 파일의 바이트 배열을 출력합니다.
*
* @param filePath 파일 경로
* @param os
* @return
*/
public static void writeBytes(String filePath, OutputStream os) throws IOException
{
FileInputStream fis = null;
try
{
File file = new File(filePath);
if (!file.exists())
{
throw new FileNotFoundException(filePath);
}
fis = new FileInputStream(file);
byte[] b = new byte;
int length;
while ((length = fis.read(b)) > 0)
{
os.write(b, 0, length);
}
}
catch (IOException e)
{
throw e;
}
finally
{
if (os != null)
{
try
{
os.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
}
- 대상 이 리소스/결과를 이 PDF로 다운로드하세요.
- 다운로드 작업에는 위의 클래스를 사용하세요.
@GetMapping("/getDiscPdfFile")
public void downloadPdf(String discResult, HttpServletResponse response, HttpServletRequest request) {
if (discResult == null) {
Result.error("들어오는 디스크 측정값은 null일 수 없습니다.");
} else if (!(discResult.equals("C") ||
discResult.equals("CD") ||
discResult.equals("CS") ||
discResult.equals("D") ||
discResult.equals("DC") ||
discResult.equals("DI") ||
discResult.equals("I") ||
discResult.equals("ID") ||
discResult.equals("IS") ||
discResult.equals("S") ||
discResult.equals("SC") ||
discResult.equals("SI") ||
discResult.equals("34-Ability")
)) {
Result.error("들어오는 디스크 측정값에 해당 유형이 없습니다.");
}
String fileNameStr = null;
if (discResult.equals("34-Ability")) {
fileNameStr = "34-Ability.pdf";
} else {
fileNameStr = "DISC-ResultReport-" + discResult + ".pdf";
}
String pdfFilePath = DiscResultController.class.getResource("/DISCResult/" + fileNameStr).getFile();
try {
FileDownloadUtil.writeBytes(pdfFilePath, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
log.error(" , e);
}
}
- 포스트맨으로 최종 테스트
다운로드 완료



