Home > Backend Development > PHP Tutorial > Why Does My Java HttpClient File Upload to PHP Fail, and How Can I Fix It Using MultipartEntity?

Why Does My Java HttpClient File Upload to PHP Fail, and How Can I Fix It Using MultipartEntity?

Mary-Kate Olsen
Release: 2024-12-09 17:52:10
Original
156 people have browsed it

Why Does My Java HttpClient File Upload to PHP Fail, and How Can I Fix It Using MultipartEntity?

Uploading a File using Java HttpClient with PHP

When attempting to upload a file to an Apache server with PHP using Java HttpClient library version 4.0 beta2, an issue arises where the PHP script fails to detect the file uploaded by the Java application.

In the given Java code snippet, the error lies in the usage of a FileEntity to encapsulate the file data. The corrected Java class below uses a MultipartEntity instead, which is essential for multipart/form-data uploads:

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

This correction ensures the file is uploaded properly, and the PHP script's is_uploaded_file() method should return true upon successful file transmission, as intended.

The above is the detailed content of Why Does My Java HttpClient File Upload to PHP Fail, and How Can I Fix It Using MultipartEntity?. 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