update
This commit is contained in:
@@ -3,6 +3,7 @@ package com.yutou.nas.utils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.nas.interfaces.DownloadInterface;
|
||||
import com.yutou.nas.utils.Interfaces.NetworkInterface;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -14,7 +15,7 @@ public class HttpTools {
|
||||
public static String get(String url) {
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestProperty("User-Agent", getKuKuUA());
|
||||
connection.setRequestProperty("User-Agent", getExtUa());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder str = new StringBuilder();
|
||||
String tmp;
|
||||
@@ -43,6 +44,7 @@ public class HttpTools {
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
connection.setConnectTimeout(5 * 1000);
|
||||
connection.setReadTimeout(10 * 1000);
|
||||
//connection.addRequestProperty("Connection", "keep-alive");
|
||||
@@ -127,7 +129,9 @@ public class HttpTools {
|
||||
com.yutou.nas.utils.Log.i(url);
|
||||
//String str=get(url);
|
||||
}
|
||||
private static String donwloadPath="tmp"+File.separator;
|
||||
|
||||
private static String donwloadPath = "tmp" + File.separator;
|
||||
|
||||
public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
@@ -140,7 +144,7 @@ public class HttpTools {
|
||||
}
|
||||
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
|
||||
|
||||
|
||||
@@ -150,14 +154,14 @@ public class HttpTools {
|
||||
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
|
||||
OutputStream outputStream = new FileOutputStream(jar);
|
||||
byte[] bytes = new byte[1024];
|
||||
double size=connection.getContentLength();
|
||||
double downSize=0;
|
||||
double size = connection.getContentLength();
|
||||
double downSize = 0;
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) > 0) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
downSize+=len;
|
||||
if(downloadInterface!=null){
|
||||
downloadInterface.onDownloading(downSize,size);
|
||||
downSize += len;
|
||||
if (downloadInterface != null) {
|
||||
downloadInterface.onDownloading(downSize, size);
|
||||
}
|
||||
}
|
||||
outputStream.close();
|
||||
@@ -183,4 +187,53 @@ public class HttpTools {
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public synchronized static File syncDownload(final String url, final String saveName) {
|
||||
if(StringUtils.isEmpty(url)){
|
||||
return null;
|
||||
}
|
||||
File jar = null;
|
||||
try {
|
||||
File savePath = new File(donwloadPath);
|
||||
if (!savePath.exists()) {
|
||||
savePath.mkdirs();
|
||||
}
|
||||
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());
|
||||
|
||||
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
jar = new File(donwloadPath + saveName + "_tmp.tmp");
|
||||
jar.createNewFile();
|
||||
Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
|
||||
OutputStream outputStream = new FileOutputStream(jar);
|
||||
byte[] bytes = new byte[1024];
|
||||
double size = connection.getContentLength();
|
||||
double downSize = 0;
|
||||
int len;
|
||||
while ((len = inputStream.read(bytes)) > 0) {
|
||||
outputStream.write(bytes, 0, len);
|
||||
downSize += len;
|
||||
}
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
File oldJar = new File(donwloadPath + saveName);
|
||||
if (oldJar.exists()) {
|
||||
oldJar.delete();
|
||||
}
|
||||
connection.disconnect();
|
||||
jar.renameTo(oldJar);
|
||||
Log.i("DOWNLOAD", "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName());
|
||||
return oldJar;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (jar != null) {
|
||||
jar.delete();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user