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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient通过post multipart/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通过post multipart/form-data 上传文件的主要内容,如果未能解决你的问题,请参考以下文章

如何通过python twisted HTTPClient生成POST和GET请求?

java使用httpclient通过post方式提交表单失败求助

如何通过 HttpClient 在 POST 请求中将 JSON 数据作为正文发送

HttpClient通过Post方式发送Json数据

Android下通过HttpClient执行 HTTP POST 请求 的代码

取消 'HttpClient' POST 请求