Home > Java > javaTutorial > How to Correctly Upload Files from Java HttpClient to a PHP Server?

How to Correctly Upload Files from Java HttpClient to a PHP Server?

Susan Sarandon
Release: 2024-12-01 17:30:12
Original
270 people have browsed it

How to Correctly Upload Files from Java HttpClient to a PHP Server?

Java HttpClient with PHP File Upload

In this article, we explore how to utilize the Java HttpClient library version 4.0 beta2 to upload a file to an Apache server running PHP.

Scenario and Issue

A developer aims to transfer a file from Java to PHP via the Jakarta HttpClient library. However, upon executing the request, the PHP server doesn't detect the uploaded file. The is_uploaded_file() function returns false, and the $_FILES variable remains empty.

Solution

To rectify this, we need to revise the Java code. The problem stems from the incorrect usage of the FileEntity. Here's the corrected code:

import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
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.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


public class PostFile {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://localhost:9001/upload.php");
    File file = new File("c:/TRASH/zaba_1.jpg");

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    mpEntity.addPart("userfile", cbFile);


    httppost.setEntity(mpEntity);
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();
  }
}
Copy after login

The key change is using MultipartEntity instead of FileEntity. This allows us to send the file as a multipart form-data request, which is the expected format for PHP file uploads.

The above is the detailed content of How to Correctly Upload Files from Java HttpClient to a PHP Server?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template