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. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 当使用opendkim在MTA签名时,DKIM失败(身体
  2. MySQLi处理查询两次或两次出错
  3. 用于cdn集成的文件类型的域更改正则表达
  4. PHP会话变量没有转移到我登录的页面,但是
  5. 腾讯AI鉴权demo(PHP版)
  6. php综合复习题大全(下)——提升篇
  7. 为什么我没有滚动条?
  8. Nginx php-fpm和Memcached的问题
  9. thinkphp中ajaxReturn的用法
  10. 应用TP3.2.3的一些小问题(一)