Post to PHP script from Java Applet
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"> </div>
<div id="userInfoBar">
Welcome, please sign in. </div>
<div id="leftSpacer"> </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>