由于之前自己做过jquery分页 ,就是调用 jni接口时,只能用前台 分页 解决 显示 问题。最近看到有人提这样的问题:一个 请求传过来上万个数据怎么办?于是萌生了写这篇博客 的想法。
因为核心代码 主要在前端jquery,为了简便,后台 就用servlet遍历本地磁盘目录文件 的形式模拟响应的数据。
本项目的目录结构:
本项目的本地遍历文件 夹结构:
处理显示 请求的servlet:
rush:java;">
package com.cn.action;
import com.alibaba.fastjson.JSON;
import com.cn.entity.Downloadfile;
import javax.servlet.
servletexception ;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Created by Nolimitors on 2017/3/17.
*/
public class PagesServlet extends HttpServlet{
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws
servletexception ,IOException {
/**
*@Author: Nolimitor
*@P
ara ms: * @p
ara m req
* @p
ara m resp
*@Date: 17:55 2017/3/17
*/
doPost(req,resp);
}
protected void doPost(HttpServletRequest req,IOException {
/*
@Author: Nolimitor
@Para ms: @para m req
@para m resp
@Date: 17:55 2017/3/17
*/
Properties props = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(this.getClass().getResource("/fileroot.properties").getPath()));
props.load(in);
String rootPath = props.getProperty("Root");
List fileList = new ArrayList();
File file = new File(rootPath);
File []files = file.listFiles();
Downloadfile df = new Downloadfile();
for(File f:files) {
df.setName(f.getName());
df.setFilesize(Long.toString(f.length()));
System .out.println(f.getName());
fileList.add(JSON.toJSONString(df));
}
resp.addHeader("Content-type","application/json");
resp.setHeader("content-type","text/html;charset=UTF-8");
resp.getWriter().print(JSON.toJSONString(fileList));
}
}
PagesServlet
处理下载文件 请求的servlet:
rush:java;">
package com.cn.action;
import javax.servlet.
servletexception ;
import javax.servlet.Servl
eto utputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Properties;
/**
* Created by Nolimitors on 2017/3/20.
*/
public class DownloadFile extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req,IOException {
doGet(req,resp);
}
@Override
protected void doGet(HttpServletRequest req,IOException {
//
获取 所要下载
文件 的路径
Properties props = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(this.getClass().getResource("/fileroot.properties").getPath()));
props.load(in);
String rootPath = props.getProperty("Root");
String name = req.getP
ara meter("filename");
name = new String(name.getBytes("ISO8859-1"),"UTF-8");
Sy
stem .out.println(name);
//处理请求
//读取要下载的
文件
File f = new File(rootPath+"\\"+ name);
if(f.exists()){
FileInputStream fis = new FileInputStream(f);
String filename=java.net.URLEncoder.encode(f.getName(),"utf-8"); //
解决 中文 文件 名下载乱码的问题
byte[] b = new byte[fis.available()];
fis.read(b);
//
解决 中文 文件 名下载后乱码的问题
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-
dis position","attachment;filename="+
new String(filename.getBytes("utf-8"),"ISO-8859-1"));
//
获取 响应报文
输出 流对象
Servl
eto utputStream out =resp.g
eto utputStream();
//
输出
out.write(b);
out.flush();
out.close();
}
}
}
DownloadFile
web.xml配置:
rush:xml;">
PageServlet
com.cn.action.PagesServlet
PageServlet
/doPages
DownServlet
com.cn.action.DownloadFile
DownServlet
/download
web.xml
rush:xhtml;">
<
Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
nofollow" rel="stylesheet">
nofollow" rel="stylesheet">
Num
Files
Size
Operation