客戶端請求下載,服務端響應下載請求並返回文件流

工作中遇到這樣的需求:

 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服務下載文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章