å¨HTTPéä¿¡ä¸ä½¿ç¨æå¤çå°±æ¯GETåPOSTäºï¼GET请æ±å¯ä»¥è·åéæ页é¢ï¼ä¹å¯ä»¥æåæ°æ¾å¨URLå符串çåé¢ï¼ä¼ éç»æå¡å¨ãPOSTä¸GETçä¸åä¹å¤å¨äºPOSTçåæ°ä¸æ¯æ¾å¨URLå符串éé¢ï¼èæ¯æ¾å¨HTTP请æ±æ°æ®ä¸ã
android ç¨postæ¹å¼ä¸ä¼ å¾çå°æå¡å¨ç示ä¾ä»£ç å¦ä¸ï¼
/**
* ä¸ä¼ æ件å°æå¡å¨ç±»
*/
public class UploadUtil {
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; // è¶
æ¶æ¶é´
private static final String CHARSET = "utf-8"; // 设置ç¼ç
/**
* Androidä¸ä¼ æ件å°æå¡ç«¯
*
* @param file éè¦ä¸ä¼ çæ件
* @param RequestURL 请æ±çrul
* @return è¿åååºçå
容
*/
public static String uploadFile(File file, String RequestURL) {
String result = null;
String BOUNDARY = UUID.randomUUID().toString(); // è¾¹çæ è¯ éæºçæ
String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // å
容类å
try {
URL url = new URL(RequestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(TIME_OUT);
conn.setConnectTimeout(TIME_OUT);
conn.setDoInput(true); // å
许è¾å
¥æµ
conn.setDoOutput(true); // å
许è¾åºæµ
conn.setUseCaches(false); // ä¸å
许使ç¨ç¼å
conn.setRequestMethod("POST"); // 请æ±æ¹å¼
conn.setRequestProperty("Charset", CHARSET); // 设置ç¼ç
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
if (file != null) {
/**
* å½æ件ä¸ä¸ºç©ºï¼ææ件å
è£
并ä¸ä¸ä¼
*/
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
/**
* è¿ééç¹æ³¨æï¼ nameéé¢çå¼ä¸ºæå¡ç«¯éè¦key åªæè¿ä¸ªkey æå¯ä»¥å¾å°å¯¹åºçæ件
* filenameæ¯æ件çååï¼å
å«åç¼åç æ¯å¦:abc.png
*/
sb.append("Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
+ file.getName() + "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close();
dos.write(LINE_END.getBytes());
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();
dos.write(end_data);
dos.flush();
/**
* è·åååºç 200=æå å½ååºæåï¼è·åååºçæµ
*/
int res = conn.getResponseCode();
Log.e(TAG, "response code:" + res);
// if(res==200)
// {
Log.e(TAG, "request success");
InputStream input = conn.getInputStream();
StringBuffer sb1 = new StringBuffer();
int ss;
while ((ss = input.read()) != -1) {
sb1.append((char) ss);
}
result = sb1.toString();
Log.e(TAG, "result : " + result);
// }
// else{
// Log.e(TAG, "request error");
// }
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* éè¿æ¼æ¥çæ¹å¼æé 请æ±å
容ï¼å®ç°åæ°ä¼ è¾ä»¥åæä»¶ä¼ è¾
*
* @param url Service net address
* @param params text content
* @param files pictures
* @return String result of Service response
* @throws IOException
*/
public static String post(String url, Map<String, String> params, Map<String, File> files)
throws IOException {
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(url);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(10 * 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();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
// åéæ件æ°æ®
if (files != null)
for (Map.Entry<String, File> file : files.entrySet()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
+ file.getValue().getName() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
// 请æ±ç»ææ å¿
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
// å¾å°ååºç
int res = conn.getResponseCode();
InputStream in = conn.getInputStream();
StringBuilder sb2 = new StringBuilder();
if (res == 200) {
int ch;
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
outStream.close();
conn.disconnect();
return sb2.toString();
}
}