有个需求是展示html但不让用户看到文件位置
思路如下
- io流读取文件
- 写入response
- 前台用jquery的load方法交html加载到div中
后台
private void returnByStream(File html, HttpServletResponse response) throws IOException{
FileInputStream inputStream = new FileInputStream(html);
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
// .设置文件头
response.setHeader("Content-Disposition", "attachment;fileName=" + new String(html.getName().getBytes("gb2312"),"ISO8859-1"));
int b = 0;
byte[] buffer = new byte[1024];
while ((b = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, b);
}
inputStream.close();
}
前台
$("selector").load("serviceUrl",data)