Android用http協(xié)議上傳文件
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
http協(xié)議上傳文件一般最大是2M,比較適合上傳小于兩M的文件 [代碼] [Java]代碼 001 import java.io.File; 002 import java.io.FileInputStream; 003 import java.io.FileNotFoundException; 004 import java.io.InputStream; 005 006 /** 007 * 上傳的文件 008 */ 009 public class FormFile { 010 /** 上傳文件的數(shù)據(jù) */ 011 private byte[] data; 012 private InputStream inStream; 013 private File file; 014 /** 文件名稱 */ 015 private String filname; 016 /** 請求參數(shù)名稱*/ 017 private String parameterName; 018 /** 內(nèi)容類型 */ 019 private String contentType = "application/octet-stream"; 020 /** 021 * 022 * @param filname 文件名稱 023 * @param data 上傳的文件數(shù)據(jù) 024 * @param parameterName 參數(shù) 025 * @param contentType 內(nèi)容類型 026 */ 027 public FormFile(String filname, byte[] data, String parameterName, String contentType) { 028 this.data = data; 029 this.filname = filname; 030 this.parameterName = parameterName; 031 if(contentType!=null) this.contentType = contentType; 032 } 033 /** 034 * 035 * @param filname 文件名 036 * @param file 上傳的文件 037 * @param parameterName 參數(shù) 038 * @param contentType 內(nèi)容內(nèi)容類型 039 */ 040 public FormFile(String filname, File file, String parameterName, String contentType) { 041 this.filname = filname; 042 this.parameterName = parameterName; 043 this.file = file; 044 try { 045 this.inStream = new FileInputStream(file); 046 } catch (FileNotFoundException e) { 047 e.printStackTrace(); 048 } 049 if(contentType!=null) this.contentType = contentType; 050 } 051 052 public File getFile() { 053 return file; 054 } 055 056 public InputStream getInStream() { 057 return inStream; 058 } 059 060 public byte[] getData() { 061 return data; 062 } 063 064 public String getFilname() { 065 return filname; 066 } 067 068 public void setFilname(String filname) { 069 this.filname = filname; 070 } 071 072 public String getParameterName() { 073 return parameterName; 074 } 075 076 public void setParameterName(String parameterName) { 077 this.parameterName = parameterName; 078 } 079 080 public String getContentType() { 081 return contentType; 082 } 083 084 public void setContentType(String contentType) { 085 this.contentType = contentType; 086 } 087 088 } 089 090 /** 091 * 直接通過HTTP協(xié)議提交數(shù)據(jù)到服務(wù)器,實現(xiàn)如下面表單提交功能: 092 * <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data"> 093 <INPUT TYPE="text" NAME="name"> 094 <INPUT TYPE="text" NAME="id"> 095 <input type="file" name="imagefile"/> 096 <input type="file" name="zip"/> 097 </FORM> 098 * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機(jī)模擬器,你可以使用http://www.xxx.cn或http://192.168.1.10:8080這樣的路徑測試) 099 * @param params 請求參數(shù) key為參數(shù)名,value為參數(shù)值 100 * @param file 上傳文件 101 */ 102 public static boolean post(String path, Map<String, String> params, FormFile[] files) throws Exception{ 103 final String BOUNDARY = "---------------------------7da2137580612"; //數(shù)據(jù)分隔線 104 final String endline = "--" + BOUNDARY + "--\r\n";//數(shù)據(jù)結(jié)束標(biāo)志 105 106 int fileDataLength = 0; 107 for(FormFile uploadFile : files){//得到文件類型數(shù)據(jù)的總長度 108 StringBuilder fileExplain = new StringBuilder(); 109 fileExplain.append("--"); 110 fileExplain.append(BOUNDARY); 111 fileExplain.append("\r\n"); 112 fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n"); 113 fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n"); 114 fileExplain.append("\r\n"); 115 fileDataLength += fileExplain.length(); 116 if(uploadFile.getInStream()!=null){ 117 fileDataLength += uploadFile.getFile().length(); 118 }else{ 119 fileDataLength += uploadFile.getData().length; 120 } 121 } 122 StringBuilder textEntity = new StringBuilder(); 123 for (Map.Entry<String, String> entry : params.entrySet()) {//構(gòu)造文本類型參數(shù)的實體數(shù)據(jù) 124 textEntity.append("--"); 125 textEntity.append(BOUNDARY); 126 textEntity.append("\r\n"); 127 textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n"); 128 textEntity.append(entry.getValue()); 129 textEntity.append("\r\n"); 130 } 131 //計算傳輸給服務(wù)器的實體數(shù)據(jù)總長度 132 int dataLength = textEntity.toString().getBytes().length + fileDataLength + endline.getBytes().length; 133 134 URL url = new URL(path); 135 int port = url.getPort()==-1 ? 80 : url.getPort(); 136 Socket socket = new Socket(InetAddress.getByName(url.getHost()), port); 137 OutputStream outStream = socket.getOutputStream(); 138 //下面完成HTTP請求頭的發(fā)送 139 String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n"; 140 outStream.write(requestmethod.getBytes()); 141 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"; 142 outStream.write(accept.getBytes()); 143 String language = "Accept-Language: zh-CN\r\n"; 144 outStream.write(language.getBytes()); 145 String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n"; 146 outStream.write(contenttype.getBytes()); 147 String contentlength = "Content-Length: "+ dataLength + "\r\n"; 148 outStream.write(contentlength.getBytes()); 149 String alive = "Connection: Keep-Alive\r\n"; 150 outStream.write(alive.getBytes()); 151 String host = "Host: "+ url.getHost() +":"+ port +"\r\n"; 152 outStream.write(host.getBytes()); 153 //寫完HTTP請求頭后根據(jù)HTTP協(xié)議再寫一個回車換行 154 outStream.write("\r\n".getBytes()); 155 //把所有文本類型的實體數(shù)據(jù)發(fā)送出來 156 outStream.write(textEntity.toString().getBytes()); 157 //把所有文件類型的實體數(shù)據(jù)發(fā)送出來 158 for(FormFile uploadFile : files){ 159 StringBuilder fileEntity = new StringBuilder(); 160 fileEntity.append("--"); 161 fileEntity.append(BOUNDARY); 162 fileEntity.append("\r\n"); 163 fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n"); 164 fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n"); 165 outStream.write(fileEntity.toString().getBytes()); 166 if(uploadFile.getInStream()!=null){ 167 byte[] buffer = new byte[1024]; 168 int len = 0; 169 while((len = uploadFile.getInStream().read(buffer, 0,1024))!=-1){ 170 outStream.write(buffer, 0, len); 171 } 172 uploadFile.getInStream().close(); 173 }else{ 174 outStream.write(uploadFile.getData(), 0, uploadFile.getData().length); 175 } 176 outStream.write("\r\n".getBytes()); 177 } 178 //下面發(fā)送數(shù)據(jù)結(jié)束標(biāo)志,表示數(shù)據(jù)已經(jīng)結(jié)束 179 outStream.write(endline.getBytes()); 180 181 BufferedReader reader = new BufferedReader(newInputStreamReader(socket.getInputStream())); 182 if(reader.readLine().indexOf("200")==-1){//讀取web服務(wù)器返回的數(shù)據(jù),判斷請求碼是否為200,如果不是200,代表請求失敗 183 return false; 184 } 185 outStream.flush(); 186 outStream.close(); 187 reader.close(); 188 socket.close(); 189 return true; 190 } 191 192 /** 193 * 提交數(shù)據(jù)到服務(wù)器 194 * @param path 上傳路徑(注:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機(jī)模擬器,你可以使用http://www.xxx.cn或http://192.168.1.10:8080這樣的路徑測試) 195 * @param params 請求參數(shù) key為參數(shù)名,value為參數(shù)值 196 * @param file 上傳文件 197 */ 198 public static boolean post(String path, Map<String, String> params, FormFile file) throws Exception{ 199 return post(path, params, new FormFile[]{file}); 200 } 該文章在 2014/11/24 23:56:01 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |