java web 多线程下载文件的问题。

**当我开启多线程下载文件时,outputstream.write 报 空指针 错误。 大佬们帮我看看
**

 ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(
                new Runnable() {
                    @Override
                    public void run() {
                        String path=System.getProperty("catalina.home")+"/webapps/uploadFiles/njhb/areaFiles/南京市/"+fileName;
                        final File file  = new File(path);
                        if(!file.exists()){
                            System.out.println("文件不存在");
                            //response.setHeader("content-type", "text/html;charset=UTF-8");
                            response.setContentType("text/html;charset=UTF-8");
                            PrintWriter writer = null;
                            try {
                                writer = response.getWriter();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            writer.write("文件不存在!");
                            return;
                        }
                        response.reset();
                        response.setCharacterEncoding("UTF-8");
                        response.setContentType("multipart/form-data");
                        response.setHeader("Content-Disposition",
                                "attachment;fileName="+ encodingFileName(fileName));
                        response.setHeader("Set-Cookie", "fileDownload=true; path=/;" +
                                "attachment;fileName=" + encodingFileName(fileName));

                        try {
                            //打开本地文件流
                            InputStream inputStream = new FileInputStream(file);
                            //激活下载操作
                            OutputStream outputStream = response.getOutputStream();

                            //循环写入输出流  每次读取2048  一直读完
                            byte[] b = new byte[2048];
                            int length;
                            int count=0;
                            while ((length = inputStream.read(b)) > 0) {
                                System.out.println("b: "+b+"     length: "+length);
                                outputStream.write(b, 0, length);
                                count++;
                            }

                            // 这里主要关闭。
                            outputStream.close();
                            inputStream.close();





                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                            logg.info("文件下载终止!!!");
                        }
                    }
                }
        );

下面是报错信息!!

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问