Post to PHP script from Java Applet

G

Greg

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>
 
R

Roedy Green

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.
 
L

Luuk

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
 
G

Greg

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)


i do see a HTML page below this line i quooted

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
 
G

Greg

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top