hi all, i am using file upload api for io operation to download files on server using multipart/form-data.
link given below:- http://commons.apache.org/fileupload If we are using normal html file for upload no issue, browser takes care of "boundary"... but if we write core java app to connect to filedownload app, we need to set "boundary" manually... I have set boundary manually,it works.But it does not work without boundary. is it possible to download files without boundary? is there any other api which can do it? if I remove boundary from code it gives me error: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found my html code being used to connect to filedownload app:- ----------------------------- <html> <head> <script language="JavaScript"> function show(){} </script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form AUTOCOMPLETE="off" name="addFiles" method="post" action="https://tpslvl0109:8443/FileDownload/pushFile" enctype="multipart/form-data"> username:<input type=text name=username id=username value="myusername"><br> password:<input type=password name=password id=password value="mypassword"><br> Input file1:<input name="inputfile1" type="file" id="inputfile1" size="55"><br> <input type=submit name=submit value=submit onClick="show()" > </form></body></html> ----------------------------- my core java client app used to connect to same filedownload app --------------------------------- import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class connect implements HostnameVerifier{ public connect(){} public void try_connect(){ try { String boundary = "*****"; System.setProperty( "java.protocol.handler.pkgs" , "javax.net.ssl" ); java.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() ); TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){ public java.security.cert.X509Certificate[] getAcceptedIssuers(){ return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType ) { } public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType ) { } } }; SSLContext sc = SSLContext.getInstance( "SSL" ); sc.init( null, trustAllCerts, new java.security.SecureRandom() ); HttpsURLConnection.setDefaultSSLSocketFactory( sc.getSocketFactory() ); URL url = new URL("https", "tpslvl0109", 8443, "/FileDownload/pushFile"); System.out.println(url.getQuery()); HttpsURLConnection m_oHttpURLConnection = (HttpsURLConnection) url.openConnection(); m_oHttpURLConnection.setDoInput( true ); m_oHttpURLConnection.setDoOutput( true ); m_oHttpURLConnection.setUseCaches( false ); m_oHttpURLConnection.setRequestProperty("Connection", "Keep-Alive"); m_oHttpURLConnection.setRequestProperty( "Content-Type", "multipart/form-data;boundary="+boundary ); m_oHttpURLConnection.setAllowUserInteraction( true ); m_oHttpURLConnection.setRequestMethod("POST"); m_oHttpURLConnection.setInstanceFollowRedirects( true ); ( ( HttpsURLConnection )( m_oHttpURLConnection ) ).setHostnameVerifier( this ); m_oHttpURLConnection.connect(); DataOutputStream dos = null; String exsistingFileName = "D:\\projects\\104-FileDownload\\TestFilefrom.txt"; String lineEnd = "\r\n"; String twoHyphens = "--"; dos = new DataOutputStream( m_oHttpURLConnection.getOutputStream() ); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=username ; " + lineEnd ); dos.writeBytes(lineEnd); dos.writeBytes("myusername"); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=password ; " + lineEnd ); dos.writeBytes(lineEnd); dos.writeBytes("mypassword"); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" + " filename=\"" + exsistingFileName +"\"" + lineEnd ); dos.writeBytes(lineEnd); FileInputStream fileInputStream = new FileInputStream( new File(exsistingFileName) ); int bytesRead, bytesAvailable, bufferSize; byte[] buffer;int maxBufferSize = 1*1024*1024; bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); fileInputStream.close(); dos.flush(); dos.close(); System.out.println("It's Connected"); InputStream is = m_oHttpURLConnection.getInputStream ( ) ; if ( is == null ) { throw new Exception ( "Got a null content object!" ) ; } StringBuffer putBackTogether = new StringBuffer ( ) ; Reader r = new InputStreamReader ( is, "UTF-8" ) ; char [ ] cb = new char [ 2048 ] ; int amtRead = r.read ( cb ) ; while ( amtRead > 0 ) { putBackTogether.append ( cb, 0, amtRead ) ; amtRead = r.read ( cb ) ; } String pageSource = putBackTogether.toString ( ) ; System.out.println(pageSource); } catch (Exception e){ e.printStackTrace(); } } public boolean verify(String hostname, SSLSession session) { return true; } public static void main(String[] args) { try{connect c=new connect(); c.try_connect();}catch(Exception e){ e.printStackTrace(System.out); }} } --------------------------------- Disclaimer: ""Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. TechProcess Solutions Ltd. accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.""
