This is a creation in Article, where the information may have evolved or changed.
This article is to demonstrate the use of the Android program as a client (using HttpURLConnection to access the network), the Golang program as a server side, to implement file upload.
Client code:
public static string UploadFile (String uploadurl, String filePath) {log.v (TAG, "url:" + uploadurl); LOG.V (TAG, "FilePath:" + FilePath); String nextline = "\ r \ n"; String Dividerstart = "--"; String boundary = "******"; try {URL url = new URL (uploadurl); HttpURLConnection connection = (httpurlconnection) url.openconnection (); Connection.setchunkedstreamingmode (1024 * 256); Connection.setdoinput (TRUE); Connection.setdooutput (TRUE); Connection.setusecaches (FALSE); Connection.setrequestmethod ("POST"); Set HTTP request Header Connection.setrequestproperty ("Connection", "keep-alive"); Connection.setrequestproperty ("Charset", "UTF-8"); The delimiter must be specified in the Content-type request header Connection.setrequestproperty ("Content-type", "multipart/form-data;boundary=" + Bou Ndary); Define data write stream, ready to upload file DataOutputStream dos = new DataoutputstreAM (Connection.getoutputstream ()); Dos.writebytes (Dividerstart + boundary + nextline); Set up information related to uploading files dos.writebytes ("Content-disposition:form-data; Name=\ "File\"; Filename=\ "" + filepath.substring (Filepath.lastindexof ("/") + 1) + "\" "+ nextline); Dos.writebytes (nextline); FileInputStream fis = new FileInputStream (FilePath); byte[] buffer = new byte[1024 * 32]; int count; Reads the contents of the file and writes the OutputStream object while ((count = fis.read (buffer))! =-1) {dos.write (buffer, 0, Coun T); } fis.close (); Dos.writebytes (nextline); Dos.writebytes (Dividerstart + boundary + Dividerstart + nextline); Dos.flush (); Start reading information from the server InputStream is = Connection.getinputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (IS, "UTF-8")); String result = Br.readline (); Dos.close (); Is.close (); Connection.disconnect (); return result; } catch (IOException e) {e.printstacktrace (); } return null; }
Server-side code:
Package webserver//receives client-uploaded files via http//date:2015-3-25 16:18:33import ("FMT" "Io/ioutil" "Log" "Net/http" "OS") func Uploadbase () {fmt. Println ("This is Uploadbase") http. Handlefunc ("/httpuploadfile", Handleuploadfile) http. Listenandserve (": 8086", nil) if err! = Nil {fmt. PRINTLN ("Listenandserve error:", err.) Error ())}}func Handleuploadfile (w http. Responsewriter, R *http. Request) {fmt. PRINTLN ("Client:", r.remoteaddr) file, Fileheader, err: = R.formfile ("file") if err! = Nil {log. Fatal ("Formfile:", err.) Error ()) Return}defer func () {if err: = file. Close (); Err! = Nil {log. Fatal ("Close:", err.) Error ()) Return}} ()//file name filename: = Fileheader.filenameif filename = = "" {log. Fatal ("Param filename cannot be null.") return}//file Contents bytes, err: = Ioutil. ReadAll (file)//write to the server-side local file Outputfilepath: = "/home/admin/Desktop/" + Filenameerr = ioutil. WriteFile (Outputfilepath, Bytes, os. MODEPERM) If err! = Nil {log. Fatal ("Writefileerror:", err.) Error ()) return}w.write ([]byte) ("Upload file succeeded! "))}