您的位置:首页 > 理论基础 > 计算机网络

Android Http网络数据传输备忘

2011-06-18 15:17 267 查看
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class HttpTool {
private static int TIME_OUT = 1000 * 6;
private static String POST = "POST";
private static String GET = "GET";
private static String UTF8 = "UTF-8";
public static void downloadGET(String path) throws Exception{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(TIME_OUT);
connection.setRequestMethod(GET);
connection.setUseCaches(false);
connection.connect();
if(connection.getResponseCode() != 200){
throw new RuntimeException("ResponseCode != 200");
}
InputStream inputStream = connection.getInputStream();
File file = new File(rename(path));
FileOutputStream fileOutputStream = new FileOutputStream(file);
readinput(inputStream, fileOutputStream);
connection.disconnect();
System.out.println("ok");
}
public static void downloadPOST(String path,Map<String, String> params) throws Exception{
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(TIME_OUT);
connection.setRequestMethod(POST);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", UTF8);
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset="+UTF8);
StringBuilder sb = new StringBuilder();
if(params != null){
for(Map.Entry<String, String> item : params.entrySet()){
sb.append(item.getKey()+"="+item.getValue()).append("&");
}
sb = sb.deleteCharAt(sb.length()-1);
byte[] data = sb.toString().getBytes();
connection.setRequestProperty("Content-Length",String.valueOf(data.length));
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data);
outputStream.flush();
outputStream.close();
}
if(connection.getResponseCode() != 200){
throw new RuntimeException("ResponseCode != 200");
}
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
while((len = bufferedInputStream.read(buffer)) != -1){
arrayOutputStream.write(buffer, 0, len);
}
byte[] bytes = arrayOutputStream.toByteArray();
String str = new String(bytes,UTF8);
arrayOutputStream.close();
bufferedInputStream.close();
connection.disconnect();
System.out.println(str);
}

/**
* 读取输入流
* @param inputStream
* @param fileOutputStream
* @throws Exception
*/
private static void readinput(InputStream inputStream,FileOutputStream fileOutputStream) throws Exception{
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int len = -1;
while((len = bufferedInputStream.read(buffer)) != -1){
bufferedOutputStream.write(buffer, 0, len);
}
bufferedOutputStream.close();
bufferedInputStream.close();
}
/**
* 重名名
* @param path
* @return
*/
public static String rename(String path){
String str = path.substring(path.lastIndexOf("."));
return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date())+str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: