zoukankan      html  css  js  c++  java
  • 代码模拟带文件上传的表单

    使用的是Apache httpcomponents

    相关的maven依赖jar包如下

    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4-alpha1</version>
    </dependency>
    <dependency>
    <!--必须是4.3以后版本 --> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.1</version> </dependency>
     public static void main(String[] args) throws Exception {
            if (args.length != 1)  {
                System.out.println("File path not given");
                System.exit(1);
            }
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpPost httppost = new HttpPost("http://localhost:8080" +
                        "/servlets-examples/servlet/RequestInfoExample");
    
                FileBody bin = new FileBody(new File(args[0]));
                StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
    
                HttpEntity reqEntity = MultipartEntityBuilder.create()
                        .addPart("bin", bin)
                        .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();
            }
        }

    http://hc.apache.org/httpcomponents-client-ga/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java

  • 相关阅读:
    Spring__SpringMVC__Mybatis整合
    Mybatis__延迟加载
    mybatis__关联关系__1对1,1对多
    动态SQL基本语句用法
    log4j的使用 && slf4j简单介绍
    Mybatis接口与映射文件
    ORM简介 && MyBatis和Hibernate的不同 && 动态代理简单实现Mybatis基本使用
    20169207《linux内核原理与分析》第二周作业
    关于Linux学习中的问题和体会
    [algothrim]URL相似度计算的思考
  • 原文地址:https://www.cnblogs.com/zhengqun/p/3991229.html
Copyright © 2011-2022 走看看