-
web配置:
web.xml的所有servlet和filter添加配置项: <async-supported>true</async-supported>
-
java服务端响应:
HttpServlet实现: private ExecutorService executorPool=Executors.newFixedThreadPool(20); protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) { AsyncContext ctx=request.startAsync();//开启异步处理 ctx.setTimeout(5000); executorPool.submit(new AsyncRequestDownloadService(faceCloudService,downloadBean,ctx)); // 使用线程池控制调用 } ---------------------------------------》 public class AsyncRequestDownloadService implements Runnable{ private AsyncContext ctx; private DownloadBean downloadBean; private BusinessService businessService; public AsyncRequestDownloadService(BusinessService businessService,DownloadBean downloadBean,AsyncContext ctx ){ this.businessService=businessService; this.ctx = ctx; this.downloadBean=downloadBean; } @Override public void run() { downloadFile(downloadBean,(HttpServletResponse)ctx.getResponse()); ctx.complete(); } private void downloadFile(DownloadBean downloadBean,HttpServletResponse response) { switch (downloadBean.getBusinessType()) { // TODO } }
servlet文件异步下载
最新推荐文章于 2024-02-02 09:01:59 发布