How to upload a file using Java HttpClient library working with PHP

前端 未结 10 2269
旧巷少年郎
旧巷少年郎 2020-11-22 08:48

I want to write Java application that will upload a file to the Apache server with PHP. The Java code uses Jakarta HttpClient library version 4.0 beta2:

impo         


        
10条回答
  •  野性不改
    2020-11-22 09:23

    An update for those trying to use MultipartEntity...

    org.apache.http.entity.mime.MultipartEntity is deprecated in 4.3.1.

    You can use MultipartEntityBuilder to create the HttpEntity object.

    File file = new File();
    
    HttpEntity httpEntity = MultipartEntityBuilder.create()
        .addBinaryBody("file", file, ContentType.create("image/jpeg"), file.getName())
        .build();
    

    For Maven users the class is available in the following dependency (almost the same as fervisa's answer, just with a later version).

    
      org.apache.httpcomponents
      httpmime
      4.3.1
    
    

提交回复
热议问题