안녕하세요, 다음과 같이 작성된 컨트롤러 클래스가 있습니다. mongo db에서 파일을 가져 와서 다운로드하려고합니다.Spring mvc 및 mongodb에서 파일을 다운로드하는 방법
organizationFileAttachmentService.setUser(getUser());
GridFSDBFile file = organizationFileAttachmentService.getGridFSDBFileById(new ObjectId(id), "File");
if (file != null) {
byte[] content = organizationFileAttachmentService.findByIdAndBucket(new ObjectId(id), "File");
try {
int size = content.length;
InputStream is = null;
byte[] b = new byte[size];
try {
is = new ByteArrayInputStream(content);
is.read(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
} catch (Exception ex) {
}
}
response.setContentType(file.getContentType());
// String attachment =
// "attachment; filename=\""+file.getFilename()+"\"";
String attachment = "attachment; filename=" + file.getFilename();
// response.setContentLength(new
// Long(file.getLength()).intValue());
response.setCharacterEncoding(file.getMD5());
response.setHeader("content-Disposition", attachment);// "attachment;filename=test.xls"
// copy it to response's OutputStream
// FileCopyUtils.copy(is, response.getOutputStream());
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
} catch (IOException ex) {
_logger.info("Error writing file to output stream. Filename was '" + id + "'");
throw new RuntimeException("IOError writing file to output stream");
}
하지만 파일을 다운로드 할 수 없습니다. 어느 누구도 나를 도와 줄 수 없다. 당신이 그것을 놓친 경우
라인은'response.setCharacterEncoding (file.getMD5()); incorrecr'이다. CharacterEncoding은 "UTF-8"또는 이와 비슷한 형식이어야합니다. –