如何使用nio下载文件?

时间:2019-05-17 02:27:42

标签: java nio file-transfer

我尝试使用nio通过API下载照片文件,但不确定如何创建响应渠道,此代码正确吗?

@RequestMapping(value = "/photosFast/{filename}.jpg", method = RequestMethod.GET)
@ResponseBody
...
FileChannel srcChannel = new FileInputStream(src.getAbsolutePath()).getChannel();
FileChannel destChannel = new FileOutputStream(response.getHeader("localtion")).getChannel();
try {
    srcChannel.transferTo(0, srcChannel.size(), destChannel);
} finally {
    srcChannel.close();
    destChannel.close();
}
...

0 个答案:

没有答案