• 论httpclient上传带参数【commons-httpclient和apache httpclient区别】


    需要做一个httpclient上传,然后啪啪啪网上找资料

    1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试

    		PostMethod filePost = new PostMethod(url);
    		filePost.setParameter("system", "vinuxpost");
    		try {
    			Part part[] = UploadRequestHelper.getPart(request);
    			filePost.setRequestEntity(new MultipartRequestEntity(part, filePost
    					.getParams()));
    			HttpClient client = new HttpClient();
    			client.getHttpConnectionManager().getParams()
    					.setConnectionTimeout(5000);
    			int status = client.executeMethod(filePost);
    			System.out.println(status);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    

      其中一段主要代码,上传文件方式用的是byte方式,然后带上需要的参数,然后就遇到了一个奇怪的问题,就是参数过不来,然后去掉了上传文件的部分,参数就过来了,好奇怪,用了一下午,网上找资料还是没有解决,目前还没有解决!!!希望有人能告诉我,哈哈哈,也许可能是jar包版本的问题,我看网上好多资料都是可以带参数上传文件的。

    希望大神能帮我解决上面的问题

    2.然后接着apache httpclient上传,上代码

    CloseableHttpClient httpClient = HttpClients.createDefault();
    		HttpPost httpPost = new HttpPost(url);
    		List<MultipartFile> listFile = UploadRequestHelper
    				.getListMultipartFile(request);
    		// 创建待处理的表单域内容文本
    		StringBody system = new StringBody("vinuxpost");
    		MultipartEntity reqEntity = new MultipartEntity();
    		for (MultipartFile file : listFile) {
    			ByteArrayBody bab = new ByteArrayBody(file.getBytes(),
    					file.getName());
    			reqEntity.addPart(file.getName(), bab);
    		}
    		reqEntity.addPart("system", system);
    		httpPost.setEntity(reqEntity);
    		CloseableHttpResponse response = httpClient.execute(httpPost);
    		System.out.println("状态:" + response.getStatusLine());
    

      不出意外,成功了,可以继续下面的工作了,哈哈哈

    总结,commons-httpclient和apache httpclient 的区别:

      首先commons-httpclient好像很少更新了,而且文档不是很充足,网上的例子很多都是重复的,apache httpclient 一直在更新,哈哈,资料比较多!!!

    贴上apache httpclient maven 嘿嘿就这了

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.3</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.2.1</version>
        </dependency>

    需要 httpmime别忘了

  • 相关阅读:
    堆排序
    伽马分布
    隔壁-贪心
    对刚—约瑟夫环
    站军姿-两圆并集
    单纯的线性筛素数
    3兔子
    2.圆桌游戏
    1.花
    历史
  • 原文地址:https://www.cnblogs.com/ppli/p/5344303.html
Copyright © 2020-2023  润新知