Android Upload file

Source: Internet
Author: User
Tags response code

Method One:

 Public voidUploadFile () {String name=urlencoder.encode (Text1.gettext (). toString (),"Utf-8"); String Pass=urlencoder.encode (Text2.gettext (). toString (),"Utf-8"); Map<string, string>params=NewHashmap<string, string>(); params. put ("NAME", name); params. put ("PASSWORD", pass); Post ("Http://192.168.1.120/dev/index.php/Device/UploadFile?filename=111&filetype=IMAGE",params, Upfiles); } Map<string, file> upfiles =NewHashmap<string, file>(); voidgetFile () {File file=NewFile ("/sdcard/"); file[] Files= File.listfiles (NewFileFilter ());  for(File f:files) {upfiles.put (F.getname (),NewFile ("/sdcard/"+f.getname ())); }                     //Toast.maketext (this, filename, Toast.length_long). Show ();                       }                 classFileFilter implements FilenameFilter {@Override PublicBoolean Accept (File dir, String filename) {returnFilename.endswith (". mp3"); }            }               //upload Code, the first parameter, for the URL to use, the second parameter, for the form content, the third parameter for the file to be uploaded, you can upload multiple files, depending on the need to page     Public StaticBoolean post (String actionurl,map<string, string>params, Map<string, file>files) throws IOException {String boundary=Java.util.UUID.randomUUID (). toString (); String PREFIX="--", Linend ="\ r \ n"; String Multipart_from_data="Multipart/form-data"; String CHARSET="UTF-8"; URL URI=NewURL (ActionURL); HttpURLConnection Conn=(HttpURLConnection) uri.openconnection (); Conn.setreadtimeout (5* +); Conn.setdoinput (true);//Allow inputConn.setdooutput (true);//Allow outputConn.setusecaches (false); Conn.setrequestmethod ("POST");//Post ModeConn.setrequestproperty ("Connection","keep-alive"); Conn.setrequestproperty ("Charsert","UTF-8"); Conn.setrequestproperty ("Content-type", Multipart_from_data+"; boundary="+boundary); //first set of parameters for the type of the spelling textStringBuilder SB =NewStringBuilder ();  for(Map.entry<string, string> Entry:params. EntrySet ())                {sb.append (PREFIX);                Sb.append (boundary);                Sb.append (Linend); Sb.append ("content-disposition:form-data; name=\ ""+ Entry.getkey () +"\""+linend); Sb.append ("Content-type:text/plain; charset="+ CHARSET +linend); Sb.append ("Content-transfer-encoding:8bit"+linend);                Sb.append (Linend);                Sb.append (Entry.getvalue ());            Sb.append (Linend); } DataOutputStream OutStream=NewDataOutputStream (Conn.getoutputstream ());            Outstream.write (Sb.tostring (). GetBytes ()); //Send file Data        if(Files! =NULL)                 for(Map.entry<string, file>File:files.entrySet ()) {StringBuilder SB1=NewStringBuilder ();                    Sb1.append (PREFIX);                    Sb1.append (boundary);                    Sb1.append (Linend); Sb1.append ("Content-disposition:form-data, name=\ "file\"; filename=\ ""+ File.getkey () +"\""+linend); Sb1.append ("content-type:multipart/form-data; charset="+ CHARSET +linend);                    Sb1.append (Linend);                    Outstream.write (Sb1.tostring (). GetBytes ()); InputStream is=NewFileInputStream (File.getvalue ()); byte[] buffer =New byte[1024x768]; intLen =0;  while(len = is. Read (buffer))! =-1) {outstream.write (buffer,0, Len); }                     is. Close ();                Outstream.write (Linend.getbytes ()); }            //Request End Flag        byte[] End_data = (PREFIX + boundary + PREFIX +linend). GetBytes ();            Outstream.write (End_data);            Outstream.flush (); //Get the response codeBoolean success = Conn.getresponsecode () = = $; InputStreaminch=Conn.getinputstream (); InputStreamReader Isreader=NewInputStreamReader (inch); BufferedReader Bufreader=NewBufferedReader (Isreader); String Line=NULL; String Data="getresult=";  while(line = Bufreader.readline ())! =NULL) Data+=Line ;            Outstream.close ();            Conn.disconnect (); returnsuccess; }    

Android Upload file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.