Post to PHP script from Java Applet

Discussion in 'Java' started by Greg, Apr 6, 2011.

  1. Greg

    Greg Guest

    Everyone,

    I have been tirelessly working on an applet that uploads pictures to
    an apache server and subsequently writes them to a database. I have
    an html equivalent that uploads them and it works fine. However, when
    I try to create a POST from my Java Applet, the PHP doesn't seem to
    react. The pictures never get posted to the database. Any help would
    be GREATLY appreciated. Thanks to everyone in advance!

    JAVA Method to send:

    /**
    * This method uploads files to the server.
    * @param url - The url to upload the files to.
    * @param files - The list of pictures to upload.
    * @throws Exception - Thrown if anything goes wrong.
    */
    public void upload(URL url, Map<BufferedImage, String> files) throws
    Exception
    {
    for (BufferedImage imageSub : files.keySet()) {
    HttpURLConnection connection = null;
    try {
    //Create connection
    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "image/jpeg");
    connection.setRequestProperty("userfile", files.get(imageSub));


    connection.setUseCaches (false);
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //Send request
    DataOutputStream wr = new DataOutputStream (
    connection.getOutputStream ());

    ImageIO.write(imageSub, "jpeg", wr);
    wr.flush ();
    wr.close ();

    //Get Response
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while((line = rd.readLine()) != null) {
    response.append(line);
    response.append('\r');
    }
    rd.close();

    System.out.println(response);
    System.out.println(connection.getResponseCode());
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println("500");

    } finally {

    if(connection != null) {
    connection.disconnect();
    }
    }
    }
    }

    PHP Page getting posted to:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

    "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <title>

    </title>

    <link rel="stylesheet" type="text/css" href="/assets/css/common/
    RootStyle.css">

    <!--[if IE 6]>

    <link rel="stylesheet" type="text/css" href="/assets/css/common/
    IE6Style.css">

    <![endif]-->

    <script type="text/javascript" src="/assets/js/common/
    common_functions.js"></script>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/
    libs/jquery/1.5.0/jquery.min.js"></script>

    <script type="text/javascript" src="/assets/js/lib/jquery-
    jtemplates.js"></script>



    <!-- Beginning of required code for drop down menu -->

    <link rel="stylesheet" type="text/css" href="/assets/css/
    centeredmenu/modernstyle.css" />

    <script type="text/javascript" src="/assets/js/common/chrome.js">

    /***********************************************

    * Chrome CSS Drop Down Menu- (c) Dynamic Drive DHTML code library
    (www.dynamicdrive.com)

    * This notice MUST stay intact for legal use

    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full
    source code

    ***********************************************/

    </script>

    <!-- / END code needed for dropdown-->



    </head>



    <body>

    <div id=container>

    <div id="logo">

    <div id="userInfoBarContainer">

    <div id="rightSpacer">&nbsp;</div>

    <div id="userInfoBar">

    Welcome, please sign in. </div>

    <div id="leftSpacer">&nbsp;</div>

    </div>

    </div>



    <form method="post" enctype="multipart/form-data" action="http://
    localhost/index.php/public/uploadPic/getInfo" name="uploadForm">

    <table width="350" border="0" cellpadding="1" cellspacing="1"
    class="box">

    <tr>

    <td width="246">

    <input type="hidden" name="MAX_FILE_SIZE" value="8000000">

    <input name="userfile" type="file" id="userfile">

    </td>

    <td width="80"><input name="upload" type="submit" class="box"
    id="upload" value=" Upload "></td>

    </tr>

    </table>

    </form>

    <div class="clearfooter"></div>



    </div><!-- end container div -->



    <div id="footer">

    <p>Powered by NameofTool</p>

    </div>



    </body>

    </html>
     
    Greg, Apr 6, 2011
    #1
    1. Advertising

  2. Greg

    Roedy Green Guest

    On Wed, 6 Apr 2011 08:17:33 -0700 (PDT), Greg <>
    wrote, quoted or indirectly quoted someone who said :

    >I have been tirelessly working on an applet that uploads pictures to
    >an apache server and subsequently writes them to a database. I have
    >an html equivalent that uploads them and it works fine. However, when
    >I try to create a POST from my Java Applet, the PHP doesn't seem to
    >react. The pictures never get posted to the database. Any help would
    >be GREATLY appreciated. Thanks to everyone in advance!


    First you need wireshark. See
    http://mindprod.com/jgloss/wireshark.html

    This will let you watch the HTTP messages going back and forth.

    You can use my POST code. See http://mindprod.com/products1.html#POST

    That will handle much of the busywork for you and make sure your
    headers are correct.

    Do you have some other program that can post to your server? If so,
    watch what it does with wireshark then make sure your Applet is doing
    the same thing.

    Your Applet probably must be signed. See
    http://mindprod.com/jgloss/signedapplet.html


    Get it working as an application first,then flip to an Applet. See
    http://mindprod.com/jgloss/applet.html for how to write a hybrid that
    will work either way.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Doing what the user expects with respect to navigation is absurdly important for user satisfaction.
    ~ anonymous Google Android developer
     
    Roedy Green, Apr 6, 2011
    #2
    1. Advertising

  3. Greg

    Luuk Guest

    On 06-04-2011 17:17, Greg wrote:

    You cannot post to PHP script from Java Applet

    Its only possible to POST to a webserver, and the webserver will decide
    how this is processed (through PHP, or not)

    > PHP Page getting posted to:


    i do see a HTML page below this line i quooted

    --
    Luuk
     
    Luuk, Apr 6, 2011
    #3
  4. Greg

    Greg Guest

    On Apr 6, 11:39 am, Luuk <> wrote:
    > On 06-04-2011 17:17, Greg wrote:
    >
    > You cannot post to PHP script from  Java Applet
    >
    > Its only possible to POST to a webserver, and the webserver will decide
    > how this is processed (through PHP, or not)
    >
    > > PHP Page getting posted to:

    >
    > i do see a HTML page below this line i quooted
    >
    > --
    > Luuk


    Luuk,

    I am not quite sure that I understand what you are saying. Right now
    that URL being passed in is a php script at the end. I am posting to
    the server and telling the server to use that php script in the same
    way that a web page does I believe. If not, please correct me.

    Roedy,

    I do have wireshark and could use that. The only other thong that I
    have to do a post is my web browser. The page does work if it isn't
    using the applet. The php script writes HTML and also posts to
    another php script. Would I be able to just be able to see what the
    web page is sending?

    Thanks everyone for this help!

    Greg
     
    Greg, Apr 6, 2011
    #4
  5. Greg

    Greg Guest

    On Apr 6, 2:01 pm, Greg <> wrote:
    > On Apr 6, 11:39 am, Luuk <> wrote:
    >
    > > On 06-04-2011 17:17, Greg wrote:

    >
    > > You cannot post to PHP script from  Java Applet

    >
    > > Its only possible to POST to a webserver, and the webserver will decide
    > > how this is processed (through PHP, or not)

    >
    > > > PHP Page getting posted to:

    >
    > > i do see a HTML page below this line i quooted

    >
    > > --
    > > Luuk

    >
    > Luuk,
    >
    > I am not quite sure that I understand what you are saying.  Right now
    > that URL being passed in is a php script at the end.  I am posting to
    > the server and telling the server to use that php script in the same
    > way that a web page does I believe.  If not, please correct me.
    >
    > Roedy,
    >
    > I do have wireshark and could use that.  The only other thong that I
    > have to do a post is my web browser.  The page does work if it isn't
    > using the applet.  The php script writes HTML and also posts to
    > another php script.  Would I be able to just be able to see what the
    > web page is sending?
    >
    > Thanks everyone for this help!
    >
    > Greg


    Never mind. I got it. I was address the PHP that was displaying the
    page, not the one doing the server work. Thanks everyone!
     
    Greg, Apr 7, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorch
    Replies:
    2
    Views:
    1,260
    Jorch
    May 18, 2004
  2. Grzegorz Wrazen

    Applet and connection to php script

    Grzegorz Wrazen, Jul 22, 2004, in forum: Java
    Replies:
    4
    Views:
    1,497
    Grzegorz Wrazen
    Jul 22, 2004
  3. Krista
    Replies:
    3
    Views:
    2,321
    Andrew Thompson
    Sep 15, 2004
  4. Roland Poellinger
    Replies:
    1
    Views:
    1,301
    Ryan Dillon
    Mar 12, 2005
  5. Rajive Narain
    Replies:
    0
    Views:
    1,885
    Rajive Narain
    Sep 18, 2009
Loading...

Share This Page