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;
}

更多相关文章

  1. Android常用权限
  2. Android关闭输入法
  3. android 读写文件
  4. Android(安卓)uses-permission 总结
  5. Android(安卓)限制EditText输入小数点后面位数
  6. android中限制EditText中输入指定字符
  7. android 读取sd卡中的图片
  8. 如何消除原生Android原生网络连接显示x或者惊叹号
  9. android EditText和输入法相关知识总结

随机推荐

  1. android SQLite数据库存储数据
  2. 怎样成为一名Android开发者
  3. Android 内核开发初步
  4. android N多窗口和画中画属性
  5. NETBEAN 开发Android应用
  6. Android 线性布局(LinearLayout)内各控件
  7. 线性布局和相对布局
  8. ListView背景修饰
  9. FrameLayout的使用
  10. android客户端和servlet服务端的简单登录