YozoUtils.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package jnpf.utils;
  2. import jnpf.constant.MsgCode;
  3. import jnpf.yozo.client.AppAuthenticator;
  4. import jnpf.yozo.client.UaaAppAuthenticator;
  5. import jnpf.yozo.utils.DefaultResult;
  6. import jnpf.yozo.utils.IResult;
  7. import jnpf.model.YozoParams;
  8. import jnpf.util.XSSEscape;
  9. import jnpf.yozo.constants.EnumResultCode;
  10. import jnpf.yozo.constants.UaaConstant;
  11. import lombok.Cleanup;
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.client.methods.CloseableHttpResponse;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.ContentType;
  16. import org.apache.http.entity.mime.HttpMultipartMode;
  17. import org.apache.http.entity.mime.MultipartEntityBuilder;
  18. import org.apache.http.entity.mime.content.FileBody;
  19. import org.apache.http.impl.client.CloseableHttpClient;
  20. import org.apache.http.impl.client.HttpClients;
  21. import org.apache.http.util.EntityUtils;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.util.StringUtils;
  24. import java.io.*;
  25. import java.net.HttpURLConnection;
  26. import java.net.URL;
  27. import java.nio.charset.Charset;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. /**
  31. * 文件预览编辑工具类
  32. *
  33. * @author JNPF开发平台组
  34. * @version V3.1.0
  35. * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com)
  36. * @date 2021/5/13
  37. */
  38. @Component
  39. public class YozoUtils {
  40. /**
  41. * 生成签名
  42. *
  43. * @param appId
  44. * @param secret
  45. * @param params
  46. * @return
  47. */
  48. public IResult<String> generateSign(String appId, String secret, Map<String, String[]> params) {
  49. AppAuthenticator authenticator = new UaaAppAuthenticator(UaaConstant.SIGN, null, UaaConstant.APPID);
  50. try {
  51. String[] appIds = params.get(UaaConstant.APPID);
  52. if (appIds == null || appIds.length != 1 || StringUtils.isEmpty(appIds[0])) {
  53. params.put(UaaConstant.APPID, new String[]{appId});
  54. }
  55. String sign = authenticator.generateSign(secret, params);
  56. return DefaultResult.successResult(sign);
  57. } catch (Exception e) {
  58. return DefaultResult.failResult(EnumResultCode.E_GENERATE_SIGN_FAIL.getInfo());
  59. }
  60. }
  61. /**
  62. * 获取文件名
  63. * @param fileName
  64. * @param templateType
  65. * @return
  66. */
  67. public String getFileName(String fileName, String templateType) {
  68. String suffix;
  69. switch (templateType) {
  70. case "1":
  71. suffix = ".doc";
  72. break;
  73. case "2":
  74. suffix = ".docx";
  75. break;
  76. case "3":
  77. suffix = ".ppt";
  78. break;
  79. case "4":
  80. suffix = ".pptx";
  81. break;
  82. case "5":
  83. suffix = ".xls";
  84. break;
  85. case "6":
  86. suffix = ".xlsx";
  87. break;
  88. default:
  89. suffix = null;
  90. }
  91. if (suffix==null){
  92. return null;
  93. }
  94. String name = fileName + suffix;
  95. return name;
  96. }
  97. /**
  98. * 使用httpclint 发送文件
  99. * @author: qingfeng
  100. * @date: 2019-05-27
  101. * @param file
  102. * 上传的文件
  103. */
  104. public String uploadFile(String url , File file, String appId, String sign) {
  105. String result="";
  106. //构建HttpClient对象
  107. CloseableHttpResponse response = null;
  108. //构建POST请求
  109. HttpPost httpPost = new HttpPost(url);
  110. try {
  111. @Cleanup CloseableHttpClient client = HttpClients.createDefault();
  112. //构建文件体
  113. String fileName = file.getName();
  114. FileBody fileBody = new FileBody(file, ContentType.MULTIPART_FORM_DATA, fileName);
  115. HttpEntity httpEntity = MultipartEntityBuilder
  116. .create()
  117. .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
  118. .addPart("file", fileBody)
  119. .addTextBody("appId",appId)
  120. .addTextBody("sign",sign)
  121. .build();
  122. httpPost.setEntity(httpEntity);
  123. // 执行http请求
  124. response = client.execute(httpPost);
  125. result = EntityUtils.toString(response.getEntity(), "utf-8");
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. return result;
  130. }
  131. /**
  132. * 下载文件到指定目录
  133. * @param fileUrl
  134. * @param savePath
  135. * @throws Exception
  136. */
  137. public void downloadFile(String fileUrl,String savePath) throws Exception {
  138. File file=new File(XSSEscape.escapePath(savePath));
  139. //判断文件是否存在,不存在则创建文件
  140. if(!file.exists()){
  141. file.createNewFile();
  142. }
  143. URL url = new URL(fileUrl);
  144. HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
  145. urlCon.setConnectTimeout(6000);
  146. urlCon.setReadTimeout(6000);
  147. int code = urlCon.getResponseCode();
  148. if (code != HttpURLConnection.HTTP_OK) {
  149. throw new Exception(MsgCode.FA046.get());
  150. }
  151. @Cleanup DataInputStream in = new DataInputStream(urlCon.getInputStream());
  152. @Cleanup FileOutputStream fileOutputStream = new FileOutputStream(savePath);
  153. @Cleanup DataOutputStream out = new DataOutputStream(fileOutputStream);
  154. byte[] buffer = new byte[2048];
  155. int count = 0;
  156. while ((count = in.read(buffer)) > 0) {
  157. out.write(buffer, 0, count);
  158. }
  159. try {
  160. out.close();
  161. in.close();
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. /**
  167. * 上传文件到永中
  168. * @param
  169. * @return
  170. */
  171. public String uploadFileInPreview(InputStream inputStream,String fileName) throws IOException {
  172. //获取签名
  173. Map<String, String[]> parameter = new HashMap<String, String[]>();
  174. parameter.put("appId", new String[]{YozoParams.APP_ID});
  175. String sign = this.generateSign(YozoParams.APP_ID, YozoParams.APP_KEY, parameter).getData();
  176. String url= "http://dmc.yozocloud.cn/api/file/upload?appId="+YozoParams.APP_ID+"&sign="+sign;
  177. @Cleanup CloseableHttpClient httpClient = HttpClients.createDefault();
  178. String result ="";
  179. HttpEntity httpEntity =null;
  180. HttpEntity responseEntity = null;
  181. try {
  182. HttpPost httpPost = new HttpPost(url);
  183. MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
  184. multipartEntityBuilder.setCharset(Charset.forName("utf-8"));
  185. multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//加上此行代码解决返回中文乱码问题
  186. multipartEntityBuilder.addBinaryBody("file", inputStream, ContentType.MULTIPART_FORM_DATA, fileName);
  187. httpEntity = multipartEntityBuilder.build();
  188. httpPost.setEntity(httpEntity);
  189. CloseableHttpResponse response = httpClient.execute(httpPost);
  190. responseEntity = response.getEntity();
  191. result = EntityUtils.toString(responseEntity,Charset.forName("UTF-8"));
  192. } catch (Exception e) {
  193. e.printStackTrace();
  194. }finally {
  195. if (inputStream!=null){
  196. inputStream.close();
  197. }
  198. }
  199. return result;
  200. }
  201. }