android 文件上傳的類--完整 可以直接被調用的

android 文件上傳的類--完整 可以直接被調用的

public class post {

// 如果是文本的文件的話那麼通過map類傳遞進來如果是文件的話通過FormFile傳遞進來
public static String post(String actionUrl, Map<String, String> 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<String, String> 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章