1

I have the following uploader method in Java:

public void uploadFile() throws IOException
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://myurl/uploadper.php");
File file = new File("C:/Users/mislam/Desktop/perfimg/0.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();
}

I have the following php file

    <?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['userfile']['name']); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['userfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

I am getting the following output

executing request POST http://reactor.ctre.iastate.edu/uploadper.php 

HTTP/1.1
HTTP/1.1 200 OK
There was an error uploading the file, please try again!

Which indicates that the communication with the Server was successful but it couldn't upload. What can be the reason that PHP is not uploading the file? Is there any problem in java code?

EDIT After Rewriting code for multipart entity I get the file in the server But uploads operation is not working:

HttpEntity httpEntity = MultipartEntityBuilder.create()
    .addBinaryBody("userfile", file, ContentType.create("image/jpeg"), file.getName())
    .build();
    httppost.setEntity(httpEntity);
2
  • 2
    Start with print_r($_FILES['userfile']) Commented Apr 3, 2016 at 19:26
  • Its showing nothing. I wonder why!! Commented Apr 3, 2016 at 19:36

1 Answer 1

1

First I would have a look at the value of $_FILES['userfile']['error']

See this manual page for description of error codes:

http://php.net/manual/en/features.file-upload.errors.php

Sign up to request clarification or add additional context in comments.

4 Comments

Can you get the raw http request? It may be malformed. One thing to check is the content-type header. If it is not correct php wont see the file. It should be: Content-Type: multipart/form-data
Its showing Content-Type: multipart/form-data; boundary=YjiAoTjQY6yMFjEStp8ryIbHHeF_9BPAova4d3Jb; charset=US-ASCII
Using the command System.out.println(mpEntity.getContentType());
So that's from the client but what is the server getting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.