日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 文件上传类(可以直接被调用的)

發(fā)布時(shí)間:2025/3/15 编程问答 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 文件上传类(可以直接被调用的) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public class post {
// 如果是文本的文件的話那么通過(guò)map類傳遞進(jìn)來(lái)如果是文件的話通過(guò)FormFile傳遞進(jìn)來(lái)
public static String post(String actionUrl, Map params,
FormFile[] files) throws IOException {
String BOUNDARY = “743520vjdk4e”;
String MULTIPART_FROM_DATA = “multipart/form-data”;
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000); // 緩存的最長(zhǎng)時(shí)間
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false); // 不允許使用緩存
// 下面的幾個(gè)值是必須需要設(shè)置進(jìn)去的
conn.setRequestMethod(”POST”);
conn.setRequestProperty(”connection”, “keep-alive”);
conn.setRequestProperty(”Charsert”, “UTF-8″);
conn.setRequestProperty(”Content-Type”, MULTIPART_FROM_DATA
+ “:boundary” + BOUNDARY);
// 首先組拼文本類型的參數(shù)
StringBuilder sb = new StringBuilder();
// 這個(gè)地方使用了Map循環(huán) map循環(huán)的方式需要注意一下了
for (Map.Entry entry : params.entrySet()) {
sb.append(”–”);
sb.append(BOUNDARY);
// 回車換行
sb.append(”\r\n”);
sb.append(”Content-Disposition:form-data:name\”" + entry.getKey()
+ “\r\n\r\n”);
sb.append(entry.getValue());
sb.append(”\r\n”);
}
DataOutputStream outStream = new DataOutputStream(conn
.getOutputStream());
outStream.write(sb.toString().getBytes());
// 前面必須是數(shù)組才可以
// 發(fā)送文件數(shù)據(jù)
for (FormFile file : files) {
StringBuilder sb1 = new StringBuilder();
sb1.append(”—”);
sb1.append(BOUNDARY);
sb1.append(”\r\n”);
// 這個(gè)地方?jīng)]有完
sb1.append(”Content-Disposition:form-data:name=\”"
+ file.getFormname());
sb1.append(”Content-Type” + file.getContentType() + “\r\n\r\n”);
outStream.write(sb1.toString().getBytes());
// 先判斷formfile里面是否為空 如果不為空的話則寫出 獲取formfile的data里面的
if (file.getInStream() != null) {
// 提供流的的方式的話就是一邊讀一邊寫了
byte[] buffer = new byte[1024];
int len = 0;
while ((len = file.getInStream().read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
file.getInStream().close();
} else {
outStream.write(file.getData(), 0, file.getData().length);
}
outStream.write(”\r\n”.getBytes());
}
byte[] end_data = (”–” + BOUNDARY + “\r\n”).getBytes();
outStream.write(end_data);
outStream.flush();
// 得到響應(yīng)號(hào)碼
int res = conn.getResponseCode();
if (res != 200)
throw new RuntimeException(”請(qǐng)求失敗 “);
InputStream in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1) {
sb.append((char) ch);
}
outStream.close();
conn.disconnect();
return in.toString();
}
這是相關(guān)聯(lián)的formFIle類的定義
public class FormFile {
// 定義了使用的文件的特點(diǎn)
// 上傳文件的數(shù)據(jù)
private byte[] data;
private InputStream inStream;
// 文件名稱
private String fileName;
// 請(qǐng)求參數(shù)名稱
private String Formnames;
// 內(nèi)容類型
private String contentType = “application/octet-stream”;
public FormFile(byte[] data, String fileName, String formnames,
String contentType) {
this.data = data;
this.fileName = fileName;
Formnames = formnames;
this.contentType = contentType;
}
public FormFile(InputStream inStream, String fileName, String formnames,
String contentType) {
this.inStream = inStream;
this.fileName = fileName;
Formnames = formnames;
this.contentType = contentType;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public InputStream getInStream() {
return inStream;
}
public void setInStream(InputStream inStream) {
this.inStream = inStream;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFormnames() {
return Formnames;
}
public void setFormnames(String formnames) {
Formnames = formnames;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}

原文:http://blog.chinaunix.net/uid-20787846-id-1842620.html

轉(zhuǎn)載于:https://www.cnblogs.com/shanzei/archive/2012/04/06/2434556.html

總結(jié)

以上是生活随笔為你收集整理的android 文件上传类(可以直接被调用的)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。