客户端请求下载,服务端响应下载请求并返回文件流

工作中遇到这样的需求:

 1、A项目提供下载服务

 2、B项目封装A项目的下载服务

 3、通过B项目来下载文件。

遇到的场景是:只有A服务可以下载文件,这里ip 限制,其他的服务器无法直接下载文件,这里类似下载请求,B服务无法直接下载文件,只能先请求A服务,通过A服务实现下载。(这里不讲sendredirect)。

A服务,这里对应下载的服务器端,代码如下:

//服务器端
    @RequestMapping(value = "/realdwonloadFile", method = RequestMethod.POST)
    public void processDownload(HttpServletRequest request, HttpServletResponse response){
        int BUFFER_SIZE = 100000;
        InputStream in = null;
        OutputStream out = null;
        System.out.println("Come on, baby .......");

        try{
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");

            String fileName = request.getHeader("fileName");

            System.out.println("fileName:" + fileName);

            File file = new File(fileName);
            response.setContentLength((int) file.length());
            response.setHeader("Accept-Ranges", "bytes");

            int readLength = 0;

            in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
            out = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[BUFFER_SIZE];
            while ((readLength=in.read(buffer)) > 0) {
                byte[] bytes = new byte[readLength];
                System.arraycopy(buffer, 0, bytes, 0, readLength);
                out.write(bytes);
            }

            out.flush();

            response.addHeader("token", "hello 1");

        }catch(Exception e){
            e.printStackTrace();
            response.addHeader("token", "hello 2");
        }finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

B服务,这里是客户端。代码如下:

/**
     *
     * @param request
     * @param response
     * @param urlPath 调用访问下载文件的url(方法)
     * @param remoteFilePath  需要下载的文件所在路径
     */

    public void clientDown(HttpServletRequest request, HttpServletResponse response,
                           String urlPath, String remoteFilePath){

        DefaultHttpClient httpClient = new DefaultHttpClient();
        OutputStream out = null;
        InputStream in = null;

//        urlPath = "http://localhost:9990/realdown/realdwonloadFile";
//        String remoteFilePath  = "D:\\usr\\local\\ciecc\\testdown\\";

        String fileName =request.getParameter("name");
        try {
            HttpPost httpPost = new HttpPost(urlPath);

            httpPost.addHeader("fileName", remoteFilePath+fileName);

            httpPost.setHeader("Content-Disposition", "attachment;filename="+remoteFilePath+fileName);

            //这里访问server 端(也就是urlPath路径),server下载文件并将文件流回传
            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity entity = httpResponse.getEntity();
            in = entity.getContent();

            long length = entity.getContentLength();
            if (length <= 0) {
                System.out.println("下载文件不存在!");
                return;
            }

            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);

            out = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[4096];
            int readLength = 0;
            while ((readLength=in.read(buffer)) > 0) {
                byte[] bytes = new byte[readLength];
                System.arraycopy(buffer, 0, bytes, 0, readLength);
                out.write(bytes);
            }

            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

这样就可以实现 通过B服务下载文件。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值