BarryWearBurberry 2022-04-09 14:45 采纳率: 81.8%
浏览 44
已结题

使用HttpClientUtils post请求返回后拼接地址下载下来的excel打开显示部分内容有问题

使用HttpClientUtils post请求返回responseStr,将responseStr中的 resultUrl和ip地址拼接,实现api调用下载excel,使用postman配置参数使用拼接后的地址下载下来的excel正常,但是程序下载下来的excel打开显示部分内容有问题,其中请求的地址,body参数都一致

@Override
    public R getViewUrl2(String product, String config, String process) throws IOException {
        // TODO Auto-generated method stub
        if(StringUtils.isBlank(goerConfig.getGrapecityUrl2()) 
                || StringUtils.isBlank(goerConfig.getOarToken()) 
          ) {
            return R.error("缺少相关配置项,请联系管理员!");
            }
        
        String reportId = goerConfig.getTFSputterReportId();
        String grapeCityUrl2 = goerConfig.getGrapecityUrl2(); 
        String token = goerConfig.getOarToken();
        
        String[] productArray = new String[1];
        String[] configArray = new String[1];
        String[] processArray = new String[1];
        
        productArray[0] = product;
        configArray[0] = config;
        processArray[0] = process;        
        
        JSONObject param = new JSONObject();
        param.put("product", productArray);
        param.put("config", configArray);
        param.put("process", processArray);
        
        JSONObject param2 = new JSONObject();                
        param2.put("FileFormat", "Xlsx");
        param2.put("Orientation", "Default");
        param2.put("OutputFormat", "Transitional");
        param2.put("PaperSize", "Default");
        param2.put("UseCompression", false);
        param2.put("EnableToggles", false);
        param2.put("UseDefaultPalette", false);
        param2.put("MultiSheet", false);
        param2.put("SheetName","Sheet");
        param2.put("ProtectedBy", "");        
        param2.put("WritePassword", "");
        param2.put("Password", "");
        param2.put("ReadOnlyRecommended", false);
        param2.put("AddTimestamp", false);    
        
        JSONObject param3 = new JSONObject();
        param3.put("parameters", param);
        param3.put("settings", param2);
                
        String address = grapeCityUrl2;
        String url = address + "api/reporting/exportTemplate/"+reportId+"/e5ec3255-8d8d-4ef9-9656-6e44149ae673"
                + "?token="+ token;                
        CloseableHttpClient httpClient = null;
        // 设置请求和传输超时时间
        String connTimeout = goerConfig.getConnTimeout();
        String socketTimeout = goerConfig.getSocketTimeout();
        int connTimeoutNum = Integer.valueOf(StringUtils.trim(connTimeout));
        int socketTimeoutNum = Integer.valueOf(StringUtils.trim(socketTimeout));
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeoutNum)
                .setConnectTimeout(connTimeoutNum).build();
        try {
            httpClient = HttpClients.createDefault();
            String responseStr = HttpClientUtils.getResponseFromPostMethod(httpClient, url, requestConfig, param3.toString(),
                    "utf-8");
            LOGGER.info(param3.toString());
            LOGGER.info(responseStr);    
            if (StringUtils.isNotEmpty(responseStr)) {
                JSONObject respObj = JSONObject.parseObject(responseStr);
                if(respObj != null) {
                    String resultUrl = respObj.getString("resultUrl");
                    if(StringUtils.isNotBlank(resultUrl)) {                        
                        resultUrl = address + resultUrl;
                        LOGGER.info(resultUrl);
                    }
                   return R.ok().put("url", resultUrl);
                }                                                        
            }
        } catch (ClientProtocolException e) {
            LOGGER.error(e.getMessage(), e);
        } catch (HttpHostConnectException e) {
            // 捕获超时异常 并反馈给调用者
            LOGGER.error(e.getMessage(), e);
        } catch (ConnectTimeoutException e) {
            // 捕获超时异常 并反馈给调用者
            LOGGER.error(e.getMessage(), e);
        } catch (IOException e) {
            // e.printStackTrace();
            LOGGER.error(e.getMessage(), e);
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        // 返回下载地址失败
        return R.error().put("msg", "获取下载地址失败,请联系管理员!");
    }    
}

js

function view2(product,config,process) {
    var param = {'product':product,'config':config,'process':process};
    console.log(JSON.stringify(param));
    console.log(param);
    $.ajax({
        url : prefix+"/getViewUrl2",
        type : "post",
        data : {
            'process' : process,
            'config' : config,
            'product' : product
        },
        success : function(r) {
            if (r.code==0) {              
                location.href = r.url;
            }else{
                layer.msg(r.msg);
            }
        }
    });
}

运行时的输出

{"settings":{"WritePassword":"","ReadOnlyRecommended":false,"UseCompression":false,"OutputFormat":"Transitional","AddTimestamp":false,"SheetName":"Sheet","FileFormat":"Xlsx","Orientation":"Default","EnableToggles":false,"ProtectedBy":"","PaperSize":"Default","MultiSheet":false,"UseDefaultPalette":false,"Password":""},"parameters":{"product":["TF"],"process":["Sputter"],"config":["M001528"]}}
13:52:14 [http-nio-8082-exec-5] INFO  c.g.o.s.impl.MaintableServiceImpl - {"resultUrl":"api/workerJob/1d40100b-f135-41f3-81f7-3017205c97c0","resultId":"1d40100b-f135-41f3-81f7-3017205c97c0","verificationUrl":"api/workerJob/1d40100b-f135-41f3-81f7-3017205c97c0/verify","renderingSkipped":false}
13:52:14 [http-nio-8082-exec-5] INFO  c.g.o.s.impl.MaintableServiceImpl - http://10.10.112.100:51980/api/workerJob/1d40100b-f135-41f3-81f7-3017205c97c0
13:52:14 [http-nio-8082-exec-5] INFO  c.g.common.aspect.WebLogAspect - 耗时 : 12671
13:52:14 [http-nio-8082-exec-5] DEBUG c.g.common.aspect.WebLogAspect - 返回值 : {msg=操作成功, code=0, url=http://10.10.112.100:51980/api/workerJob/1d40100b-f135-41f3-81f7-3017205c97c0}

打开显示部分内容错误

img


postman中的设置

img


postman post请求返回后拼接在浏览器下载的excel正常

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-Time 2022-04-09 14:51
    关注

    会不会是因为传的参数不对。导致下载出错?
    用node写api试一试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月17日
  • 已采纳回答 4月9日
  • 创建了问题 4月9日

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题