zoukankan      html  css  js  c++  java
  • httpclient upload file

      依赖

    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>

      demo

    package com.oy.controller;
    
    import java.io.File;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    @RestController
    public class PostMultiData {
        
        public static void main(String[] args) throws Exception {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                String uri = "http://localhost:7070/file";
                HttpPost httppost = new HttpPost(uri);
    
                String path = "e:/logo.jpg";
                File file = new File(path);
                FileBody upFile = new FileBody(file);
                StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
    
                HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", upFile)
                        .addPart("comment", comment)
                        .build();
    
                httppost.setEntity(reqEntity);
    
                System.out.println("executing request " + httppost.getRequestLine());
                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    System.out.println("----------------------------------------");
                    System.out.println(response.getStatusLine());
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        System.out.println("Response content length: " + resEntity.getContentLength());
                    }
                    EntityUtils.consume(resEntity);
                } finally {
                    response.close();
                }
            } finally {
                httpclient.close();
            }
        }
        
        @PostMapping("/file")
        public void file(MultipartFile file) throws Exception {
            System.out.println(file.getOriginalFilename());
            file.transferTo(new File("e:/abc.jpg"));
        }
    }

    ---

  • 相关阅读:
    Mybatis分页插件PageHelper简单使用
    UUID
    JavaWeb初学者session的使用
    轻松理解AOP思想(面向切面编程)
    DOM操作中,getElementByXXXX 和 querySelector 的区别
    DOM操作中,遍历动态集合的注意事项。ex: elem.children
    微信官方团队放出了UI库,看来以后前端还要学WeChatUI了,哈哈
    jQuery中使用$.each()遍历后台响应的json字符串问题
    平衡树
    后缀数组原理浅析(转载自tqx)
  • 原文地址:https://www.cnblogs.com/xy-ouyang/p/14420034.html
Copyright © 2011-2022 走看看