Results 1 to 8 of 8
  1. #1
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Sending a file to a web page using POST method

    Hi Advanced users,

    I have an application and I would like it to send a file to a web page using POST method.
    I've found the HttpClient from Apache, but I had no luck. All the tutorials and samples I can find on the net are for 3.x. I've tried adopting the samples with the HttpClient 4.1 docs, but I failed.

    I have a target url of a page and source file path. I get the basic idea of what needs to happen, I am just finding it hard to implement it.

    Any help would be appreciated, Thanks.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Some code might help...along with what you mean by "I had no luck".

  3. #3
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Java Code:
    File file = new File("target file location");
                    String targetURL = "URL to a .php web page";
    
                    HttpClient httpclient = new DefaultHttpClient();
                try {
                HttpPost httppost = new HttpPost(targetURL);
    
                InputStreamEntity reqEntity = new InputStreamEntity(
                        new FileInputStream(file), -1);
                reqEntity.setContentType("binary/octet-stream");
                reqEntity.setChunked(true);
                // It may be more appropriate to use FileEntity class in this particular
                // instance but we are using a more generic InputStreamEntity to demonstrate
                // the capability to stream out data from any arbitrary source
                //
                // FileEntity entity = new FileEntity(file, "binary/octet-stream");
    
                httppost.setEntity(reqEntity);
    
                System.out.println("executing request " + httppost.getRequestLine());
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();
    
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                    System.out.println("Chunked?: " + resEntity.isChunked());
                }
                EntityUtils.consume(resEntity);
            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpclient.getConnectionManager().shutdown();
            }
    I keep getting 406 Not Acceptable Response.

    EDIT: This is essentially one of the examples from http://hc.apache.org/httpcomponents-.../examples.html
    Last edited by FlyNn; 06-22-2011 at 03:33 PM.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Which means the server is trying to return something the client has told it it can't accept.
    What data is the server trying to return?

  5. #5
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    That's a very good questions cause I did not write the .php page on the server side. I do know it processes at the file and echos the contents out.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    You should be able to find out what the Accept: header is for your post, though I couldn't tell you exactly how.
    Then you'll need to find a way of seeing what the server is planning on sending.
    If it's echoing your file back then I suspect it may be whatever MIME type that would fall under.

  7. #7
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    I have changed the code like this:
    Java Code:
    HttpClient httpclient = new DefaultHttpClient();
                    
                    try {
                        
                        HttpPost httppost = new HttpPost(targetURL);
    
                        FileEntity fe = new FileEntity(file,"text/xml; charset=\"UTF-8\"");
    
                        httppost.setEntity(fe);
    
                        System.out.println("executing request " + httppost.getRequestLine());
                        HttpResponse response = httpclient.execute(httppost);
    
                        HttpEntity resEntity = response.getEntity();
    
                        System.out.println("----------------------------------------");
                        System.out.println(response.getStatusLine());
    
                            if (resEntity != null) {
                                System.out.println("Response content length: " + resEntity.getContentLength());
                                System.out.println("Chunked?: " + resEntity.isChunked());
                            }
    
                        EntityUtils.consume(resEntity);
                        
                    }catch(Exception e){
                        e.printStackTrace();
                    } finally {
                        httpclient.getConnectionManager().shutdown();
                    }
    I have added the fileEntity and setting it for the httppost and correctly executing it.
    I get 200 OK response.

    Thank you
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  8. #8
    GSharan is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default Re: Sending a file to a web page using POST method

    Me too getting 200 Ok response & the resEntity = response.getEntity();

    But I'm not able to see my uploaded file on the server please correct me.


    import java.io.File;
    import org.apache.http.HttpEntity;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.FileEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.HttpResponse;


    public class UploadFile1 {
    public static void main(String[] args)
    {

    File file = new File("C:\\....\\test.jpg");

    String targetURL = "http://serverurl...";
    HttpClient httpclient = new DefaultHttpClient();
    try {

    HttpPost httppost = new HttpPost(targetURL);

    FileEntity fe = new FileEntity(file,"binary/octet-stream");

    httppost.setEntity(fe);

    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);

    HttpEntity resEntity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());

    if (resEntity != null) {
    System.out.println("Response content length: " + resEntity.getContentLength());
    System.out.println("Chunked?: " + resEntity.isChunked());
    }
    System.out.println(EntityUtils.toString(resEntity) );
    //EntityUtils.consume(resEntity);

    }catch(Exception e){
    e.printStackTrace();
    } finally {
    httpclient.getConnectionManager().shutdown();
    }
    }

    }

Similar Threads

  1. Java applet post get method to php
    By lotdox in forum Java Applets
    Replies: 0
    Last Post: 01-07-2011, 12:17 AM
  2. Replies: 10
    Last Post: 08-05-2010, 05:27 PM
  3. Sending text to a web page
    By shyameni in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2009, 07:14 AM
  4. Sending text to a web page
    By shyameni in forum Advanced Java
    Replies: 0
    Last Post: 10-07-2009, 06:38 PM
  5. Post Method in java.net
    By freddieMaize in forum Advanced Java
    Replies: 2
    Last Post: 02-23-2009, 02:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •