该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
public class DownloadClient {
private String usr = null;
private String pwd = null;
private File localPath = null;
public DownloadClient(File targetPath) {
this.localPath = targetPath;
}
public DownloadClient(File targetPath, String usr, String pwd) {
this.localPath = targetPath;
this.usr = usr;
this.pwd = pwd;
}
public void downLoad(String urlstr, String fileName) {
HttpClient httpClient = HttpClientBuilder.create().build();
OutputStream out = null;
InputStream in = null;
try {
HttpGet httpGet = new HttpGet(urlstr);
httpGet.addHeader("userName", usr);
httpGet.addHeader("passwd", pwd);
httpGet.addHeader("fileName", fileName);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
in = entity.getContent();
long length = entity.getContentLength();
if (length <= 0) {
System.out.println("下载文件不存在!");
return;
}
System.out.println("The response value of token:" + httpResponse.getFirstHeader("token"));
File file = new File(localPath, fileName);
if(!file.exists()){
file.createNewFile();
}
out = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int readLength = 0;
while ((readLength=in.read(buffer)) > 0) {
byte[] bytes = new byte[readLength];
System.arraycopy(buffer, 0, bytes, 0, readLength);
out.write(bytes);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void download3(String urlstr, String fileName) {
FileOutputStream out = null;
InputStream in = null;
try{
URL url = new URL(urlstr);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// true -- will setting parameters
httpURLConnection.setDoOutput(true);
// true--will allow read in from
httpURLConnection.setDoInput(true);
// will not use caches
httpURLConnection.setUseCaches(false);
// setting serialized
httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
// default is GET
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charsert", "UTF-8");
// 1 min
httpURLConnection.setConnectTimeout(60000);
// 1 min
httpURLConnection.setReadTimeout(60000);
httpURLConnection.addRequestProperty("userName", usr);
httpURLConnection.addRequestProperty("passwd", pwd);
httpURLConnection.addRequestProperty("fileName", fileName);
// connect to server (tcp)
httpURLConnection.connect();
in = httpURLConnection.getInputStream();// send request to
// server
File file = new File(fileName);
if(!file.exists()){
file.createNewFile();
}
out = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int len = -1;
while ((len = in.read(buffer)) > 0) {
byte[] bytes = new byte[len];
System.arraycopy(buffer, 0, bytes, 0, len);
out.write(bytes);
}
out.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String usr = "";
String pwd = "";
String url = "http://forspeed.onlinedown.net/down/QQGameMini_1080000016_0.zip";
String fn = "QQGameMini_1080000016_0.zip";
File localPath = new File("E:\\test");
DownloadClient dc = new DownloadClient(localPath);
//Download download = new Download(usr, pwd);
dc.downLoad(url, fn);
}
}