使用httpclient提交表单方式上传文件

HttpResponse response=null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            String url="http://192.168.1.135:3000/v2/accounts/jHb9CJAWyB4jr91VRWn96DkukG4bwdtyTh/upload?validated=true";
            File file=new File("D:","v2.0.2.pdf");
            
            //输入流,
            InputStream in = new FileInputStream(file);
            
            HttpPost httpPost = new HttpPost(url);
            
            /**设置 contenttype 以form形式上传文件,  强制使用UTF-8编码**/
            ContentType type=ContentType.MULTIPART_FORM_DATA.withCharset("UTF-8");
            HttpEntity httpEntity = MultipartE
 

ntityBuilder
                    .create()
                    .addPart("secret",new StringBody("snoPBjXtMeMyMHUVTgbuqAfg1SUTb",type))
                    .addPart("DatagramSN",new StringBody(new Date().getTime()+"嗷嗷嗷啊",type))
                    .addPart("destination",new StringBody("jJpba91JvNw9PCmCfxUFjJyBGdrro9ThnA",type))
                    .addPart("amount", new StringBody("1",type))
                    .addPart("DatagramType",new StringBody(type.getMimeType(),type))
                    .addPart("TerminalID",new StringBody("呵呵",type))
                    .addPart("NodeID", new StringBody("你好",type))
                    
                    /**
                     *   以原生文件形式  上传文件
                     */
//                    .addPart("DatagramName", new FileBody(file))
                    
                    /**
                     *   以流的形式上传  并且使用base64  对文件名称 进行加密
                     */
//                    .addBinaryBody("DatagramName", in,type,Base64.getEncoder().encodeToString("银杏树标准服务接口v2.0.2.pdf".getBytes()))
                    
                    /**
                     *   以流的形式上传
                     */
//                    .addBinaryBody("DatagramName", in,type,file.getName())
                    
                    /**
                     *   以文件形式上传  并且使用base64  对文件名称 进行加密
                     */
//                    .addBinaryBody("DatagramName", new File("D:\\timg.jpg"),type,Base64.getEncoder().encodeToString("timg.jpg".getBytes()))
                    
                    /**
                     *文件上传 添加 contenttype
                     */
                    .addBinaryBody("DatagramName", file,type,file.getName())
                    
                    /**
                     * 强制使用UTF-8
                     */
                    .setCharset(Charset.forName("UTF-8"))
                    /**
                     * 添加浏览器兼容性
                     */
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .build();

            httpPost.setEntity(httpEntity);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(10000)
                    .setConnectTimeout(10000)
                    .build();
            httpPost.setConfig(requestConfig);
            response = httpclient.execute(httpPost);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值