아래 코드를 사용하여 S3에서 파일을 다운로드하고 있습니다. 이 코드는 중형 및 대형 파일 크기에서 잘 작동하지만 다운로드가 실패하면 극히 작은 파일 크기 (3kb - TXT 파일에서 한 줄)가 발생합니다. 작은 파일작은 파일을 다운로드 할 수 없지만 큰 파일을 다운로드 할 수 있습니다.
// 컨트롤러
def download() {
Request request = Request.get(params.int("id"))
response.setContentType("application/octet-stream")
myService.downloadFileFromS3(request.origFileName, response)
}
void downloadFileFromS3(String fileName, HttpServletResponse response) {
String fullFileNameWithExtension = fileName
response.setHeader("Content-disposition", "attachment;filename=${fullFileNameWithExtension}")
InputStream is = getS3Client().getObject(getBucketName(), fullFileNameWithExtension).getObjectContent()
OutputStream outputStream = response.getOutputStream()
byte[] buffer = new byte[1024];
int length
log.info("This is the length: " + length)
while ((length = is.read(buffer)) != -1) {
log.info("Came here with length: " + length)
outputStream.write(buffer, 0, length)
}
is.close()
}
로그 아웃 풋 :
This is the length: 0
Came here with length: 15
GroovyPagesServlet: "/WEB-INF/grails-app/views/request/download.gsp" not found
로그 출력 큰 파일 :
This is the length: 0
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 1024
Came here with length: 531
은 그래서 작은 파일은 것 같다 download.gsp
보기. 그러나 나는 물건을 넣고 있기 때문에 그 견해를 찾지 않아야한다. response
방법이 실패하는 MyController에? 제기되는 예외는 무엇입니까? 어떤 오류가 있습니까? 자세한 내용을 제공해야합니다. –
@JoshuaMoore 자세한 내용을 업데이트했습니다. – Anthony
메소드에서 돌아 오기 전에'outputStream.flush()'를 시도하십시오. –