zoukankan      html  css  js  c++  java
  • java模拟http请求上传文件,基于Apache的httpclient

    1.依赖

      模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加

         <dependency>
                <groupId>org.apache</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.1.2</version>
            </dependency>


    <dependency>
       <groupId>org.apache</groupId>
       <artifactId>httpclient</artifactId>
       <version>4.1.2</version>
    </dependency>

    2.源码

    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.InputStreamBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONObject;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    /**
     * Created by Administrator on 2016/1/19.
     */
    public class FileUploadTest {
    
        public static String upload(String url,String filePath){
            String fdfsPath = "";
            try {
                HttpClient httpclient = new DefaultHttpClient();
    
    //            HttpPost httppost = new HttpPost("http://127.0.0.1:8082/fileUpload.action");//请求格式
                HttpPost httppost = new HttpPost(url);
                File file = new File(filePath);
                String name = file.getName();
                InputStream in = new FileInputStream(file);
                MultipartEntity reqEntity = new MultipartEntity();
                InputStreamBody inputStreamBody = new InputStreamBody(in,name);
                StringBody fileNam = new StringBody(name);
    
                reqEntity.addPart("uploadFile", inputStreamBody);
                reqEntity.addPart("uploadFileName", fileNam);
    
                httppost.setEntity(reqEntity);
                HttpResponse response = httpclient.execute(httppost);
                int statusCode = response.getStatusLine().getStatusCode();
    
                if(statusCode == HttpStatus.SC_OK){
    
    //                System.out.println("服务器正常响应.....");
                    HttpEntity resEntity = response.getEntity();
                    JSONObject json = new JSONObject(EntityUtils.toString(resEntity).toString());
    
    
                    System.out.println(json.getString("returnResult"));
    //                System.out.println(EntityUtils.toString(resEntity));//httpclient自带的工具类读取返回数据
    //                System.out.println(resEntity.getContent());
    
                    EntityUtils.consume(resEntity);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }
    
      
    }

    3.解析

      上传参数:URL--上传的服务器端接口请求;filePath ---本地的文件或图片存储路径。

       

  • 相关阅读:
    springmvc常用注解之@Controller和@RequestMapping
    解决nginx负载均衡的session共享问题
    iOS顶部滑动菜单:FDSlideBar 与NinaPagerView
    C#中使用WeiFenLuo.WinFormsUI.Docking.dll实现窗口停靠效果
    [转]C#使用Log4Net记录日志
    ICSharpCode.SharpZipLib 开源压缩库使用示例
    WorldWind源码剖析系列:日志类Log
    [转]反射基础
    WorldWind源码剖析系列:星球球体的加载与渲染
    Vue 前端路由 vue-router
  • 原文地址:https://www.cnblogs.com/sagech/p/5156596.html
Copyright © 2011-2022 走看看