目录

后端传递文件流到前端

后端传递文件流到前端

传网络文件

public void getMobileNumber(@RequestBody String fileName, HttpServletResponse response) throws UnsupportedEncodingException {response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".pdf");
        try (ServletOutputStream out=response.getOutputStream()){
            URL url=new URL("http://192.168.0.49:8080/.../.../"+fileName+".pdf");
            InputStream stream = url.openStream();  //打开网络文件
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) >0){
                out.write(buff,0,length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e){
            e.printStackTrace();
        }
}

传本地文件

public void getMobileNumber(@RequestBody String fileName, HttpServletResponse response) throws UnsupportedEncodingException {response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment;filename="+fileName+".pdf");
        try (ServletOutputStream out=response.getOutputStream()){
            InputStream stream = new FileInputStream("D:\.\\test.pdf");
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = stream.read(buff)) >0){
                out.write(buff,0,length);
            }
            stream.close();
            out.close();
            out.flush();
        } catch (IOException e){
            e.printStackTrace();
        }
}