如何使用Java通过HTTP将文件上载到服务器到PHP5

如何使用Java通过HTTP将文件上载到服务器到PHP5,java,php,http,file-upload,server,Java,Php,Http,File Upload,Server,我一直在开发一个java/android应用程序,我需要能够从设备上拍摄一张照片并将其上传到我的ubuntu服务器上,该服务器运行在我的raspberry pi 2上 我想在php上测试upload函数,所以我制作了一个简单的html页面,将其转发到我的upload.php文件 index.html <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype

我一直在开发一个java/android应用程序,我需要能够从设备上拍摄一张照片并将其上传到我的ubuntu服务器上,该服务器运行在我的raspberry pi 2上

我想在php上测试upload函数,所以我制作了一个简单的html页面,将其转发到我的upload.php文件

index.html

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
但是,当我运行此代码时,我总是得到以下响应:

结果

executing request POST http://192.168.0.14/upload.php HTTP/1.1
ERROR: Could not upload the image
HTTP/1.1 200 OK
Sorry, file already exists.Sorry, only JPG, JPEG, PNG & GIF files are         allowed.Sorry, your file was not uploaded.

我怀疑这是因为数据没有被传输,因为它总是通过大小测试,可能是0字节。有人能帮我解决这个问题吗?提前谢谢你

好吧,我解决了我的问题。我发送了一个没有多端口的请求,因此php文件无法读取该文件

这是我的新代码

  private static final String UPLOAD_URL = SERVER_IP + "/upload.php";

    public int uploadImageToServer(String fileLocation) throws IOException {

        // the URL where the file will be posted
        String postReceiverUrl = "http://192.168.0.14/upload.php";

        // new HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        //Create File
        File file = new File(fileLocation);
        FileBody fileBody = new FileBody(file);

        //Set up HTTP post
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("fileToUpload", fileBody);

        httpPost.setEntity(reqEntity);

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            String responseStr = EntityUtils.toString(resEntity).trim();


            // you can add an if statement here and do other actions based on the response
            System.out.println(responseStr);
            System.out.println(response.getStatusLine());
        }
        return 0;

    }
这条线可能有用
executing request POST http://192.168.0.14/upload.php HTTP/1.1
ERROR: Could not upload the image
HTTP/1.1 200 OK
Sorry, file already exists.Sorry, only JPG, JPEG, PNG & GIF files are         allowed.Sorry, your file was not uploaded.
  private static final String UPLOAD_URL = SERVER_IP + "/upload.php";

    public int uploadImageToServer(String fileLocation) throws IOException {

        // the URL where the file will be posted
        String postReceiverUrl = "http://192.168.0.14/upload.php";

        // new HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        //Create File
        File file = new File(fileLocation);
        FileBody fileBody = new FileBody(file);

        //Set up HTTP post
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("fileToUpload", fileBody);

        httpPost.setEntity(reqEntity);

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            String responseStr = EntityUtils.toString(resEntity).trim();


            // you can add an if statement here and do other actions based on the response
            System.out.println(responseStr);
            System.out.println(response.getStatusLine());
        }
        return 0;

    }