Android - Programmatically upload file to .NET server, with metadata -
i know question seems bit specific, have thought quite common scenario. goal here upload image (~8 mb) server. server runs .net (no php believe), not need upload file, need pass text information along it. how this? client , server code like?
i used .net
webservice upload database
file. can use image
upload.
download demo.
add these files in package.
1. outputstream.java
package com.abhan.example; public class outputstream extends java.io.filteroutputstream { public final static int encode = 1; public final static int do_break_lines = 8; private final static int max_line_length = 76; private final static byte new_line = (byte)'\n'; private final static byte white_space_enc = -5; private boolean encode; private int position; private byte[] buffer; private int bufferlength; private int linelength; private boolean breaklines; private byte[] b4; private boolean suspendencoding; private int options; private byte[] decodabet; public outputstream( java.io.outputstream out ) { this( out, encode ); } public outputstream( java.io.outputstream out, int options ) { super( out ); this.breaklines = (options & do_break_lines) != 0; this.encode = (options & encode) != 0; this.bufferlength = encode ? 3 : 4; this.buffer = new byte[ bufferlength ]; this.position = 0; this.linelength = 0; this.suspendencoding = false; this.b4 = new byte[4]; this.options = options; this.decodabet = base64.getdecodabet(options); } @override public void write(int thebyte) throws java.io.ioexception { if( suspendencoding ) { this.out.write( thebyte ); return; } if( encode ) { buffer[ position++ ] = (byte)thebyte; if( position >= bufferlength ) { this.out.write( base64.encode3to4( b4, buffer, bufferlength, options ) ); linelength += 4; if( breaklines && linelength >= max_line_length ) { this.out.write( new_line ); linelength = 0; } position = 0; } } else { if( decodabet[ thebyte & 0x7f ] > white_space_enc ) { buffer[ position++ ] = (byte)thebyte; if( position >= bufferlength ) { int len = base64.decode4to3( buffer, 0, b4, 0, options ); out.write( b4, 0, len ); position = 0; } } else if( decodabet[ thebyte & 0x7f ] != white_space_enc ) { throw new java.io.ioexception( "invalid character in base64 data." ); } } } @override public void write( byte[] thebytes, int off, int len ) throws java.io.ioexception { if( suspendencoding ) { this.out.write( thebytes, off, len ); return; } for( int = 0; < len; i++ ) { write( thebytes[ off + ] ); } } public void flushbase64() throws java.io.ioexception { if( position > 0 ) { if( encode ) { out.write( base64.encode3to4( b4, buffer, position, options ) ); position = 0; } else { throw new java.io.ioexception( "base64 input not padded." ); } } } @override public void close() throws java.io.ioexception { flushbase64(); super.close(); buffer = null; out = null; } public void suspendencoding() throws java.io.ioexception { flushbase64(); this.suspendencoding = true; } public void resumeencoding() { this.suspendencoding = false; } }
2. utils.java
package com.abhan.example; import java.io.bufferedinputstream; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.text.dateformat; import java.text.simpledateformat; import java.util.calendar; import java.util.gregoriancalendar; import android.os.environment; public class utils { public static boolean checkexternalstorageavailable() { boolean mexternalstorageavailable = false; string state = environment.getexternalstoragestate(); if(state.equals(environment.media_mounted)) { mexternalstorageavailable = true; } else if(state.equals(environment.media_mounted_read_only)) { mexternalstorageavailable = false; } else if(state.equals(environment.media_nofs)) { mexternalstorageavailable = false; } else if(state.equals(environment.media_shared)) { mexternalstorageavailable = false; } else if(state.equals(environment.media_unmountable)) { mexternalstorageavailable = false; } else if(state.equals(environment.media_unmounted)) { mexternalstorageavailable = false; } else if(state.equals(environment.media_removed)) { mexternalstorageavailable = false; } return mexternalstorageavailable; } public static string datadirectory() { string datadir = null; if(checkexternalstorageavailable()) { datadir = environment.getdatadirectory().getabsolutepath(); } return datadir; } public static boolean createdirectory(final string dirname) { boolean isdirectorycreated = false; if(checkexternalstorageavailable()) { file dirfile = new file(dirname); if(!dirfile.exists()) { dirfile.mkdirs(); isdirectorycreated = true; } } return isdirectorycreated; } public static boolean deletedirectory(final string dirname) { boolean isdirectorydeleted = false; if(checkexternalstorageavailable()) { file dirfile = new file(dirname); if(!dirfile.exists()) { isdirectorydeleted = false; } else { dirfile.delete(); isdirectorydeleted = true; } } return isdirectorydeleted; } public static string todaydate(final string providedformat) { string returneddatestring = null; try { calendar calendar = new gregoriancalendar(); dateformat dateformat = new simpledateformat(providedformat); returneddatestring = dateformat.format(calendar.gettime()); } catch (exception e) { e.printstacktrace(); } return returneddatestring; } public static byte[] convertdatabasefiletoarray(final string filepath) { inputstream inputstream = null; bytearrayoutputstream byteoutputstream = null; try { inputstream = new bufferedinputstream( new fileinputstream(new file(filepath)), 8192); byte[] buffer = new byte[8096]; byteoutputstream = new bytearrayoutputstream(); int length = 0; while((length = inputstream.read(buffer)) != -1) { byteoutputstream.write(buffer, 0, length); } if(inputstream != null) { inputstream.close(); } if(byteoutputstream != null) { byteoutputstream.flush(); byteoutputstream.close(); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return byteoutputstream.tobytearray(); } }
3. base64.java
to download file can use grepcode search.
encode bytes / string according needs in web service using base64
, pass param.
in mainactivity.java
change following things.
final string filepath = utils.datadirectory() + file.separator + "data" + file.separator + mainactivity.this.getpackagename() + file.separator + "databases" + file.separator + "my_db"; final string todaydate = utils.todaydate("your_preferred_format"); final string param0 = "http://121.121.121.121/mwebservice/abhan.asmx"; final string param1 = "121.121.121.121"; final string param2 = "http://localhost/mwebservice/uploadfile"; final string param3 = gettopofheader() + "<uploadfile xmlns=\"http://localhost/mwebservice\">" + "<filearray>" + base64.encodebytes(utils.convertdatabasefiletoarray(filepath)) + "</filearray>" + "<filename>" + todaydate + "_1234567890_my_db.db" + "</filename>" + "<number>1234567890</number>" + "</uploadfile>" + getbottomofheader();
in demo have used local service
. please change live
or local
web service. moreover, used created database
file in \data\data\databases\my_db
. can remove unnecessary code.
run demo.
thanks.
Comments
Post a Comment