HttpPostAttUtil.java 6.85 KB
Newer Older
1 2 3 4 5
package com.hand.att;




Luochenglong's avatar
Luochenglong committed
6 7 8 9 10 11 12 13 14
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;
15 16

import javax.servlet.http.HttpServletResponse;
17
import java.io.File;
18
import java.util.*;
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/**
 * description
 *
 * @author Administrator 2023/01/03 15:07
 */
public class HttpPostAttUtil {









35
    /*public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        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);
58
            HttpEntity httpEntity=builder.build();
59 60 61 62 63 64 65 66 67
            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() ;
        }
68 69 70
    }*/

    public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
Luochenglong's avatar
Luochenglong committed
71
       /* HttpClient httpClient = new HttpClient();
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        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();
        }
Luochenglong's avatar
Luochenglong committed
97
        return "";*/
98 99


Luochenglong's avatar
Luochenglong committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        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;
        }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    }



    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);

    }

}