package com.hand.att; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.util.*; /** * description * * @author Administrator 2023/01/03 15:07 */ public class HttpPostAttUtil { /*public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception { try { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append( url) .append("?"); int i=1; Set<Map.Entry<String, String>> set = param.entrySet(); Iterator<Map.Entry<String, String>> it = set.iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next(); if(i==1){ stringBuffer.append(entry.getKey()+"="+entry.getValue()); i++; }else{ stringBuffer.append("&"+entry.getKey()+"="+entry.getValue()); } } String post_url = stringBuffer.toString(); System.out.println(post_url); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(post_url); MultipartEntityBuilder builder=MultipartEntityBuilder.create(); builder.addBinaryBody("file",file); HttpEntity httpEntity=builder.build(); httpPost.setHeader("Authorization", tokentype+" "+token); httpPost.setEntity(httpEntity);//设置请求参数 HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); String result = EntityUtils.toString(responseEntity, java.nio.charset.Charset.forName("UTF-8")); return result; } catch (Exception ex) { return ex.getMessage() ; } }*/ public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception { /* HttpClient httpClient = new HttpClient(); HttpServletResponse response = null; PostMethod post = new PostMethod(url); String resultString = ""; List<Part> list = new ArrayList<>(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); try { list.add(new FilePart("file", file)); if (param != null) { for (String key : param.keySet()) { list.add(new StringPart(key, param.get(key), "UTF-8")); } } Part[] parts = list.toArray(new Part[list.size()]); MultipartRequestEntity entity = new MultipartRequestEntity(parts, new HttpMethodParams()); post.setRequestEntity(entity); post.addRequestHeader( "Authorization", tokentype+" "+token); System.out.println(entity.getContentType()); int status = httpClient.executeMethod(post); String result = post.getResponseBodyAsString() ; System.out.println(result); } catch (Exception e) { e.printStackTrace(); } finally { post.releaseConnection(); } return "";*/ HttpClient client =null; String result=null; try { HttpPost postMethod = new HttpPost(url); client = new DefaultHttpClient(); FileBody filebody = new FileBody(file); MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("file", filebody); multipartEntity.addPart("documentTypeCode",new StringBody(param.get("documentTypeCode"))); multipartEntity.addPart("primaryField", new StringBody(param.get("primaryField"))); multipartEntity.addPart("documentSource", new StringBody(param.get("documentSource"))); multipartEntity.addPart("companyCode", new StringBody(param.get("companyCode"))); postMethod.setEntity(multipartEntity); HttpResponse response = client.execute(postMethod); HttpEntity httpEntity = response.getEntity(); result = EntityUtils.toString(httpEntity); }catch (Exception e){ e.printStackTrace(); }finally { client.getConnectionManager().shutdown(); // 关闭连接. return result; } } public static void main(String[] args) throws Exception { //String resPonse=httpPostAttImport("http://apistage.huilianyi.com/gateway/e-archives/api/open/v1/import/document","[{\"companyCode\":\"0001\",\"particularYear\":\"2022\",\"documentTypeCode\":\"DT002002\",\"originalNumber\":\"1234567890\",\"primaryField\":\"SAP_TEST0000001\",\"isPaper\":\"true\",\"documentSource\":\"SAP\",\"attachmentList\":[{\"fileURL\":\"https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/314e251f95cad1c8ea88af74793e6709c93d51b5.jpg\",\"fileName\":\"314e251f95cad1c8ea88af74793e6709c93d51b5.jpg\",\"attachmentOID\":\"\"},{\"fileURL\":\"https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/314e251f95cad1c8ea88af74793e6709c93d51b5.jpg\",\"fileName\":\"314e251f95cad1c8ea88af74793e6709c93d51b5.jpg\",\"attachmentOID\":\"\",\"attachTypeCode\":\"0\"}],\"ruleOID\":\"684fb42f-1df4-4710-8ac7-ea0146786a05\",\"securityLevelCode\":\"CONFIDENTIAL\",\"fieldValueList\":[{\"fieldCode\":\"AMOUNT\",\"value\":\"13\"},{\"fieldCode\":\"COMPANY\",\"value\":\"阳光照明\"},{\"fieldCode\":\"DOC_NAME\",\"value\":\"日常报销单\"},{\"fieldCode\":\"CURRENCY\",\"value\":\"CNY\"},{\"fieldCode\":\"CREATED_DATE\",\"value\":\"2021-01-01 12:00:00\"},{\"fieldCode\":\"CREATION_NAME\",\"value\":\"张三\"},{\"fieldCode\":\"REMARK\",\"value\":\"去年的报销\"}]}]","4557fa35-d2da-479a-be68-323b6be619d9","Bearer"); File file=new File("D:\\aurora_reference.pdf"); HashMap mmp = new HashMap(); mmp.put("documentTypeCode", "workflow_QC01"); mmp.put("primaryField", "CF-4252CE7A665C4562BF8E8AC3AD79BF1D"); mmp.put("documentSource", "CF"); mmp.put("companyCode", "HL"); //String resPonse1=doPostFile1("http://apistage.huilianyi.com/gateway/e-archives/api/open/v1/attachment/upload",mmp,file,"4557fa35-d2da-479a-be68-323b6be619d9","Bearer"); String resPonse2=UploadFile("http://apistage.huilianyi.com/gateway/e-archives/api/open/v1/attachment/upload",mmp,file,"4557fa35-d2da-479a-be68-323b6be619d9","Bearer"); System.out.println(resPonse2); } }