728x90
반응형
스프링 - zip 파일 다운로드
🟣 DataBase Data 에 저장 되어 있는 zip 파일의 주소 ( 리눅스 서버 경로 ) 를 가지고 와서 다운로드 하기.
package com.rsn.LucyAI.api.lucyai.util.service;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 정수빈
* 2021 06
*
* ZIP 파일 다운로드
*/
public class DownloadView {
/*
* filePath 에 ZIP 파일 경로를 보내주면
* 해당 ZIP 파일을 사용자의 로컬에 ZIP 파일이 저장 됩니다.
*/
public void filDown( HttpServletRequest request, HttpServletResponse response, String filePath ) throws IOException {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"" + "test.zip" + "\";");
FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis);
ServletOutputStream so = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(so);
byte[] data = new byte[2048];
int input = 0;
while ((input = bis.read(data)) != -1) {
bos.write(data, 0, input);
bos.flush();
}
if (bos != null)
bos.close();
if (bis != null)
bis.close();
if (so != null)
so.close();
if (fis != null)
fis.close();
}
}
✨💡 글이 도움이 되었다면 클릭 💡✨
728x90
반응형
'개발중 > Spring' 카테고리의 다른 글
😎 CSV 읽고 쓰고 파일 만들고 😎 (0) | 2021.07.10 |
---|---|
[스프링] 액셀 문서 업로드 - 이름 변경 (0) | 2021.07.06 |
JPA 공부하기 (0) | 2021.06.23 |
스프링 서버에 액셀 파일 업로드 (0) | 2021.06.11 |
@ApiParam 과 @RequestParam 의 차이점 (0) | 2021.05.27 |