Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2012, 07:41 AM   PM User | #1
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
upload to php using post

Code:
/*im using the following lib 
 http://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&ved=0CFwQjBAwAg&url=http%3A%2F%2Fhc.apache.org%2Fdownloads.cgi&ei=X3Y0UNabPMeW0QW25YD4BQ&usg=AFQjCNHrmO3F7Uv5EEIOp54nZkIbS4jDdg

response when actionlistener is fired
executing request POST http://dreamwebdesign.comxa.com/jar/upload.php?username=test&password=test OK
<span style='color:red;'> No file recieved </span>*/

btnupload.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					HttpClient httpclient = new DefaultHttpClient();
					httpclient.getParams().setParameter(
							CoreProtocolPNames.PROTOCOL_VERSION,
							HttpVersion.HTTP_1_1);

					HttpPost httppost = new HttpPost(
							"http://dreamwebdesign.comxa.com/jar/upload.php?username=test&password=test");
					File file = new File("c:/test.schematic");

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

					httppost.setEntity(reqEntity);
					reqEntity.setContentType("binary/octet-stream");
					System.out.println("executing request "
							+ httppost.getRequestLine());
					HttpResponse response;

					response = httpclient.execute(httppost);

					HttpEntity resEntity = response.getEntity();

					System.out.println(response.getStatusLine());
					if (resEntity != null)
					{
						System.out.println(EntityUtils
								.toString(resEntity));
					}
					if (resEntity != null)
					{
						resEntity.consumeContent();
					}

					httpclient.getConnectionManager().shutdown();
				}
				catch (Throwable error)
				{
					System.out.println("Http Post Error.");
				}
			}
		});

php code for recieving, anything that is declared as null is just you cant see it but its right in the actual code (stuff like usernames/passwords)
Code:
<?php
function getRealIpAddr() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function connect_db($user, $pass) 
{
	$link = mysql_connect('null', $user, $pass);
	if (!$link) 
	{
		die('error: '. mysql_error());
	}
	mysql_select_db("users", $link);
	return $link;
}

function check_login($link, $username, $password) {
	$query = sprintf("SELECT * FROM table WHERE username='%s'", 
						mysql_real_escape_string($username));
	$result = mysql_query($query, $link);
	if ($result) {
		// check user
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if ($row['password'] == $password) {
			$query = sprintf("UPDATE 'table' SET lastIP='%d' WHERE username='%s'", getRealIpAddr(),mysql_real_escape_string($username));
			mysql_query($query, $link);
		} else {
			echo "error, Incorect password";
		}
	} else {
		// add new user
		$query = sprintf("INSERT INTO table 'table' VALUES \(%s, %s, 0\)",
							mysql_real_escape_string($username), 
							mysql_real_escape_string($password));
		mysql_query($query, $link);
	}
	
}

function update_count_db_upload($link, $username) {
	// retrieve user upload uploads
	$query = sprintf("SELECT 'uploads' FROM table WHERE username='%s'", mysql_real_escape_string($username));
	$result = mysql_query($query, $link);

	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$row['uploads']++;
	
	// update user upload uploads
	$query = sprintf("UPDATE 'table' SET uploads='%d' WHERE username='%s'", $row['uploads'], mysql_real_escape_string($username));
	//$query = sprintf("UPDATE 'table' SET uploads="%d" WHERE username='%s'", $row['count']
	//			mysql_real_escape_string($username));
	mysql_query($query, $link);
}

function save_file($file, $username) {
	$fname = "uploads/schematics/".addslashes($username)."_filename.schematic";
	move_uploaded_file($file['tmp_name'], $fname);
	echo ("done uploading <br />");
}

function list_schematics() {
	if ($handle = opendir('uploads/schematics/')) {
		//echo "Directory handle: $handle\n <br />";
		echo "Entries:\n <br />";

		/* This is the correct way to loop over the directory. */
		while (false !== ($entry = readdir($handle))) {
			if($entry != ".") 
			{
				echo "There are currently no uploads";
			}
			elseif($entry != "..")
			{
				
			}
			else echo "$entry\n <br />";
		}

		closedir($handle);
	}
}
// -------------------------------------------------------------
$user = "null";
$pass = "null";
$link = connect_db($user, $pass);

$username = $_GET['username'];
$password = $_GET['password'];
$file = $_FILES['filename'];
var_dump($_FILES);

if ($username != null && $username != "") {
	check_login($link, $username, $password);
	if ($file != null) {
		save_file($file, $username);
		update_count_db_upload($link, $username);
	} else {
		echo "<span style='color:red;'> Error in file </span>";
	}
} else {
	list_schematics();
}
?>

Last edited by jordandap; 08-22-2012 at 07:43 AM..
jordandap is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:05 AM.


Advertisement
Log in to turn off these ads.