调用接收端

    @ApiOperation(value = "文件请求展示方法")
    @RequestMapping(value = "/showFile", method = RequestMethod.GET)
    public void showFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取接口url
        String url = "http://127.0.0.1/a/b";
        //然后根据表名获取公司信息
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("comName", "111111"));
        HttpResponse httpResponse = null;
        HttpEntity httpEntity = null;
        try {
            httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            RequestConfig requestConfig = RequestConfig.custom()  
                    .setConnectTimeout(30000).setConnectionRequestTimeout(30000)  
                    .setSocketTimeout(30000).build(); 
            httppost.setConfig(requestConfig);
            HttpClient httpclient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler()).build(); 
            httpResponse = httpclient.execute(httppost);
        }catch (Exception e1) {
            logger.error("----------------失败");
        }
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 请求正常
            try {
                httpEntity = httpResponse.getEntity();
                BufferedInputStream br = new BufferedInputStream(httpEntity.getContent());
                byte[] buf = new byte[1024];
                int len = 0;
                response.reset(); // 非常重要
                response.setContentType("application/pdf");
                String fileName ="report.pdf";
                try {
                    fileName = httpResponse.getAllHeaders()[5].getValue().split(";")[1].split("=")[1];
                } catch (Exception e) {
                }
                        
                response.setHeader("Content-Disposition",
                        "inline; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));

                OutputStream out = response.getOutputStream();
                while ((len = br.read(buf)) != -1)
                    out.write(buf, 0, len);
                br.close();
                out.flush();

            } catch (Exception e) {
                logger.error("解析失败");
            }
        }else {
            logger.error("调用失败");
        }
        
    }

文件存储方

   @ApiOperation(value = "文件支持接口")
    @RequestMapping(value = "/getReport", method = RequestMethod.GET)
    public Object getReport(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        String comName = (String) params.get("comName");
        File file = new File("C:\\Users\\Administrator\\Desktop\\滴滴电子发票.pdf");
        
        try {
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition",
                    "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        try {
            ServletOutputStream pw = response.getOutputStream();
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
            byte[] by = new byte[(int) file.length()];
            while(br.read(by)!=-1){
                pw.write(by);
            }
            pw.flush();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        
        return null;
    }

 

posted on 2019-09-17 21:47  程序员丁先生  阅读(6387)  评论(0编辑  收藏  举报