我在那裏我在成批讀了大量文件,並將其轉換成JSON將其發送到一個Spring MVC控制器代碼,我的方法看起來是這樣的,我通過我想要的開始的偏移和CHUNKSIZE在noBytes,結果被傳遞到轉化成JSON和發送到我的控制器打開組塊到網頁中的Java
public String readByteBlock(File file, int offset, int noBytes) throws IOException {
InputStream in = new FileInputStream(file);
byte[] result = new byte[noBytes];
in.skip(offset);
in.read(result, 0, noBytes);
return FileSaver.byteArrayToString(result, noBytes);
}
我有問題,使下一個和以前的拼版作業在我的控制器
@RequestMapping("/page/{id}")
@ResponseBody
File file = new File("C:/teste.txt");
FileSaver fs = new FileSaver();
int byteblock = 100;
int offset = 0;
if(id.equals("next")){
offset = (int) req.getSession().getAttribute("prevEndPos");
String s = fs.readByteBlock(file, offset, byteblock);
req.getSession().setAttribute("prevStartPos", offset);
req.getSession().setAttribute("prevEndPos", (offset + byteblock));
return s;
}
if(id.equals("prev")){
offset = (int) req.getSession().getAttribute("prevStartPos") - byteblock ;
if(offset < 0){
String s = fs.readByteBlock(file, 0, byteblock);
return s;
}
String s = fs.readByteBlock(file, offset, byteblock);
req.getSession().setAttribute("prevStartPos", (offset - byteblock));
return s;
}
String s = fs.readByteBlock(file, 0, byteblock);
req.getSession().setAttribute("prevStartPos", offset);
req.getSession().setAttribute("prevEndPos", (offset + byteblock));
return s;
所以文件的每個塊將投入在米HTML表y應用程序中,我將當前塊startByte和endByte保存到我的會話中,但是當用戶按下我的頁面上的上一個按鈕時,我怎麼知道上一個塊的開始和結束位置?自會話將保持當前頁面,而不是以前的一個