需求:实现服务器上excel文件转html,然后在浏览器上以流的方式读出(不让用户看到文件存放位置):
Java以字节流读取html文件,然后想在前台显示:读取html代码如下:
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
// 2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + file_name);
ServletOutputStream out;
// 通过文件路径获得File对象
html_file_path = html_file_path.replace("\\", "/");
File html_file = new File(html_file_path);
FileInputStream inputStream = new FileInputStream(html_file);
// 3.通过response获取ServletOutputStream对象(out)
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
while ((b = inputStream.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
}
inputStream.close();
out.flush();
out.close();
输出的内容想在浏览器上显示,但是浏览器一直提示下载:
<body>
<div id="divclass">
<div id="pdfLeftlist">
<a href="#" οnclick="updateUrl('a');">1001.pdf</a>
</div>
<div id="pdfRightlist">
<object width="100%" height="100%" data="${path}pdfPreview/viewer.jsp">
<!-- 传递参数:如list -->
<%-- <param name="file_url" id="file_url" value="${path}pdfPreview/04.pdf"> --%>
<%-- <param name="file_url" id="file_url" value="${path}readFileToPdf/pdfPreview.do"> --%>
</object>
</div>
</div>
</body>
前台代码:不管使用object标签还是iframe标签都不行:
$("#pdfRightlist").html("<object width='100%' height='100%' data='${path}readFileToHtml/htmlPreview.do?id="+id+"&fileType="+type+"'><param name='file_urla' id='file_urla' value=''/></object>");
报:ClientAbortException: java.io.IOException 异常
使用:
$("#pdfRightlist").html("<iframe src='${path}readFileToHtml/htmlPreview.do?id="+id+"&fileType="+type+"'"+
" frameborder='0' border='0' marginwidth='0' marginheight='0' scrolling='auto' allowtransparency='yes' width='100%' height='800'></iframe>");
浏览器提示下载:
提示下载文件;最后解决办法:使用$("#pdfRightlist").load("${path}readFileToHtml/htmlPreview.do",{"id":id,"fileType":type});
load方法:在video.js中