diff options
-rw-r--r-- | src/main/java/org/openslx/taskmanager/tasks/DownloadText.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java index 098b284..bf185bd 100644 --- a/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java +++ b/src/main/java/org/openslx/taskmanager/tasks/DownloadText.java @@ -2,7 +2,8 @@ package org.openslx.taskmanager.tasks; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; @@ -33,6 +34,28 @@ public class DownloadText extends AbstractTask return true; } + private String getError( URLConnection connection, boolean close ) + { + if ( ! ( connection instanceof HttpURLConnection ) ) + return null; + + StringBuilder sb = new StringBuilder(); + InputStream es = ( (HttpURLConnection)connection ).getErrorStream(); + final byte data[] = new byte[ 9000 ]; + try { + while ( es.read( data, 0, data.length ) != -1 ) { + if ( sb.length() < 1024 ) { + sb.append( new String( data, StandardCharsets.UTF_8 ) ); + } + } + } catch ( Exception e ) { + } + if ( close ) { + Util.multiClose( es ); + } + return sb.toString(); + } + @Override protected boolean execute() { @@ -61,10 +84,11 @@ public class DownloadText extends AbstractTask } status.content = baos.toString( StandardCharsets.UTF_8.name() ); return true; - } catch ( IOException e ) { - status.error = "Download error: " + e.toString(); + } catch ( Exception e ) { + status.error = "Download error: " + e.toString() + "\n" + getError( connection, false ); return false; } finally { + getError( connection, true ); Util.multiClose( in ); } } |