Java Okhttp带参数传输文件

Java And php

public static void uploadFile() throws IOException {

        OkHttpClient client = new OkHttpClient();
        String url="https://www.baidu.com/";//上传URL
        File file=new File("xxx/xxx/xxx.zip");//文件路径(本地)
        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        String filename = file.getName();
        requestBody.addFormDataPart("file", filename, body);
        //参数(key,value)
        requestBody.addFormDataPart("user","qwe");
        requestBody.addFormDataPart("password","123456");
        Request request = new Request.Builder().url(url).post(requestBody.build()).build();
        //请求超时时间5s
        client.newBuilder().readTimeout(5000, TimeUnit.MILLISECONDS).build().newCall(request);
        final Call call = client.newCall(request);
        Response response = call.execute();
        System.out.println(response.body().string());

    }
}

你可能感兴趣的:(java,php)