CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com | eBook Library |
|
Java Programming Ask your Java programming question and help out others with theirs. |
![]() |
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
#1
|
|||
|
|||
Writing an URL Connection
I would like to send form data from java to a php page. I have referred to the page http://www.devx.com/Java/Article/17679/1954?pf=true and use its class file provided (http://www.devx.com/assets/sourcecode/7315.zip).
I have written something like this Code:
private void upload(){ try{ InputStream serverInput = ClientHttpRequest.post( new java.net.URL("http://domain.com/processFile.php"), new Object[] {"test.txt", new File("C:\\home\\vp\\tmp\\test.txt") }); ClientHttpRequest cr = new ClientHttpRequest(new URL("http://domain.com/processFile.php")); cr.setParameter("abc", "aaa"); }catch (Exception e){ } } I would like to know how to make something like, when i triggered the "upload" method, the java program will open the processFile.php and send form data to it. Thank you. |
#2
|
||||
|
||||
Re: Writing an URL Connection
I am quite ignorant about URL and using Java to interact w/ the web, so this may be a red herring, but when you use path strings, I think that you need to double up on your backslashes. Thus this:
"http://domain.com/processFile.php" becomes this: "http:////domain.com//processFile.php" etc. Would making this change help any? |
#3
|
|||
|
|||
Re: Writing an URL Connection
Quote:
Opposites are not contradictory but complementary... N. Bohr
__________________
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present. |
#4
|
|||
|
|||
Re: Writing an URL Connection
i tried, but still cannot open the processFile.php page.
Thx |
#5
|
||||
|
||||
Re: Writing an URL Connection
Quote:
I'm really going out on a mental limb here. somebody bring a net and ladder!!! |
#6
|
|||
|
|||
Re: Writing an URL Connection
i have written something like
Code:
private void upload(){ DataOutputStream dataOut; URLConnection urlConn; URL url; String urlString = "http://domain.com/processFile.php"; String filename = "C:/us.jpg"; String localFilename = filename; try { url = new URL(urlString); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestProperty("Content-Type", "multipart/form-data, boundary=AaB03x"); dataOut = new DataOutputStream(urlConn.getOutputStream()); dataOut.writeBytes("--AaB03x\r\n"); dataOut.writeBytes("Content-Disposition: form-data; name=\"data_file\"; filename=\"" + URLEncoder.encode( filename) + "\"\r\n"); dataOut.writeBytes("Content-Type: image/jpeg\r\n"); dataOut.writeBytes("\r\n"); FileInputStream fis = new FileInputStream(localFilename); System.out.println("output from " + localFilename + " to " + filename); byte buffer[] = new byte[1000000]; int len; while ((len = fis.read(buffer)) != -1) { dataOut.write(buffer, 0, len); } fis.close(); dataOut.close(); dataOut.writeBytes("\r\n--AaB03x--\r\n"); dataOut.flush(); dataOut.close(); status.setText("waiting for server"); InputStream in = urlConn.getInputStream(); int input = 0; String inputSt = ""; while (input != -1) { input = in.read(); inputSt = inputSt + ((char) input); } status.setText("from Server" + inputSt); } catch (MalformedURLException me) { System.err.println("MalformedURLException: " + me); } catch (IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); } } Thank you. |
#7
|
|||
|
|||
Re: Writing an URL Connection
Quote:
Computer Science is no more about computers than astronomy is about telescopes... E. Dijkstra
__________________
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present. |
#8
|
|||
|
|||
![]()
Here is The PHP code That Required For file upload response.
<?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> |
#9
|
|||
|
|||
Re: Writing an URL Connection
Please don't resurrect old threads and especially as a PHP solution is off topic for this forum.
__________________
Posting code? Use code tags like this: [code]...Your code here...[/code] Click here for examples of Java Code |
![]() |
Bookmarks |
|
Thread Tools | Search this Thread |
Display Modes | Rate This Thread |
|
|