Home >Java >javaTutorial >Why is my Java HttpClient file upload to a PHP server failing, even though the connection is successful?

Why is my Java HttpClient file upload to a PHP server failing, even though the connection is successful?

DDD
DDDOriginal
2024-12-07 09:43:12696browse

Why is my Java HttpClient file upload to a PHP server failing, even though the connection is successful?

File Uploading Using Java HttpClient and PHP

Problem:

You aim to create a Java application that can upload files to an Apache server running PHP using the Jakarta HttpClient library. The PHP server utilizes a simple script to receive and store the uploaded files. However, upon execution, you encounter an unusual issue where the file upload fails, as reported by the PHP script. The code execution response indicates a successful connection, but the file remains unnoticed by PHP.

Solution:

The initial Java code you provided contained an error. The correct implementation using the Java HttpClient library is shown below:

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();
  }
}

Analysis:

The key modification is the use of MultipartEntity. In the previous code, a FileEntity was employed. However, MultipartEntity is more suited for file uploads, as it allows for the inclusion of additional parameters and the specification of content types.

After utilizing MultipartEntity, the file upload was successful, and the PHP file received the upload without any issues. The is_uploaded_file() function returned true, and the $_FILES array was properly populated with data about the uploaded file.

The above is the detailed content of Why is my Java HttpClient file upload to a PHP server failing, even though the connection is successful?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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