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

    ---

  • 相关阅读:
    VC窗口类的销毁-是否需要delete
    ScrollView在调试状态一点击就挂的原因(OnMouseActivate)
    TextOut与DrawText的区别
    NOIP2010 引水入城
    欧拉回路
    BZOJ 1202: [HNOI2005]狡猾的商人
    codevs 2491 玉蟾宫
    BZOJ 1059: [ZJOI2007]矩阵游戏
    BZOJ 1024: [SCOI2009]生日快乐
    ural 1297. Palindrome
  • 原文地址:https://www.cnblogs.com/xy-ouyang/p/14420034.html
Copyright © 2011-2022 走看看