okhttp3 上传文件

     String baseUrl = "http://localhost:9000/file";
        File file = new File("E:/girl.jpg");
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        MultipartBody body = new MultipartBody.Builder().setType(MediaType.parse("multipart/form-data"))
                .addFormDataPart("file", file.getName(), fileBody).build();
        Request request = new Request.Builder().post(body).url(baseUrl).build();

        long start = new Date().getTime();
        okHttpClient.newCall(request).execute().body().string();
        long end = new Date().getTime();
        System.out.println("use "+(end-start));

 

上一篇:Apache HttpClient在PUT/POST时的一个坑


下一篇:(三)微服务消费者订单Module模块