使用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}
打开显示部分内容错误
postman中的设置
postman post请求返回后拼接在浏览器下载的excel正常