How to upload file from java client to php

hi

i am trying to upload/send a file from client using swing/applet

and receiving it with php code.

here is the php code which uploads the post file from the client

$uploaddir = "/home/raghavendra/Documents/";

$file = basename( $_FILES["uploadedfile"]["name"]);

echo "file:\n".$file;

$uploadfile = $uploaddir. $file;

if (move_uploaded_file($_FILES["uploadedfile"]["tmp_name"],$uploadfile)) {

echo "File is valid, and was successfully uploaded.\n";

}

else {

echo "File upload failure Possible file upload attack!\n";

}

and corresponding different java code which post the

1)

public void postmethodTest(String filefrom){

try{

String hostname = "localhost";

int port = 80;

InetAddress addr = InetAddress.getByName(hostname);

Socket socket = new Socket(addr, port);

// Send header

String path ="/php_prgs/var/www/nsboxng/htdocs/tryupdate.php";

File theFile = new File(filefrom);

System.out.println ("size: " + (int) theFile.length());

DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(theFile)));

byte[] theData = new byte[(int) theFile.length( )];

fis.readFully(theData);

fis.close();

DataOutputStream raw = new DataOutputStream(socket.getOutputStream());

Writer wr = new OutputStreamWriter(raw);

String command =

"POST "+path+" HTTP/1.0\r\n"

+ "Content-type: multipart/form-data, boundary=mango\r\n"

+ "Content-length: " + ((int) theFile.length()) + "\r\n"

+ "\r\n"

+ "--mango\r\n"

+ "content-disposition: name=\"MAX_FILE_SIZE\"\r\n"

+ "\r\n"

+ "\r\n--mango\r\n"

+ "content-disposition: attachment; name=\"datafile\"" ;

String filename="test.doc\"\r\n"

+ "Content-Type: text/doc\r\n"

+ "Content-Transfer-Encoding: binary\r\n"

+ "\r\n";

wr.write(command);

wr.flush();

raw.write(theData);

raw.flush( );

wr.write("\r\n--mango--\r\n");

wr.flush( );

BufferedReader rd = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

String line;

while ((line = rd.readLine()) != null) {

System.out.println("out"+line);

}

wr.close();

raw.close();

socket.close();

} catch (Exception e) {System.out.println(e.toString());}

}

2)

public void postMethod(String strURL, String filefrom){

try {

String fname = filefrom.substring(filefrom.lastIndexOf("/")+1, filefrom.length());

File input=new File(filefrom);

// Prepare HTTP post

PostMethod post = new PostMethod(strURL);

// Request content will be retrieved directly

// from the input stream

// Per default, the request content needs to be buffered

// in order to determine its length.

// Request body buffering can be avoided when

// content length is explicitly specified

post.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(input), input.length()));

// Specify content type and encoding

// If content encoding is not explicitly specified

// ISO-8859-1 is assumed

//post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");

post.setRequestHeader("Content-Type","multipart/form-data");

post.setRequestHeader("Content-Disposition", "form-data; name="+fname);

// Get HTTP client

HttpClient httpclient = new HttpClient();

// Execute request

try {

int result=httpclient.executeMethod(post);

// Display status code

System.out.println("Response status code: " +result);

// Display response

System.out.println("Response body: ");

// System.out.println(post.getResponseBodyAsString());

BufferedReader console = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));

String name = null;

String line = null;

try {

while ((line = console.readLine()) != null) {

System.out.println("output"+line);

}

//name = console.readLine();

}

catch (IOException e) { name = "<" + e + ">"; }

// System.out.println("Hello " + name);

} finally {

// Release current connection to the connection pool

// once you are done

post.releaseConnection();

}

}

catch(IOException e){

}

}

but am getting else condition response from php code

but if i post with html code it is working fine.

can anybody help me please where i have to change the code

please suggest me. am in a big trouble and i have to complete this as soon as possible

[4730 byte] By [raghavan_laxa] at [2007-11-27 11:40:13]
# 1

One thread is enough.

http://forum.java.sun.com/thread.jspa?threadID=5198449

You could have bumped it instead. Also, you still just posted a junk of unformatted stuff. Furthermore, for HttpClient support ask at an HttpClient mailing list of rorum.

CeciNEstPasUnProgrammeura at 2007-7-29 17:30:39 > top of Java-index,Java Essentials,Java Programming...