java写的文件下载接口,客户端无法获取到文件大小问题
程序员文章站
2022-05-30 21:27:17
...
直接上代码:
// 字符串转InputStream
byte[] manifestPlistBytes = manifestPlistStr.getBytes("UTF-8");
InputStream input = new ByteArrayInputStream(manifestPlistBytes);
String filename = System.currentTimeMillis() + "manifest.plist";
int len = 0;
byte[] bytes = new byte[1024];
ServletOutputStream servletOutputStream = resp.getOutputStream();
resp.setContentType("application/octet-stream");
resp.setHeader("Accept-Ranges", "bytes");
resp.setHeader("Content-Length", String.valueOf(manifestPlistBytes.length));
resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
resp.setCharacterEncoding("UTF-8");
while ((len = input.read(bytes)) > 0) {
servletOutputStream.write(bytes, 0, len);
}
servletOutputStream.close();
input.close();
关键代码:
resp.setHeader("Accept-Ranges", "bytes");
resp.setHeader("Content-Length", String.valueOf(manifestPlistBytes.length));
参考网上资料和jfinal源码。
下一篇: PHP中的浅复制与深复制的实例详解