Android Http網路資料轉送備忘

來源:互聯網
上載者:User
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;
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.