读取文件为String 、访问链接直接跳转html
环境:SpringMVC 、前端jsp
InputStreamReader
FileInputStream fileInputStream = new FileInputStream(formatFile.getHtmlpath());
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
char[] b = new char[1024];
int cnt = 0;
StringBuilder sb = new StringBuilder();
while((cnt = reader.read(b,0,b.length)) != -1){
sb.append(b.toString());
}
结果:能读取出来,不乱码,但是字符中间多了逗号
BufferedReader
@RequestMapping("/xxx/yyy")
public ResponseEntity<String> markReportHtml(@RequestParam(value = "id") Integer id) throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type","text/html;charset=UTF-8");
F f = fDao.selectById(id);
if(f == null || StringUtils.isEmpty(f.getHtmlpath())){
return new ResponseEntity<>("<html><div>未查询到数据</div></html>",httpHeaders, HttpStatus.CONFLICT);
}
StringBuilder sb = new StringBuilder();
BufferedReader br=new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(formatFile.getHtmlpath())), StandardCharsets.UTF_8));
String line;
while ((line = br.readLine())!=null) {
sb.append(line);
}
return new ResponseEntity<>(sb.toString(), httpHeaders, HttpStatus.OK);
}
结果:访问接口,前端无需任何操作直接跳转html页面,符合要求