|
|
@@ -0,0 +1,302 @@
|
|
|
+package com.kingdee.eas.custom.beisen.recruitment.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.kingdee.bos.BOSException;
|
|
|
+import com.kingdee.bos.Context;
|
|
|
+import com.kingdee.bos.util.BOSUuid;
|
|
|
+import com.kingdee.eas.base.attachment.AttachmentFactory;
|
|
|
+import com.kingdee.eas.base.attachment.AttachmentInfo;
|
|
|
+import com.kingdee.eas.base.attachment.BoAttchAssoFactory;
|
|
|
+import com.kingdee.eas.base.attachment.BoAttchAssoInfo;
|
|
|
+import com.kingdee.eas.common.EASBizException;
|
|
|
+import com.kingdee.shr.attachment.AttachmentState;
|
|
|
+import com.kingdee.shr.attachment.AttachmentTypeEnum;
|
|
|
+import com.kingdee.shr.attachment.SHRAttachmentExtFactory;
|
|
|
+import com.kingdee.shr.attachment.SHRAttachmentExtInfo;
|
|
|
+import com.kingdee.shr.preentry.PreEntryPersonInfo;
|
|
|
+import com.kingdee.util.StringUtils;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.client.utils.URIBuilder;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.json.JSONException;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+
|
|
|
+public class Helper {
|
|
|
+ /***
|
|
|
+ * 获取请求数据
|
|
|
+ * @param url
|
|
|
+ * @param header
|
|
|
+ * @param requestBody
|
|
|
+ * @param method "GET" OR "POST"
|
|
|
+ * @return
|
|
|
+ * @throws URISyntaxException
|
|
|
+ * @throws JSONException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ClientProtocolException
|
|
|
+ */
|
|
|
+ public static JSONObject getURL(String url, Map<String, String> header, JSONObject requestBody, String method) throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ Iterator it = requestBody.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ uriBuilder.addParameter(key, value);
|
|
|
+ }
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ }
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取请求数据
|
|
|
+ * @param url
|
|
|
+ * @param header
|
|
|
+ * @param requestBody
|
|
|
+ * @param method "GET" OR "POST"
|
|
|
+ * @return
|
|
|
+ * @throws URISyntaxException
|
|
|
+ * @throws JSONException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ClientProtocolException
|
|
|
+ */
|
|
|
+ public static JSONObject getURL(String url, Map<String, String> header, JSONArray requestBody, String method) throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ List paramList = new ArrayList();
|
|
|
+ for (int i = 0; i < requestBody.size(); i++) {
|
|
|
+ paramList.add(requestBody.get(i));
|
|
|
+ }
|
|
|
+ uriBuilder.addParameters(paramList);
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ StringEntity requestEntity = new StringEntity(requestBody.toString(), "UTF-8");
|
|
|
+ httpPost.setEntity(requestEntity);
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ }
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 获取请求数据
|
|
|
+ * @param url
|
|
|
+ * @param header
|
|
|
+ * @param requestBody
|
|
|
+ * @param method "GET" OR "POST"
|
|
|
+ * @return
|
|
|
+ * @throws URISyntaxException
|
|
|
+ * @throws JSONException
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ClientProtocolException
|
|
|
+ */
|
|
|
+ public static JSONObject getURLEncoded(String url, Map<String, String> header, JSONObject requestBody, String method) throws URISyntaxException, JSONException, ClientProtocolException, IOException {
|
|
|
+ JSONObject responseJson = new JSONObject();
|
|
|
+ HttpResponse response;
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ if ("GET".equals(method)) {
|
|
|
+ URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
+ HttpGet httpGet = new HttpGet(uriBuilder.build());
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpGet.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ Iterator it = requestBody.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ uriBuilder.addParameter(key, value);
|
|
|
+ }
|
|
|
+ response = httpClient.execute(httpGet);
|
|
|
+ } else {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ for (Entry<String, String> entry : header.entrySet()) {
|
|
|
+ httpPost.addHeader(entry.getKey(), entry.getValue());
|
|
|
+ }
|
|
|
+ ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
|
|
|
+ Iterator it = requestBody.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ String value = requestBody.getString(key);
|
|
|
+ list.add(new BasicNameValuePair(key, value));
|
|
|
+ }
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ }
|
|
|
+ String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ responseJson = JSONObject.parseObject(responseBody);
|
|
|
+ httpClient.close();
|
|
|
+ return responseJson;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 根据文件下载地址读取字节流
|
|
|
+ * @param urlStr
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static byte[] getBytesByNetURL(String urlStr) throws IOException {
|
|
|
+// RestTemplate restTemplate = new RestTemplate();
|
|
|
+// ResponseEntity<byte[]> responseEntity = restTemplate.exchange(urlStr, HttpMethod.GET, null, byte[].class);
|
|
|
+// byte[] fileContent = responseEntity.getBody();
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+// //设置超时时间
|
|
|
+ conn.setConnectTimeout(5 * 1000);
|
|
|
+// //通过输入流获取图片数据
|
|
|
+ InputStream in = conn.getInputStream();
|
|
|
+// //得到图片的二进制数据
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ return outputStream.toByteArray();
|
|
|
+// return fileContent;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 保存附件
|
|
|
+ * @param filename
|
|
|
+ * @param uipk
|
|
|
+ * @param data
|
|
|
+ * @throws BOSException
|
|
|
+ * @throws EASBizException
|
|
|
+ */
|
|
|
+ public static void insertAttachment(Context ctx, String filename, PreEntryPersonInfo preEntryPersonInfo, String uipk, byte[] data, String userId, String attType, String qdxswz) {
|
|
|
+ try {
|
|
|
+ if (data.length > 20971520) {
|
|
|
+ throw new BOSException("保存附件失败,附件大小超过20MB!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(filename) || null == preEntryPersonInfo.getId() || data.length <= 0) {
|
|
|
+ throw new BOSException("保存附件失败,参数格式不正确!");
|
|
|
+ }
|
|
|
+// IPEAttachment iPEAttachment = PEAttachmentFactory.getLocalInstance(ctx);
|
|
|
+// PEAttachmentInfo peAttachmentInfo = null;
|
|
|
+// PEAttachmentCollection attachmentCollection = iPEAttachment
|
|
|
+// .getPEAttachmentCollection("where talent = '" + preEntryPersonInfo.getId().toString() + "'");
|
|
|
+// if (null!=attachmentCollection&&attachmentCollection.size()>0) {
|
|
|
+// peAttachmentInfo = attachmentCollection.get(0);
|
|
|
+// } else {
|
|
|
+// peAttachmentInfo = new PEAttachmentInfo();
|
|
|
+// peAttachmentInfo.setTalent(preEntryPersonInfo);
|
|
|
+// }
|
|
|
+// iPEAttachment.save(peAttachmentInfo);
|
|
|
+// String boId = peAttachmentInfo.getId().toString();
|
|
|
+// //附件
|
|
|
+// IAttachment iAttachment = AttachmentFactory.getLocalInstance(ctx);
|
|
|
+// //附件与业务单据关联表
|
|
|
+// IBoAttchAsso iBoAttchAsso = BoAttchAssoFactory.getLocalInstance(ctx);
|
|
|
+// iBoAttchAsso.delete("where boID = '"+boId+"'");
|
|
|
+// ISHRAttachmentExt iSHRAttachmentExt = SHRAttachmentExtFactory.getLocalInstance(ctx);
|
|
|
+// iSHRAttachmentExt.delete("where boID = '"+boId+"'");
|
|
|
+ String boId = preEntryPersonInfo.getId().toString();
|
|
|
+ SHRAttachmentExtInfo attchExt = new SHRAttachmentExtInfo();
|
|
|
+ AttachmentInfo ai = new AttachmentInfo();
|
|
|
+ ai.setName(filename.substring(0, filename.lastIndexOf('.')));
|
|
|
+ ai.setSimpleName(filename.substring(filename.lastIndexOf(".") + 1));
|
|
|
+ ai.setDescription("");
|
|
|
+ ai.setFile(data);
|
|
|
+ ai.setIsShared(false);
|
|
|
+ ai.setSharedDesc("否");
|
|
|
+ ai.setAttachID("" + System.currentTimeMillis());
|
|
|
+ ai.setType(getFileType(filename));
|
|
|
+
|
|
|
+ attchExt.setAttachment(ai);
|
|
|
+ attchExt.setName(filename);
|
|
|
+ attchExt.setPropertyName("null0");
|
|
|
+ attchExt.setType(AttachmentTypeEnum.FORM);
|
|
|
+ attchExt.setState(AttachmentState.UNSAVE);
|
|
|
+ attchExt.setBunding(userId + "#" + uipk);
|
|
|
+ attchExt.setBoID(boId);
|
|
|
+ attchExt.setState(AttachmentState.SAVE);
|
|
|
+ AttachmentFactory.getLocalInstance(ctx).addnew(ai);
|
|
|
+
|
|
|
+ attchExt.setState(AttachmentState.SAVE);
|
|
|
+ BoAttchAssoInfo boAttchAssoInfo = new BoAttchAssoInfo();
|
|
|
+ boAttchAssoInfo.setBoID(boId);
|
|
|
+ boAttchAssoInfo.setAssoBusObjType(String.valueOf(BOSUuid.getBOSObjectType(boId, true)));
|
|
|
+ boAttchAssoInfo.setAssoType("Added Accessories");
|
|
|
+ boAttchAssoInfo.setAttachment(ai);
|
|
|
+ BoAttchAssoFactory.getLocalInstance(ctx).addnew(boAttchAssoInfo);
|
|
|
+
|
|
|
+ SHRAttachmentExtFactory.getLocalInstance(ctx).addnew(attchExt);
|
|
|
+ } catch (EASBizException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (BOSException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getFileType(String fullname) {
|
|
|
+ String extname = fullname.substring(fullname.lastIndexOf(".") + 1, fullname.length());
|
|
|
+ if (!"doc".equalsIgnoreCase(extname) && !"docx".equalsIgnoreCase(extname)) {
|
|
|
+ if (!"xls".equalsIgnoreCase(extname) && !"xlsx".equalsIgnoreCase(extname) && !"xlsm".equalsIgnoreCase(extname) && !"xlsb".equalsIgnoreCase(extname)) {
|
|
|
+ if (!"ppt".equalsIgnoreCase(extname) && !"pptx".equalsIgnoreCase(extname) && !"pptm".equalsIgnoreCase(extname)) {
|
|
|
+ return "txt".equalsIgnoreCase(extname) ? "TEXT 文本文件" : "未知文件类型(." + extname + ")";
|
|
|
+ } else {
|
|
|
+ return "Microsoft PowerPoint 幻灯片";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return "Microsoft Excel 表格";
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return "Microsoft Word 文档";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|