String filePath = fn + path; // 本该根据ID去数据库查,这里是hard code
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return buildFailResponse("文件不存在");
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
文件预览接口
最新推荐文章于 2024-08-12 15:45:48 发布