2019/12/16 学习笔记
Java读取文件并返回前端
首先读取文件工具类
package com.chinacreator.library.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import com.chinacreator.c2.config.ConfigManager;
public class FileUtil {
private static String getCharset(String fileName) throws IOException{
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
return charset; //文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; //文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE"; //文件编码为 Unicode big endian
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8"; //文件编码为 UTF-8
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return charset;
}
public static ArrayList<String> getContent(String url,int page,int rows) throws IOException{
ArrayList<String> strArray = new ArrayList<String>();
String[] file = url.split("/");
String u = file[file.length-2];
u= ConfigManager.getInstance().getConfig("c2.file.rootFolder")+"/"+u.substring(0, 3)+"/"+u;
File filename = new File(u);
String charset = getCharset(u);
InputStreamReader reader = new InputStreamReader(new FileInputStream(filename),charset);
BufferedReader br = new BufferedReader(reader);
String line = "";
line = br.readLine();
int i=0;
while(line != null&&i<=page*rows) {
i++;
if(i>=(page-1)*rows) strArray.add(line);
line = br.readLine();
}
return strArray;
}
}
第一个方法是根据文件返回文件的字符编码,第二个方法,是根据文件路径,页数,行数,返回对应的内容,用Arraylist存储。
控制层:
@GET
@Path("/read")
@ApiOperation(value = "分页显示电子书内容", notes = "返回电子书分页内容")
public Map<String, Object> getListByPage(@ApiParam(value = "电子书url", required = false) @QueryParam(value = "url") java.lang.String url,
@ApiParam(value = "页数", required = false) @QueryParam(value = "page") int page,
@ApiParam(value = "行数", required = false) @QueryParam(value = "rows") int rows) throws IOException{
ArrayList<String> strArray = new ArrayList<String>();
strArray = FileUtil.getContent(url, page, rows);
Map<String, Object> map= new HashMap<String, Object>();
map.put("content", strArray);
map.put("page", page);
map.put("rows", rows);
return map;
}
调用工具类的方法,把取回到的相应页数和行数的内容用map存储,然后返回。
利用js制作html表格
HTML片段 只要有一个table id就可以
<div>
<table class="hovertable" id="book">
</table>
</div>
js
for(let i=0;i<data.contents.length;i++){
table+="<tr>";
if(n==0){
table+="<td><a>"+$params.contents[i].bookName+"</a></td>";
table+="<td>"+$params.contents[i].printer+"</td>";
}else{
table+="<td>"+$params.contents[i].ebookName+"</td>";
table+="<td>热度:"+$params.contents[i].hot+"</td>"
}
table+="<td>"+$params.contents[i].writer+"</td>"
table+="<td>"+$params.contents[i].typeName+"</td>"
if(n==0)table+="<td>馆藏量:"+$params.contents[i].store+"</td>"
table+="</tr>";
$("#book").html(table);
}
这就是根据后台请求到的数据,循环遍历,然后放到对于html里面。