问题描述:当浏览器端使用Active控件如(AIP、iwebPDF)请求打开后台PDF文件时,Java后台处理请求的方法处理如下:
response.setCharacterEncoding("UTF-8"); String filePath = request.getParameter("fileName"); File file = new File(filePath); try { if (!file.exists()) { response.sendError(404, "文件没有找到!"); return null; } FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] b = new byte[1024 * 8]; while ((in.read(b)) != -1) { out.write(b); } out.flush(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } return null; |
前台传了文件在服务器的全路径,存数据库也是存全路径。
红色字体行,从内存申请8K字节内存作为缓存,把文件流转成文件流响应输出到客户端,问题很容易出现PDF缓冲到客户端不完整,表现为显示有问题,另存为处理后的文件异常,打开关闭后提示“是否保存修改…”
解决办法是把8K改成1K,即1024问题就没在出现。
原因分析:还没找到!!!
其他解决方案:
int len = 0; // 字节长度 while((len = in.read(b))>0){ // 将读取到的长度记录下来 out.write(b,0,len); // 读取了多长的字节就写入多长的字节 }
|