編輯:Android技術基礎
本節和下一節文件下載一樣,慎入...現在實際開發涉及文件上傳不會自己寫上傳代碼,一般 會集成第三網絡庫來做圖片上傳,比如android-async-http,okhttp等,另外還有七牛也提供 了下載和上傳的API,喜歡的可以去官網查看相關的API文檔!本節的話有興趣看看就好!
思前想後,還是決定先貼下公司項目中用到的圖片上傳的核心方法,這裡用到一個第三方的庫: android-async-http.jar,自己到github下下這個庫~然後調用一下下面的方法即可,自己改下url!
上傳圖片的核心方法如下:
private void sendImage(Bitmap bm) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 60, stream); byte[] bytes = stream.toByteArray(); String img = new String(Base64.encodeToString(bytes, Base64.DEFAULT)); AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.add("img", img); client.post("http:xxx/postIcon", params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { Toast.makeText(MainActivity.this, "Upload Success!", Toast.LENGTH_LONG).show(); } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(MainActivity.this, "Upload Fail!", Toast.LENGTH_LONG).show(); } }); }
簡直臥槽...各種設置,各種麻煩...還是建議用1的方法吧,當然,實在太閒可以看看, 有輪子可用還是先別自己造輪子了...
public class SocketHttpRequester { /** * 發送xml數據 * @param path 請求地址 * @param xml xml數據 * @param encoding 編碼 * @return * @throws Exception */ public static byte[] postXml(String path, String xml, String encoding) throws Exception{ byte[] data = xml.getBytes(encoding); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "text/xml; charset="+ encoding); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); conn.setConnectTimeout(5 * 1000); OutputStream outStream = conn.getOutputStream(); outStream.write(data); outStream.flush(); outStream.close(); if(conn.getResponseCode()==200){ return readStream(conn.getInputStream()); } return null; } /** * 直接通過HTTP協議提交數據到服務器,實現如下面表單提交功能: * <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data"> <INPUT TYPE="text" NAME="name"> <INPUT TYPE="text" NAME="id"> <input type="file" name="imagefile"/> <input type="file" name="zip"/> </FORM> * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試, * 因為它會指向手機模擬器,你可以使用http://www.baidu.com或http://192.168.1.10:8080這樣的路徑測試) * @param params 請求參數 key為參數名,value為參數值 * @param file 上傳文件 */ public static boolean post(String path, Map<String, String> params, FormFile[] files) throws Exception { //數據分隔線 final String BOUNDARY = "---------------------------7da2137580612"; //數據結束標志"---------------------------7da2137580612--" final String endline = "--" + BOUNDARY + "--/r/n"; //下面兩個for循環都是為了得到數據長度參數,依據表單的類型而定 //首先得到文件類型數據的總長度(包括文件分割線) int fileDataLength = 0; for(FormFile uploadFile : files) { StringBuilder fileExplain = new StringBuilder(); fileExplain.append("--"); fileExplain.append(BOUNDARY); fileExplain.append("/r/n"); fileExplain.append("Content-Disposition: form-data;name=/""+ uploadFile.getParameterName()+"/";filename=/""+ uploadFile.getFilname() + "/"/r/n"); fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"/r/n/r/n"); fileExplain.append("/r/n"); fileDataLength += fileExplain.length(); if(uploadFile.getInStream()!=null){ fileDataLength += uploadFile.getFile().length(); }else{ fileDataLength += uploadFile.getData().length; } } //再構造文本類型參數的實體數據 StringBuilder textEntity = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { textEntity.append("--"); textEntity.append(BOUNDARY); textEntity.append("/r/n"); textEntity.append("Content-Disposition: form-data; name=/""+ entry.getKey() + "/"/r/n/r/n"); textEntity.append(entry.getValue()); textEntity.append("/r/n"); } //計算傳輸給服務器的實體數據總長度(文本總長度+數據總長度+分隔符) int dataLength = textEntity.toString().getBytes().length + fileDataLength + endline.getBytes().length; URL url = new URL(path); //默認端口號其實可以不寫 int port = url.getPort()==-1 ? 80 : url.getPort(); //建立一個Socket鏈接 Socket socket = new Socket(InetAddress.getByName(url.getHost()), port); //獲得一個輸出流(從Android流到web) OutputStream outStream = socket.getOutputStream(); //下面完成HTTP請求頭的發送 String requestmethod = "POST "+ url.getPath()+" HTTP/1.1/r/n"; outStream.write(requestmethod.getBytes()); //構建accept String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*/r/n"; outStream.write(accept.getBytes()); //構建language String language = "Accept-Language: zh-CN/r/n"; outStream.write(language.getBytes()); //構建contenttype String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "/r/n"; outStream.write(contenttype.getBytes()); //構建contentlength String contentlength = "Content-Length: "+ dataLength + "/r/n"; outStream.write(contentlength.getBytes()); //構建alive String alive = "Connection: Keep-Alive/r/n"; outStream.write(alive.getBytes()); //構建host String host = "Host: "+ url.getHost() +":"+ port +"/r/n"; outStream.write(host.getBytes()); //寫完HTTP請求頭後根據HTTP協議再寫一個回車換行 outStream.write("/r/n".getBytes()); //把所有文本類型的實體數據發送出來 outStream.write(textEntity.toString().getBytes()); //把所有文件類型的實體數據發送出來 for(FormFile uploadFile : files) { StringBuilder fileEntity = new StringBuilder(); fileEntity.append("--"); fileEntity.append(BOUNDARY); fileEntity.append("/r/n"); fileEntity.append("Content-Disposition: form-data;name=/""+ uploadFile.getParameterName()+"/";filename=/""+ uploadFile.getFilname() + "/"/r/n"); fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"/r/n/r/n"); outStream.write(fileEntity.toString().getBytes()); //邊讀邊寫 if(uploadFile.getInStream()!=null) { byte[] buffer = new byte[1024]; int len = 0; while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1) { outStream.write(buffer, 0, len); } uploadFile.getInStream().close(); } else { outStream.write(uploadFile.getData(), 0, uploadFile.getData().length); } outStream.write("/r/n".getBytes()); } //下面發送數據結束標志,表示數據已經結束 outStream.write(endline.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); //讀取web服務器返回的數據,判斷請求碼是否為200,如果不是200,代表請求失敗 if(reader.readLine().indexOf("200")==-1) { return false; } outStream.flush(); outStream.close(); reader.close(); socket.close(); return true; } /** * 提交數據到服務器 * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.baidu.com或http://192.168.1.10:8080這樣的路徑測試) * @param params 請求參數 key為參數名,value為參數值 * @param file 上傳文件 */ public static boolean post(String path, Map<String, String> params, FormFile file) throws Exception { return post(path, params, new FormFile[]{file}); } /** * 提交數據到服務器 * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.baidu.com或http://192.168.1.10:8080這樣的路徑測試) * @param params 請求參數 key為參數名,value為參數值 * @param encode 編碼 */ public static byte[] postFromHttpClient(String path, Map<String, String> params, String encode) throws Exception { //用於存放請求參數 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for(Map.Entry<String, String> entry : params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, encode); HttpPost httppost = new HttpPost(path); httppost.setEntity(entity); //看作是浏覽器 HttpClient httpclient = new DefaultHttpClient(); //發送post請求 HttpResponse response = httpclient.execute(httppost); return readStream(response.getEntity().getContent()); } /** * 發送請求 * @param path 請求路徑 * @param params 請求參數 key為參數名稱 value為參數值 * @param encode 請求參數的編碼 */ public static byte[] post(String path, Map<String, String> params, String encode) throws Exception { //String params = "method=save&name="+ URLEncoder.encode("老畢", "UTF-8")+ "&age=28&";//需要發送的參數 StringBuilder parambuilder = new StringBuilder(""); if(params!=null && !params.isEmpty()) { for(Map.Entry<String, String> entry : params.entrySet()) { parambuilder.append(entry.getKey()).append("=") .append(URLEncoder.encode(entry.getValue(), encode)).append("&"); } parambuilder.deleteCharAt(parambuilder.length()-1); } byte[] data = parambuilder.toString().getBytes(); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設置允許對外發送請求參數 conn.setDoOutput(true); //設置不進行緩存 conn.setUseCaches(false); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("POST"); //下面設置http請求頭 conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); conn.setRequestProperty("Connection", "Keep-Alive"); //發送參數 DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(data);//把參數發送出去 outStream.flush(); outStream.close(); if(conn.getResponseCode()==200) { return readStream(conn.getInputStream()); } return null; } /** * 讀取流 * @param inStream * @return 字節數組 * @throws Exception */ public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len=inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); } }
偶然發現一篇以前轉載的,可以搭配著上面的看看...:使用HttpConnection上傳mp3文件
本節還是直接無視吧...關於文件上傳等進階部分直接教大家用第三方算了,項目中需要用到 第三方直接復制1的代碼,導入個android-async-http即可!
在Android應用中,有多種對話框:Dialog、AlertDialog、ProgressDialog、時間、日期等對話框。 (1)Dialog類,是一切對話框的基類
網格視圖對於需要有類似“九宮格”布局時非常有用。 一、設計界面 1、打開“res/layout/activity_main
本節引言:前面兩節我們學習的都是一些概念性的東西,Http的協議以及協議頭的一些東東,而本節我們就要堆碼了,而本節學習的是Android為我們提供的
Service(服務)在Android中地位是至關重要的,我們可以通過Activity與Broadcast(廣播)啟動Service(服務),我們本章學習如何通過廣播Br