代码模拟带文件上传的表单

使用的是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

时间: 2024-08-22 14:53:31

代码模拟带文件上传的表单的相关文章

c# 模拟提交带文件上传的表单

------WebKitFormBoundary8gsfyexbIoO5OYAa Content-Disposition: form-data; name="username" admin ------WebKitFormBoundary8gsfyexbIoO5OYAa Content-Disposition: form-data; name="submit" 登录 ------WebKitFormBoundary8gsfyexbIoO5OYAa Content-D

springmvc文件上传及表单数据封装

补充: form表单需要提交时间,springmvc封装到实体类的Date字段时,丢失时分秒,可以在controller中添加     @InitBinder     public void initBinder(WebDataBinder binder) {         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         dateFormat.setLenient(f

文件上传(表单文件上传)

文件上传是开发一个网站最基本的一个需求功能 前台页面的设置: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerNa

swift文件上传及表单提交

var carData:NSMutableDictionary = NSMutableDictionary(); var request:NSMutableURLRequest = NSMutableURLRequest();         request.URL = NSURL(string: "YOUR URL ADDR");         request.HTTPMethod = "POST";         request.timeoutInterva

AJAX提交form表单带文件上传

过了三天才想要写博客,这样不好,要改正 在做毕设的时候,用户发帖涉及到了文件上传的问题,在这里记录一下 背景: 在用户发帖的时候,用户只想发表文字postText,还有些用户想在发表postText的同时还发表一些图片,如何做? 上代码 不写的太细了,和流水账似的,挑重点记录一下. 1.前台的文件上传 本来想用form表单直接上传了,但是form提交时会刷新整个页面,但这不是我想要的,所以使用了ajax提交form表单. 利用ajax提交表单需要用到jquery.form.js这个包,网上有很多

关于Java网络爬虫---模拟txt文件上传操作。

业务需求是这样的,公司400业务中客户使用的,400电话号码,可以添加多个目的码你可以理解为转接号码: 这些配置的目的码我们会在网关服务器上配置成白名单,既拥有某些权限.先提出的要求是先添加或者变动目的码要及时同步到网关. 场景: 1.我们的网关服务器接受的白名单(目的码)是已txt文件上传的,数据按照制定的格式保存在txt里面. 2.利用Java网络爬虫模拟txt文件上传.------2018-4-7现在不写了,代码在公司电脑上明天总结一下在写. 原文地址:https://www.cnblog

struts2 文件的上传下载 表单的重复提交 自定义拦截器

文件上传中表单的准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置为 post 需添加 <input type=“file”> 字段. Struts 对文件上传的支持 在 Struts 应用程序里, FileUpload 拦截器和 Jakarta Commons FileUpload 组件可以完成文件的上传. 步骤:1. 在 Jsp 页面的文件上传表单里使用

php 利用http上传协议(表单提交上传图片 )

主要就是利用php 的 fsocketopen 消息传输. 这里先通过upload.html 文件提交,利用chrome抓包,可以看到几个关键的信息. 首先指定了表单类型为multipart/form-data;. boundary是分隔符 因为上传文件不在使用原有的http协议了.请求内容不再可能以 x = y方式发送了.而使用了 分隔符 字段内容 分隔符号 字段内容2 而boundary就是指定分隔符号的标志. 请求的内容就应该是这样的了. 在来看下消息体 #socket_upload.ph

记一种有图片上传的表单提交

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>动态加载下拉框中的类别_文件上传_添加商品</title