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

}
๋ฐ˜์‘ํ˜•

+ Recent posts