import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Http {
public static void main(String[] args) {
// TODO Auto-generated method stub
// HttpDown hd = new
// HttpDown("http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip");
HttpDown hd = new HttpDown("http://download.oracle.com/technetwork/java/javase/6/docs/zh/api.zip");
hd.SavetoDisk();
}
}
class HttpDown {
private String URL_PATH;
public HttpDown(String URL_PATH) {
this.URL_PATH = URL_PATH;
}
public void SavetoDisk() {
InputStream is = null;
FileOutputStream fo = null;
try {
fo = new FileOutputStream("e:\\testFile.zip");
is = DownStream();
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fo.write(buffer, 0, len);
fo.flush();
System.out.println(len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fo != null) {
fo.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream DownStream() {
InputStream inputStream = null;
HttpURLConnection hul = null;
try {
URL rul = new URL(URL_PATH);
hul = (HttpURLConnection) rul.openConnection();
hul.setConnectTimeout(3000);
hul.setReadTimeout(3000);
hul.setDoInput(true);
hul.setRequestMethod("GET");
if (hul.getResponseCode() == 200) {
inputStream = hul.getInputStream();
} else {
inputStream = null;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return inputStream;
}
}
0 件のコメント:
コメントを投稿