public String put ( File file ) throws IOException { if ( ! connection . isOpen ( ) ) connection . open ( ) ; connection . prepare ( "PUT" ) ; String contentType = connection . getHeader ( "Content-Type" ) ; if ( contentType == null ) contentType = connection . getHeader ( "content-type" ) ; if ( contentType == null || contentType . isEmpty ( ) ) { /** * Default it to application/octet-stream if nothing has been specified */ connection . setHeader ( "Content-Type" , "application/octet-stream" ) ; } OutputStream outStream = connection . getOutputStream ( ) ; InputStream inStream = new FileInputStream ( file ) ; byte [ ] buf = new byte [ 1024 ] ; int len = 0 ; while ( len != - 1 ) { len = inStream . read ( buf ) ; if ( len > 0 ) outStream . write ( buf , 0 , len ) ; } inStream . close ( ) ; outStream . close ( ) ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( connection . getConnection ( ) . getInputStream ( ) ) ) ; StringBuffer responseBuffer = new StringBuffer ( ) ; String line = null ; while ( ( line = reader . readLine ( ) ) != null ) { responseBuffer . append ( line ) . append ( ' ' ) ; } reader . close ( ) ; connection . getConnection ( ) . disconnect ( ) ; response = new HttpResponse ( responseBuffer . toString ( ) . getBytes ( ) ) ; response . setCode ( connection . getConnection ( ) . getResponseCode ( ) ) ; if ( response . getCode ( ) < 200 || response . getCode ( ) >= 300 ) { response . setMessage ( connection . getConnection ( ) . getResponseMessage ( ) ) ; throw new IOException ( "Error uploading file: " + response . getCode ( ) + " -- " + response . getMessage ( ) ) ; } return getResponse ( ) ; }
Upload a text file to the destination URL