Since you set the Content Type to application/x-www-form-urlencoded you must
URL Encode the data.
    (*Chris*)

----- Original Message -----
From: Phillip Rhodes <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, August 15, 1999 5:45 PM
Subject: Post a file to servlet from application


> Hi.
>
> I am trying very hard to post a binary file and some other parameters to a
> servlet from a java application.  The servlet will look at the parameters
> and the associated file and write it to disk.  I am having problems
getting
> it in the right format.
>
> I can post any number of name=value pairs fine.  However, when I add my
> file byte array after my name=value pairs, it will generate an illegal
> argument exception in my servlet when I call req.getParameter("name").
>  This means that I have the format wrong.
>
> Another question:  When I send the binary data, do I have to encode that
> also?
>
> Here's the Java application code that posts to the servlet.  The servlet
> stuff is pretty simple.
>
> Thanks.  It's close to 1AM and I can't get this fixed!!
> Phillip
>
>     URL url;
>     URLConnection urlConn;
>     DataOutputStream printout;
>     DataInputStream input;
>     // URL of CGI-Bin script.
>     url = new URL ("http://localhost/servlet/FolderServlet");
>
>     // URL connection channel.
>     urlConn = url.openConnection();
>
>     // Let the run-time system (RTS) know that we want input.
>     urlConn.setDoInput (true);
>
>     // Let the RTS know that we want to do output.
>     urlConn.setDoOutput (true);
>
>     // No caching, we want the real thing.
>     urlConn.setUseCaches (false);
>
>     // Specify the content type.
>     urlConn.setRequestProperty ("Content-Type",
> "application/x-www-form-urlencoded");
>
>     // Send POST output.
>     printout = new DataOutputStream (urlConn.getOutputStream ());
>
>     String content = "username=" + URLEncoder.encode ("user") +
> "&password=" + URLEncoder.encode ("pass");
>     content += "&todo=sendfile";
>     printout.writeBytes (content);
>     File zipfile_to_send = new File("file.zip");
>       file://in
>     final int iBufLen = 512;  // default, change as needed
>     int iCount = 0;
>     BufferedInputStream filein = new BufferedInputStream(new
> FileInputStream(zipfile_to_send), iBufLen );
>     byte[] bytes = new byte[ iBufLen ];
>     while( true )
>     {
>       try{
>           iCount = filein.read( bytes, 0, iBufLen );
>         if( iCount == -1 )
>         {
>           break;        // EOS
>         }
>
> file://Should I be encoding the bytes that I am sending???
>
>         printout.write(bytes,0,iCount);
>       }         catch( IOException ioe ){
>         ioe.printStackTrace();
>             break;
>       }
>             } // end while
>     filein.close();
>     printout.flush ();
>     printout.close ();
>     }
>     String inputLine;
>           BufferedReader in = new BufferedReader(new
> InputStreamReader(urlConn.getInputStream()));
>     StringBuffer sb = new StringBuffer();
>           while ((inputLine = in.readLine()) != null)
>     {
>       System.out.println("inputLine=" + inputLine);
>       sb.append(inputLine);
>     }
>           in.close();
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to