Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Post to PHP script from Java Applet

Reply
Thread Tools

Post to PHP script from Java Applet

 
 
Greg
Guest
Posts: n/a
 
      04-06-2011
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>
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-06-2011
On Wed, 6 Apr 2011 08:17:33 -0700 (PDT), Greg <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
 
Luuk
Guest
Posts: n/a
 
      04-06-2011
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
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      04-06-2011
On Apr 6, 11:39*am, Luuk <(E-Mail Removed)> 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
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      04-07-2011
On Apr 6, 2:01*pm, Greg <(E-Mail Removed)> wrote:
> On Apr 6, 11:39*am, Luuk <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
[Applet] Image-Upload to Server via Applet+PHP Roland Poellinger Java 1 03-12-2005 07:23 AM
Java applet failed when I try to load the avi file in my java applet Krista Java 3 09-15-2004 02:53 AM
Applet and connection to php script Grzegorz Wrazen Java 4 07-22-2004 01:17 PM
Howto upload file w/ applet and php/cgi script Jorch Java 2 05-18-2004 06:04 PM



Advertisments