资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

httpclient通过postmultipart/form-data上传文件

httpclient通过post  multipart/form-data 上传文件

public static String httpClientUploadFile(String url, File file) {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                String result = "";
                //每个post参数之间的分隔。随意设定,只要不会和其他的字符串重复即可。
                String boundary ="--------------4585696313564699";
                try {
                        //文件名
                        String fileName = file.getName();
                        HttpPost httpPost = new HttpPost(url);
                        //设置请求头
                        httpPost.setHeader("Content-Type","multipart/form-data; boundary="+boundary);

                        //HttpEntity builder
                        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                        //字符编码
                        builder.setCharset(Charset.forName("UTF-8"));
                        //模拟浏览器
                        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                        //boundary
                        builder.setBoundary(boundary);
                        //multipart/form-data
                        builder.addPart("multipartFile",new FileBody(file));
                        // binary
//            builder.addBinaryBody("name=\"multipartFile\"; filename=\"test.docx\"", new FileInputStream(file), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
                        //其他参数
                        builder.addTextBody("filename", fileName,  ContentType.create("text/plain", Consts.UTF_8));
                        //HttpEntity
                        HttpEntity entity = builder.build();
                        httpPost.setEntity(entity);
                        // 执行提交
                        HttpResponse response = httpClient.execute(httpPost);
                        //响应
                        HttpEntity responseEntity = response.getEntity();
                        if (responseEntity != null) {
                                // 将响应内容转换为字符串
                                result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                } finally {
                        try {
                                httpClient.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                System.err.println("result"+result);
                return result;
        }

        //main 方法
        public static void main(String[] args) {
                httpClientUploadFile("http://127.0.0.1:8080/upload",new File("d:/test/test.docx"));
        }

分享题目:httpclient通过postmultipart/form-data上传文件
分享URL:http://cdkjz.cn/article/iesgjp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220