java写的文件下载接口,客户端无法获取到文件大小问题
直接上代码:
// 字符串转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源码。
原文作者:ty41232X32
原文地址: https://blog.csdn.net/u013727805/article/details/106052021
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/u013727805/article/details/106052021
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章