Download Pictures - Writing Error Stream to log if Request is not successful.

This commit is contained in:
LevelX2 2014-05-12 15:29:25 +02:00
parent 765652b74b
commit be9da26100

View file

@ -29,9 +29,11 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -429,6 +431,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
if (url != null) { if (url != null) {
Logger.getLogger(this.getClass()).info(url);
Runnable task = new DownloadTask(card, new URL(url), cardsToDownload.size()); Runnable task = new DownloadTask(card, new URL(url), cardsToDownload.size());
executor.execute(task); executor.execute(task);
} else { } else {
@ -460,11 +463,16 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
closeButton.setText("Close"); closeButton.setText("Close");
} }
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
private final class DownloadTask implements Runnable { private final class DownloadTask implements Runnable {
private CardDownloadData card; private final CardDownloadData card;
private URL url; private final URL url;
private int count; private final int count;
public DownloadTask(CardDownloadData card, URL url, int count) { public DownloadTask(CardDownloadData card, URL url, int count) {
this.card = card; this.card = card;
@ -497,10 +505,15 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
return; return;
} }
BufferedOutputStream out;
BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream()); // Logger.getLogger(this.getClass()).info(url.toString());
BufferedOutputStream out = new BufferedOutputStream(new TFileOutputStream(temporaryFile)); URLConnection httpConn = url.openConnection(p);
httpConn.connect();
if (((HttpURLConnection) httpConn).getResponseCode() == 200) {
try (BufferedInputStream in = new BufferedInputStream(((HttpURLConnection) httpConn).getInputStream())) {
//try (BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream())) {
out = new BufferedOutputStream(new TFileOutputStream(temporaryFile));
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
while ((len = in.read(buf)) != -1) { while ((len = in.read(buf)) != -1) {
@ -515,7 +528,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
out.write(buf, 0, len); out.write(buf, 0, len);
} }
in.close(); }
out.flush(); out.flush();
out.close(); out.close();
@ -540,6 +553,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
new TFile(temporaryFile).cp_rp(outputFile); new TFile(temporaryFile).cp_rp(outputFile);
temporaryFile.delete(); temporaryFile.delete();
} }
} else {
Logger.getLogger(this.getClass()).error(convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
}
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); log.error(e, e);
@ -550,6 +566,8 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
} }
private void writeImageToFile(BufferedImage image, TFile file) throws IOException { private void writeImageToFile(BufferedImage image, TFile file) throws IOException {
Iterator iter = ImageIO.getImageWritersByFormatName("jpg"); Iterator iter = ImageIO.getImageWritersByFormatName("jpg");