The Wayback Machine - https://web.archive.org/web/20111020073510/http://www.codeguru.com:80/forum/showthread.php?threadid=425770
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com eBook Library


Newest Articles at CodeGuru.com:

  • Guide to Implement the Factory Pattern in C#
  • Creating Your Own Search Engine for C/C++ Code Samples
  • Adventures with _chkstk
  • Multi-targeting your Windows Phone Application to Run on 7.0 and 7.5 (Mango)





  • Go Back   CodeGuru Forums > Java Programming > Java Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Java Programming Ask your Java programming question and help out others with theirs.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old June 10th, 2007, 12:09 PM
    Member
     
    Join Date: Mar 2004
    Posts: 339
    dummyagain is an unknown quantity at this point (<10)
    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){
                
            }
            
        }
    However, i dont know if it has sentd the form data to the php page as it seems not opened after the method is triggered.

    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.
    Reply With Quote
      #2    
    Old June 10th, 2007, 03:49 PM
    petes1234's Avatar
    Member +
     
    Join Date: Sep 2006
    Location: Eastern, NC, USA
    Posts: 907
    petes1234 has a spectacular aura about (150+)petes1234 has a spectacular aura about (150+)
    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?
    Reply With Quote
      #3    
    Old June 10th, 2007, 04:28 PM
    Elite Member
    Power Poster
     
    Join Date: Aug 1999
    Location: UK
    Posts: 10,161
    dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)
    Re: Writing an URL Connection

    Quote:
    Originally Posted by petes1234
    ... 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
    You're right about doubling backslashes, but those are forwardslashes...

    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.
    Reply With Quote
      #4    
    Old June 10th, 2007, 10:53 PM
    Member
     
    Join Date: Mar 2004
    Posts: 339
    dummyagain is an unknown quantity at this point (<10)
    Re: Writing an URL Connection

    i tried, but still cannot open the processFile.php page.

    Thx
    Reply With Quote
      #5    
    Old June 11th, 2007, 12:00 AM
    petes1234's Avatar
    Member +
     
    Join Date: Sep 2006
    Location: Eastern, NC, USA
    Posts: 907
    petes1234 has a spectacular aura about (150+)petes1234 has a spectacular aura about (150+)
    Re: Writing an URL Connection

    Quote:
    Originally Posted by dlorde
    You're right about doubling backslashes, but those are forwardslashes...

    Opposites are not contradictory but complementary...
    N. Bohr
    Thanks for the correction. I kind of, sort of, know but only iffily that windows tends to use the ?back slash "\" that unix uses the other and that java can use both but prefers??? the forward slash "/"??????

    I'm really going out on a mental limb here. somebody bring a net and ladder!!!
    Reply With Quote
      #6    
    Old June 12th, 2007, 04:21 AM
    Member
     
    Join Date: Mar 2004
    Posts: 339
    dummyagain is an unknown quantity at this point (<10)
    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());
    		}
        }
    but i dont know what to do next in order to show the processFile.php and show the file info.

    Thank you.
    Reply With Quote
      #7    
    Old June 12th, 2007, 04:47 AM
    Elite Member
    Power Poster
     
    Join Date: Aug 1999
    Location: UK
    Posts: 10,161
    dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)dlorde is a glorious beacon of light (400+)
    Re: Writing an URL Connection

    Quote:
    Originally Posted by petes1234
    Thanks for the correction. I kind of, sort of, know but only iffily that windows tends to use the ?back slash "\" that unix uses the other and that java can use both but prefers??? the forward slash "/"??????
    As I understand it, Java has no preference, but it uses backslashes '\' as escape characters in strings, so if you use them you have to escape them (double them up). Windows defaults to using backslashes, but will accept forwardslashes. Unix uses forwardslashes.

    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.
    Reply With Quote
      #8    
    Old October 18th, 2011, 09:42 PM
    Junior Member
     
    Join Date: Oct 2011
    Posts: 1
    11mict16@nirmauni.ac.in is an unknown quantity at this point (<10)
    Smile Re: Writing an URL Connection

    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";
    }
    ?>
    Reply With Quote
      #9    
    Old Yesterday, 04:01 AM
    Elite Member
     
    Join Date: May 2006
    Location: UK
    Posts: 3,511
    keang is a jewel in the rough (200+)keang is a jewel in the rough (200+)keang is a jewel in the rough (200+)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Java Programming > Java Programming


    Thread Tools Search this Thread
    Display Modes 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


    All times are GMT -5. The time now is 02:35 AM.