[FEATURE] Готов Downloader.

This commit is contained in:
zaxar163 2019-03-08 16:39:27 +03:00
parent 1819e1c95c
commit d06fcafdc7
3 changed files with 8 additions and 50 deletions

View file

@ -130,22 +130,6 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
lastContentFuture = sendFileFuture; lastContentFuture = sendFileFuture;
} }
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
@Override
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
if (total < 0) { // total unknown
System.err.println(future.channel() + " Transfer progress: " + progress);
} else {
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
}
}
@Override
public void operationComplete(ChannelProgressiveFuture future) {
System.err.println(future.channel() + " Transfer complete.");
}
});
// Decide whether to close the connection or not. // Decide whether to close the connection or not.
if (!HttpUtil.isKeepAlive(request)) { if (!HttpUtil.isKeepAlive(request)) {
lastContentFuture.addListener(new ClosingChannelFutureListener(raf)); lastContentFuture.addListener(new ClosingChannelFutureListener(raf));

View file

@ -39,6 +39,8 @@ public interface Handler {
private final int skip; private final int skip;
private final Handler handler; private final Handler handler;
private HttpURLConnection connect = null;
public Downloader(URL url, File file) { public Downloader(URL url, File file) {
this.requestProps = new HashMap<>(requestClient); this.requestProps = new HashMap<>(requestClient);
this.file = file; this.file = file;
@ -128,6 +130,7 @@ public void downloadFile() throws IOException {
interrupted.set(false); interrupted.set(false);
if (url.getProtocol().equalsIgnoreCase("http")) { if (url.getProtocol().equalsIgnoreCase("http")) {
HttpURLConnection connect = (HttpURLConnection) (url).openConnection(); HttpURLConnection connect = (HttpURLConnection) (url).openConnection();
this.connect = connect;
if (method != null) connect.setRequestMethod(method); if (method != null) connect.setRequestMethod(method);
for (Map.Entry<String, String> ent : requestProps.entrySet()) { for (Map.Entry<String, String> ent : requestProps.entrySet()) {
connect.setRequestProperty(ent.getKey(), ent.getValue()); connect.setRequestProperty(ent.getKey(), ent.getValue());
@ -158,6 +161,7 @@ public void downloadFile() throws IOException {
} }
} else { } else {
HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection(); HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection();
this.connect = connect;
if (method != null) connect.setRequestMethod(method); if (method != null) connect.setRequestMethod(method);
for (Map.Entry<String, String> ent : requestProps.entrySet()) { for (Map.Entry<String, String> ent : requestProps.entrySet()) {
connect.setRequestProperty(ent.getKey(), ent.getValue()); connect.setRequestProperty(ent.getKey(), ent.getValue());
@ -200,5 +204,9 @@ public void run() {
this.ex.set(ex); this.ex.set(ex);
LogHelper.error(ex); LogHelper.error(ex);
} }
if (connect != null)
try {
connect.disconnect();
} catch (Throwable ignored) { }
} }
} }

View file

@ -1,34 +0,0 @@
package ru.gravit.utils.downloader;
import java.io.File;
import java.net.URL;
public class DownloadingThread extends Thread {
private final Downloader runnable;
public DownloadingThread(File file, URL url, String name) {
super(name);
runnable = new Downloader(url, file);
}
public Downloader getDownloader() {
return runnable;
}
@Override
public void interrupt() {
runnable.interrupt.set(true);
while (!runnable.interrupted.get()) {
}
super.interrupt();
}
public void hardInterrupt() {
super.interrupt();
}
@Override
public void run() {
runnable.run();
}
}