这就是代码。大多数示例都不允许我使用通过函数参数获得的response变量。有没有人能帮我解决这个问题?或者,在这种情况下,使用try-finally比使用try-with-resources更好?
public void func(HttpServletResponse response) throws IOException {
OutputStream out = null;
InputStream in = null;
try {
out = response.getOutputStream();
ResponseEntity<Resource> url = getUrl();
Resource body = url.getBody();
in = body.getInputStream();
FileCopyUtils.copy(in, out);
} finally {
if (out != null) {
try {
out.close();
}catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
}catch (IOException e) {
}
}
}
}发布于 2021-03-12 01:47:46
您可以使用try-with-resources构造来执行此操作
public void func(HttpServletResponse response) throws IOException {
try (OutputStream out = response.getOutputStream) {
out = response.getOutputStream();
ResponseEntity<Resource> url = getUrl();
Resource body = url.getBody();
try(InputStream in = body.getInputStream()) {
FileCopyUtils.copy(in, out);
}
}
}https://stackoverflow.com/questions/66587767
复制相似问题