gpt4 book ai didi

java - 直接从 Spring Controller 返回 byte[] 是否会造成内存泄漏?

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:50 28 4
gpt4 key购买 nike

我在 Spring Controller 中有这个方法,我想确保不会造成内存泄漏。是还是不是?

@ResponseBody
@RequestMapping(value = "/document/{id}/{filename:.*}", method = RequestMethod.GET)
public byte[] getDownloadEclaimsDocument(@PathVariable("id") final String id, @PathVariable("filename") final String fileName, final HttpServletResponse response) {
final DmsFile dmsFile = dmsService.getByFileSystemId(id);
response.setContentType(dmsFile.getContentType());
// This call gets a byte[] from a backend service
return dmsService.getFileAsByteArray(dmsFile);
}

最佳答案

也许更好地使用org.springframework.core.io.Resource

@ResponseBody
@RequestMapping(value = "/document/{id}/{filename:.*}", method = RequestMethod.GET)
public Resource getDownloadEclaimsDocument(@PathVariable("id") final String id,
@PathVariable("filename") final String fileName,
final HttpServletResponse response,) {
final DmsFile dmsFile = dmsService.getByFileSystemId(id);
byte[] data = dmsService.getFileAsByteArray(dmsFile);

response.setContentType(dmsFile.getContentType());
response.setContentLength(data.length);
response.setHeader("Content-Disposition",
"attachment; filename=\"" +
MimeUtility.encodeWord(dsmFile.getName(), "utf-8", "Q") + "\"");

// This call gets a byte[] from a backend service
return new ByteArrayResource(data);
}

关于java - 直接从 Spring Controller 返回 byte[] 是否会造成内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36763376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com