- 서버에있는 고정 위치에서 zip 파일을 다운로드하려고합니다.
- 내 Rest 메서드에서 클라이언트 (브라우저)의 파일 이름을 전달 중입니다. (아래 코드 참조).
내 Rest 방법에서 zip 파일을 클라이언트로 보내고 있습니다.Java에서 Chrome에서 .zip 확장자없이 Zip 파일을 다운로드 중입니다.
파일은 아무 문제없이 브라우저에 다운로드됩니다.
제 문제는 zip 파일이 .zip 확장자없이 브라우저에 다운로드된다는 것입니다.
@RequestMapping(value = "/zip/{filePath}", method = RequestMethod.GET)
public @ResponseBody void downloadZip(@PathVariable("filePath") String filePath, HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletContext context = request.getServletContext();
File downloadFile = new File(filePath);
FileInputStream inputStream = new FileInputStream(downloadFile);
// get output stream of the response
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[(int) downloadFile.length()];
int bytesRead = -1;
// write bytes read from the input stream into the output stream
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
// get MIME type of the file
String mimeType = context.getMimeType(fullPath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// set content attributes for the response
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
response.setHeader("Content-Disposition",
String.format("attachment; filename=\"%s\"", downloadFile.getName()));
logger.error("Filename = " + downloadFile.getName());
inputStream.close();
outStream.close();
}
PS : 파일은 ZIP이있는 일부 컴퓨터에서, ZIP없이 일부 컴퓨터에서 다운로드됩니다. 나는 크롬에 대해서만 테스트했다 (클라이언트 요구 사항에 따라). 내가 생각할 필요가있는 Chrome 설정에 문제가있는 것 같습니다 (추측).
다른 사람이 도움을 줄 수 있습니까? 사전에
감사합니다 ....
응답 헤더를 설정하고 파일을 출력 스트림 아래로 밀어 넣는 순서를 변경하십시오. 결국 머리글은 먼저 나가야합니다. –
시도했지만 작동하지 않습니다. 어쨌든 고마워. – krohit
마임 유형을 "application/zip, application/octet-stream"으로 시도하십시오. –