public void addFilePart ( String fieldName , File uploadFile , String fileName ) throws IOException { if ( TextUtils . isEmpty ( fileName ) ) { fileName = uploadFile . getName ( ) ; } mWriter . append ( "--" + mBoundary ) . append ( LINE_FEED ) ; mWriter . append ( "Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"" ) . append ( LINE_FEED ) ; mWriter . append ( "Content-Type: " + URLConnection . guessContentTypeFromName ( fileName ) ) . append ( LINE_FEED ) ; mWriter . append ( "Content-Transfer-Encoding: binary" ) . append ( LINE_FEED ) ; mWriter . append ( LINE_FEED ) ; mWriter . flush ( ) ; FileInputStream inputStream = new FileInputStream ( uploadFile ) ; byte [ ] buffer = new byte [ 4096 ] ; int bytesRead = - 1 ; while ( ( bytesRead = inputStream . read ( buffer ) ) != - 1 ) { mOutputStream . write ( buffer , 0 , bytesRead ) ; } mOutputStream . flush ( ) ; inputStream . close ( ) ; mWriter . append ( LINE_FEED ) ; mWriter . flush ( ) ; }
Adds a upload file section to the request