Android File upload to server

Source: Internet
Author: User
Tags response code iqiyi

Project Presentation and explanation

Youku http://v.youku.com/v_show/id_XODk5NjkwOTg4.html

Iqiyi Art Http://www.iqiyi.com/w_19rs1v2m15.html#vfrm=8-7-0-1

Potato Http://www.tudou.com/programs/view/fv0H93IHfhM

Project download

1, mobile phone Select File upload to the server side

http://download.csdn.net/detail/u010134178/8457679

2, the mobile phone to upload photos uploaded to the server side

http://download.csdn.net/detail/u010134178/8457673

3. Total

Http://down.51cto.com/7851921/up

These are required points, if the points are not enough to send to my email [email protected] I sent the project directly.

We must pay attention to the debugging at the same time

1, the network download, upload these operations, is time-consuming operation, to another thread operation, can not be put into the main thread inside.

2, the network download, upload cannot have UI operation, can not display UI inside. Is that you cannot manipulate the UI inside a child thread. If the UI prompts the user for action, you can update the UI using handler with thread, or asynctask update the UI asynchronously.

3, upload to the local tomcat server, to turn off the firewall

public class httppost {/** *  constructs the request content through the stitching way, realizes the parameter transmission as well as the file transfer  *  * @ param acti *            .nurl *   @param  params *  @param  files *  @return  *  @throws   Ioexception */public static string 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 = new url (ActionURL); httpurlconnection conn =  (HttpURLConnection)  uri.openconnection (); Conn.setReadTimeout (5  * 1000); //  Cache maximum time Conn.setdoinput (true);//  allowInput Conn.setdooutput (true);//  allow output conn.setusecaches (false); //  does not allow caching Conn.setrequestmethod ("POST Conn.setrequestproperty ("Connection",  "keep-alive"); Conn.setrequestproperty ("Charsert",  "UTF-8"); Conn.setrequestproperty ("Content-type", multipart_from_data +  "; boundary="  + BOUNDARY ;//  first sets the parameters of the spelling type Stringbuilder sb = new stringbuilder ();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 = new dataoutputstream (CONN.GETOUTPUtstream ()); Outstream.write (Sb.tostring (). GetBytes ());//  send file Data if  (files != null) for  (Map.entry<string, file> file : files.entryset ())  {StringBuilder sb1  = new stringbuilder (); 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: application/octet-stream; charset= " +  charset + linend); Sb1.append (linend); Outstream.write (Sb1.tostring (). GetBytes ()); InputStream  is = new fileinputstream (File.getvalue ()); byte[] buffer = new byte[ 1024];int len = 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); o Utstream.flush ();//  Get Response Code Int res = conn.getresponsecode ();inputstream in =  Conn.getinputstream ();if  (res == 200)  {int ch; Stringbuilder sb2 = new stringbuilder ();while  ((Ch = in.read ())  !=  -1)  {sb2.append ((char)  ch);}} Outstream.close (); Conn.disconnect (); return in.tostring ();}}


Mobile phone photo Upload to server main code

public void Uploadphoto () {map<string, string> params = new hashmap<string, string> ();p arams.put (" strParamName "," Strparamvalue "); Map<string, file> files = new hashmap<string, file> (); Files.put (System.currenttimemillis () + ". jpg", new File (UploadFile));//uploadfile = "/sdcard/anbo/laolisb.jpg"; try {String str = httppost.post (ActionURL, params, files); SYSTEM.OUT.PRINTLN ("str--->>>" + str);} catch (Exception e) {}}

Select File upload to server main code

/******************************************************/private string fun (String msg) {int  i = msg.length (); Int j = msg.lastindexof ("/")  + 1; String a = msg.substring (j, i)  ; System.out.println (a); return a;} /******************************************************/private void uploadfile ()  { Show.setvisibility (view.visible); New thread ()  {@Overridepublic  void run ()  {message  msg = message.obtain ();//  Server access Path string uploadurl =  "/http 192.168.0.104:8080/uploadphoto1/uploadservlet "; Map<string, file> files = new hashmap<string, file> (); String name = fun (PicturePath); Files.put (Name, new file (PicturePath));//files.put (" Test.jpg ",  new file (PicturePath)); LOG.D ("Str--->>>",  picturepath); Try {string str = httppost.post (uploadurl,& nbsp;new hashmap<string,string> (),  files); SYSTEM.OUT.PRINTLN ("str--->>>"  + str); msg.what = success;}  catch  (Exception e)  {msg.what = faild;} Mhandler.sendmessage (msg);}}. Start ();}


This article is from the "7851921" blog, please be sure to keep this source http://7861921.blog.51cto.com/7851921/1615556

Android File upload to server

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.