package com.study.controller;
import org.apach.commons.io.FileUtils;
import org.springFamework.http.HttpHeaders;
import org.springFamework.http.HttpStatus;
import org.springFamework.http.MediaType;
import org.springFamework.http.ResponseEntity;
@Controller
public class AttachFileController {
@RequestMapping("/pdfView")
public ResponseEntity<byte[]> pdfView(HttpServletRequest request, HttpServletResponse) throws Exception{
request.setCharacterEncoding("utf-8");
String fileId = new String(request.getParameter("fileId".getBytes("8859_1", "KSC5601");
// fileId ์๋ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ
// fileId์ ํด๋นํ๋ ํ์ผ ์๋ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ
String filePath = ${FILES_ROOT_PATH} + fileId;
String tempFile = new File(filePath);
byte[] pdfBytes;
try {
pdfBytes = org.apache.commons.io.FileUtils.readFileToByteArray(tempFile);
} catch (Exception e){
e.printStackTrace();
String error = "<script tye='text/javascript'> alert('File Not Found');</script>";
byte[] arrorBytes = error.getBytes(StandardCharsets.UTF_8);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorBytes);
}
HttpHeaders = headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDispositionFormData("filename", "example.pdf");
headers.setContentLength(pdfBytes.length);
//ํ์ผ์ด ๋ค์ด๋ก๋ ๋์ง ์๊ณ ๋ธ๋ผ์ฐ์ ๋ด์ ๋ณด์ด๋๋ก ํจ
headers.set("Content-Disposition", "inline; filename=example.pdf"
return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
}
}
๋ฐ์ํ