0

i want to upload file with send http request to php . i test php sides on server over and over , but when i send request with this code

 private static void upload() {
    File targetFile = new File("test.txt");
    PostMethod filePost = new PostMethod("http://localhost/chat_upload/service/index.php/upload/do_upload");
    try {
        Part[] parts = {
                new FilePart(targetFile.getName(), targetFile),
                new StringPart("name", "userfile")
        };
        filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
        org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
       // client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
        int status = client.executeMethod(filePost);
        if (status == HttpStatus.SC_OK) {
            System.out.println("Upload complete, response=" + filePost.getResponseBodyAsString());
        } else {
            System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));

        }
    } catch (Exception ex) {
        System.out.println(ex.fillInStackTrace());
    } finally {
        filePost.releaseConnection();
    }
}

this method send file and print on console . when i run , server say : file not selected .

{i write php side , when file not selected , this error print}

how to fix this?

1 Answer 1

1

The do_upload handler is probably looking for the file under the name file, but you're not adding a part by that name.

You need the 3 parameter constructor to set both the name and the filename:

new FilePart("file", targetFile.getName(), targetFile)

Or was the other part the attempt to set the part name? If so, then:

Part[] parts = {
    new FilePart("userfile", targetFile.getName(), targetFile)
};
Sign up to request clarification or add additional context in comments.

Comments

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.