Java 上传带参数的文件

String fileName = "hadoop_333.zip";
String filePath = "D:\\Keen\\WorkData\\hadoop_333.zip";
String url = "http://192.168.12.244/api/v1/file/upload";
String cookie = "SESSION=NWI2M2U4MWItNDQ0Yy00MDEwLWIwMjktOTBjNGVlYzIxZjNi";
String tenantId = "1614520761203032087";
try {
    OkHttpClient client = new OkHttpClient().newBuilder().build();

    RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
    .addFormDataPart("file",fileName, RequestBody.create(MediaType.parse("application/octet-stream"), new File(filePath)))
    .addFormDataPart("envType","dev").build();

    Request request = new Request.Builder()
            .url(url)
            .method("POST", body)
            .addHeader("Cookie", cookie)
            .addHeader("tenantId", tenantId)
            .addHeader("Accept","application/json, text/plain, */*")
            .addHeader("Accept-Encoding","gzip, deflate")
            .addHeader("Content-Type","multipart/form-data; boundary=----WebKitFormBoundaryXzqIh43IbaRnuUF6")
            .build();
    Response response = client.newCall(request).execute();
    System.out.println(response.body().string());
} catch (IOException e) {
    throw new Exception(e.getMessage(), e);
}

你可能感兴趣的:(java,文件上传带参数)