mirror of
https://github.com/magefree/mage.git
synced 2025-12-23 03:51:58 -08:00
Download Pictures - Writing Error Stream to log if Request is not successful.
This commit is contained in:
parent
765652b74b
commit
be9da26100
1 changed files with 58 additions and 40 deletions
|
|
@ -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 {
|
||||||
|
|
@ -459,12 +462,17 @@ 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,48 +505,56 @@ 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();
|
||||||
byte[] buf = new byte[1024];
|
if (((HttpURLConnection) httpConn).getResponseCode() == 200) {
|
||||||
int len;
|
try (BufferedInputStream in = new BufferedInputStream(((HttpURLConnection) httpConn).getInputStream())) {
|
||||||
while ((len = in.read(buf)) != -1) {
|
//try (BufferedInputStream in = new BufferedInputStream(url.openConnection(p).getInputStream())) {
|
||||||
// user cancelled
|
out = new BufferedOutputStream(new TFileOutputStream(temporaryFile));
|
||||||
if (cancel) {
|
byte[] buf = new byte[1024];
|
||||||
in.close();
|
int len;
|
||||||
out.flush();
|
while ((len = in.read(buf)) != -1) {
|
||||||
out.close();
|
// user cancelled
|
||||||
temporaryFile.delete();
|
if (cancel) {
|
||||||
return;
|
in.close();
|
||||||
}
|
out.flush();
|
||||||
out.write(buf, 0, len);
|
out.close();
|
||||||
}
|
temporaryFile.delete();
|
||||||
|
return;
|
||||||
in.close();
|
}
|
||||||
out.flush();
|
out.write(buf, 0, len);
|
||||||
out.close();
|
|
||||||
|
|
||||||
if (card.isTwoFacedCard()) {
|
|
||||||
BufferedImage image = ImageIO.read(temporaryFile);
|
|
||||||
if (image.getHeight() == 470) {
|
|
||||||
BufferedImage renderedImage = new BufferedImage(265, 370, BufferedImage.TYPE_INT_RGB);
|
|
||||||
renderedImage.getGraphics();
|
|
||||||
Graphics2D graphics2D = renderedImage.createGraphics();
|
|
||||||
if (card.isTwoFacedCard() && card.isSecondSide()) {
|
|
||||||
graphics2D.drawImage(image, 0, 0, 265, 370, 313, 62, 578, 432, null);
|
|
||||||
} else {
|
|
||||||
graphics2D.drawImage(image, 0, 0, 265, 370, 41, 62, 306, 432, null);
|
|
||||||
}
|
}
|
||||||
graphics2D.dispose();
|
|
||||||
writeImageToFile(renderedImage, outputFile);
|
}
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
if (card.isTwoFacedCard()) {
|
||||||
|
BufferedImage image = ImageIO.read(temporaryFile);
|
||||||
|
if (image.getHeight() == 470) {
|
||||||
|
BufferedImage renderedImage = new BufferedImage(265, 370, BufferedImage.TYPE_INT_RGB);
|
||||||
|
renderedImage.getGraphics();
|
||||||
|
Graphics2D graphics2D = renderedImage.createGraphics();
|
||||||
|
if (card.isTwoFacedCard() && card.isSecondSide()) {
|
||||||
|
graphics2D.drawImage(image, 0, 0, 265, 370, 313, 62, 578, 432, null);
|
||||||
|
} else {
|
||||||
|
graphics2D.drawImage(image, 0, 0, 265, 370, 41, 62, 306, 432, null);
|
||||||
|
}
|
||||||
|
graphics2D.dispose();
|
||||||
|
writeImageToFile(renderedImage, outputFile);
|
||||||
|
} else {
|
||||||
|
new TFile(temporaryFile).cp_rp(outputFile);
|
||||||
|
}
|
||||||
|
temporaryFile.delete();
|
||||||
} else {
|
} else {
|
||||||
new TFile(temporaryFile).cp_rp(outputFile);
|
new TFile(temporaryFile).cp_rp(outputFile);
|
||||||
|
temporaryFile.delete();
|
||||||
}
|
}
|
||||||
temporaryFile.delete();
|
|
||||||
} else {
|
} else {
|
||||||
new TFile(temporaryFile).cp_rp(outputFile);
|
Logger.getLogger(this.getClass()).error(convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
|
||||||
temporaryFile.delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -549,7 +565,9 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
|
||||||
update(cardIndex + 1, count);
|
update(cardIndex + 1, count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue