본문 바로가기
Programing Language/Java

[Java] Response File Headers

by pcm9881 2023. 3. 27.

파일 다운로드시 리스폰스 헤더 설정을 도와주는 유틸입니다.

 

ResponseFileHeaderUtil.java

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class ResponseFileHeaderUtil {

    private HttpHeaders setContentType(HttpHeaders headers, String extention) {

        String e = extention.replaceAll("[^A-Za-z0-9]","");

        if (e.contains("gif")) {
            headers.setContentType(MediaType.IMAGE_GIF);
        } else if (e.contains("jpg") || e.contains("jpeg")) {
            headers.setContentType(MediaType.IMAGE_JPEG);
        } else if(e.contains("png")) {
            headers.setContentType(MediaType.IMAGE_PNG);
        } else {
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        }

        return headers;
    }


    private HttpHeaders setFilename(HttpHeaders headers, String userAgent, String filename, String extention) throws UnsupportedEncodingException {
        String newFilename;
        String e = extention.replaceAll("[^A-Za-z0-9]","");

        if (userAgent.contains("Edge")){
            newFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
            headers.add(HttpHeaders.CONTENT_DISPOSITION,  "attachment;filename=\"" + newFilename + "\"" + e + ";" );
        } else if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            newFilename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
            headers.add(HttpHeaders.CONTENT_DISPOSITION,  "attachment;filename=" + newFilename + "." + e + ";");
        } else if (userAgent.contains("Chrome")) {
            newFilename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            headers.add(HttpHeaders.CONTENT_DISPOSITION,  "attachment; filename=\"" + newFilename + "\"" + "." + e);
        } else if (userAgent.contains("Opera")) {
            newFilename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            headers.add(HttpHeaders.CONTENT_DISPOSITION,  "attachment; filename=\"" + newFilename + "\"" + "." + e);
        } else if (userAgent.contains("Firefox")) {
            newFilename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
            headers.add(HttpHeaders.CONTENT_DISPOSITION,  "attachment; filename=" + newFilename + "." + e);
        } else if (userAgent.contains("PostmanRuntime")) {
            String encodedFilename = URLEncoder.encode(filename, "UTF-8");
            headers.setContentDispositionFormData("attachment", encodedFilename + "." + e);
        }

        return headers;

    }

    public HttpHeaders set(HttpHeaders headers, String userAgent, String filename, String extention) throws UnsupportedEncodingException {
        headers = setContentType(headers, extention);
        headers = setFilename(headers, userAgent, filename, extention);
        headers.add("Content-Transfer-Encoding", "binary;");
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return headers;
    }

}

 

ResponseData.java

import java.io.*;
import org.springframework.http.HttpHeaders;
import org.springframework.core.io.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;

public class ResponseData {

    public ResponseEntity<Resource> file(HttpServletRequest request, File file, String filename, String extention) throws IOException {
        HttpHeaders headers = new HttpHeaders();
        ResponseFileHeaderUtil responseFileHeaderUtil = new ResponseFileHeaderUtil();

        String userAgent = request.getHeader("User-Agent");
        headers = responseFileHeaderUtil.set(headers, userAgent, filename, extention);

        Path path = Paths.get(file.getAbsolutePath());
        ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);
    }
}
728x90

댓글