android 文件上传类(可以直接被调用的)
生活随笔
收集整理的這篇文章主要介紹了
android 文件上传类(可以直接被调用的)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class post {
// 如果是文本的文件的話那么通過map類傳遞進來如果是文件的話通過FormFile傳遞進來
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); // 緩存的最長時間
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false); // 不允許使用緩存
// 下面的幾個值是必須需要設置進去的
conn.setRequestMethod(”POST”);
conn.setRequestProperty(”connection”, “keep-alive”);
conn.setRequestProperty(”Charsert”, “UTF-8″);
conn.setRequestProperty(”Content-Type”, MULTIPART_FROM_DATA
+ “:boundary” + BOUNDARY);
// 首先組拼文本類型的參數
StringBuilder sb = new StringBuilder();
// 這個地方使用了Map循環 map循環的方式需要注意一下了
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());
// 前面必須是數組才可以
// 發送文件數據
for (FormFile file : files) {
StringBuilder sb1 = new StringBuilder();
sb1.append(”—”);
sb1.append(BOUNDARY);
sb1.append(”\r\n”);
// 這個地方沒有完
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();
// 得到響應號碼
int res = conn.getResponseCode();
if (res != 200)
throw new RuntimeException(”請求失敗 “);
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();
}
這是相關聯的formFIle類的定義
public class FormFile {
// 定義了使用的文件的特點
// 上傳文件的數據
private byte[] data;
private InputStream inStream;
// 文件名稱
private String fileName;
// 請求參數名稱
private String Formnames;
// 內容類型
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;
}
// 如果是文本的文件的話那么通過map類傳遞進來如果是文件的話通過FormFile傳遞進來
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); // 緩存的最長時間
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false); // 不允許使用緩存
// 下面的幾個值是必須需要設置進去的
conn.setRequestMethod(”POST”);
conn.setRequestProperty(”connection”, “keep-alive”);
conn.setRequestProperty(”Charsert”, “UTF-8″);
conn.setRequestProperty(”Content-Type”, MULTIPART_FROM_DATA
+ “:boundary” + BOUNDARY);
// 首先組拼文本類型的參數
StringBuilder sb = new StringBuilder();
// 這個地方使用了Map循環 map循環的方式需要注意一下了
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());
// 前面必須是數組才可以
// 發送文件數據
for (FormFile file : files) {
StringBuilder sb1 = new StringBuilder();
sb1.append(”—”);
sb1.append(BOUNDARY);
sb1.append(”\r\n”);
// 這個地方沒有完
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();
// 得到響應號碼
int res = conn.getResponseCode();
if (res != 200)
throw new RuntimeException(”請求失敗 “);
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();
}
這是相關聯的formFIle類的定義
public class FormFile {
// 定義了使用的文件的特點
// 上傳文件的數據
private byte[] data;
private InputStream inStream;
// 文件名稱
private String fileName;
// 請求參數名稱
private String Formnames;
// 內容類型
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
轉載于:https://www.cnblogs.com/shanzei/archive/2012/04/06/2434556.html
總結
以上是生活随笔為你收集整理的android 文件上传类(可以直接被调用的)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Liferay 控制面板在指定文件夹添加
- 下一篇: poj2624