狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

Android用http協(xié)議上傳文件

admin
2014年11月24日 23:56 本文熱度 6119

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 /** 文件名稱(chēng) */

015 private String filname;

016 /** 請(qǐng)求參數(shù)名稱(chēng)*/

017 private String parameterName;

018 /** 內(nèi)容類(lèi)型 */

019 private String contentType = "application/octet-stream";

020 /**

021 *

022 * @param filname 文件名稱(chēng)

023 * @param data 上傳的文件數(shù)據(jù)

024 * @param parameterName 參數(shù)

025 * @param contentType 內(nèi)容類(lè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)容類(lè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 * 直接通過(guò)HTTP協(xié)議提交數(shù)據(jù)到服務(wù)器,實(shí)現(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這樣的路徑測(cè)試,因?yàn)樗鼤?huì)指向手機(jī)模擬器,你可以使用http://www.xxx.cn或http://192.168.1.10:8080這樣的路徑測(cè)試)

099 * @param params 請(qǐng)求參數(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ù)分隔線(xiàn)

104 final String endline = "--" + BOUNDARY + "--\r\n";//數(shù)據(jù)結(jié)束標(biāo)志

105

106 int fileDataLength = 0;

107 for(FormFile uploadFile : files){//得到文件類(lèi)型數(shù)據(jù)的總長(zhǎng)度

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)造文本類(lèi)型參數(shù)的實(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 //計(jì)算傳輸給服務(wù)器的實(shí)體數(shù)據(jù)總長(zhǎng)度

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請(qǐng)求頭的發(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 //寫(xiě)完HTTP請(qǐng)求頭后根據(jù)HTTP協(xié)議再寫(xiě)一個(gè)回車(chē)換行

154 outStream.write("\r\n".getBytes());

155 //把所有文本類(lèi)型的實(shí)體數(shù)據(jù)發(fā)送出來(lái)

156 outStream.write(textEntity.toString().getBytes());

157 //把所有文件類(lèi)型的實(shí)體數(shù)據(jù)發(fā)送出來(lái)

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ù),判斷請(qǐng)求碼是否為200,如果不是200,代表請(qǐng)求失敗

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這樣的路徑測(cè)試,因?yàn)樗鼤?huì)指向手機(jī)模擬器,你可以使用http://www.xxx.cn或http://192.168.1.10:8080這樣的路徑測(cè)試)

195 * @param params 請(qǐng)求參數(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ò)
關(guān)鍵字查詢(xún)
相關(guān)文章
正在查詢(xún)...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專(zhuān)業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶(hù)的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved