|
@ -3,22 +3,31 @@ package com.yihu.jw.wlyy.wlyyhttp;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
|
|
|
import com.yihu.jw.entity.third.wlyyinfo.OauthWlyyConfigDO;
|
|
|
import com.yihu.jw.exception.business.file_upload.FileWrongFormatException;
|
|
|
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
import com.yihu.jw.wlyy.dao.OauthWlyyConfigDao;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.NameValuePair;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.Charset;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Created by Trick on 2019/8/19.
|
|
@ -96,6 +105,72 @@ public class WlyyHttpService {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
//上传图片
|
|
|
public String request(MultipartFile file, String type,String configId) {
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
String result = "";
|
|
|
try {
|
|
|
OauthWlyyConfigDO oauthWlyyConfigDO = oauthWlyyConfigDao.findById(configId).orElse(null);
|
|
|
//token获取accesstoken
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
params.add(new BasicNameValuePair("appid", oauthWlyyConfigDO.getAppId()));
|
|
|
params.add(new BasicNameValuePair("appSecret", oauthWlyyConfigDO.getAppSecret()));
|
|
|
String res = httpClientUtil.post(oauthWlyyConfigDO.getTokenUrl(), params, "UTF-8");
|
|
|
String token = null;
|
|
|
JSONObject rsjson = JSONObject.parseObject(res);
|
|
|
logger.info("sendWlyyMes token :" + rsjson.toString());
|
|
|
Integer status = rsjson.getInteger("status");
|
|
|
if (status == 10000) {
|
|
|
token = rsjson.getJSONObject("result").getString("accesstoken");
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
HttpPost httpPost = new HttpPost(oauthWlyyConfigDO.getUrl());
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
httpPost.setHeader("accesstoken",token);
|
|
|
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
|
|
|
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
|
|
|
if (!org.springframework.util.StringUtils.isEmpty(type)) {
|
|
|
builder.addTextBody("type", type); //发送类型
|
|
|
}
|
|
|
logger.info("type===="+type);
|
|
|
if (!isFileFlag(type)){
|
|
|
throw new FileWrongFormatException("不符合文件上传格式");
|
|
|
}
|
|
|
HttpEntity entity = builder.build();
|
|
|
httpPost.setEntity(entity);
|
|
|
HttpResponse response = httpClient.execute(httpPost);// 执行提交
|
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
if (responseEntity != null) {
|
|
|
// 将响应内容转换为字符串
|
|
|
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
try {
|
|
|
httpClient.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
public boolean isFileFlag(String type){
|
|
|
type = type.toLowerCase();
|
|
|
if (type.contains(".")){
|
|
|
type = type.substring(type.lastIndexOf("."),type.length()-1);
|
|
|
}
|
|
|
logger.info(type);
|
|
|
List img = new ArrayList(Arrays.asList("jpeg","bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp","xls","xlsx","text/plain","mp3","mp4","m4v","avi","ogm","wmv","mpg","webm","ogv","mov","asx","mpeg","image/png","amr"));
|
|
|
if (!img.contains(type)) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
/**
|
|
|
* @param configId 配置ID
|
|
|
* @param param key为param,的参数
|
|
@ -115,8 +190,6 @@ public class WlyyHttpService {
|
|
|
Integer status = rsjson.getInteger("status");
|
|
|
|
|
|
if (status == 10000) {
|
|
|
|
|
|
|
|
|
//设置头部
|
|
|
token = rsjson.getJSONObject("result").getString("accesstoken");
|
|
|
Map<String,Object> headerMap = new HashedMap();
|