Note that there are some explanatory texts on larger screens.

plurals
  1. POJava 7 URL Connection Fail
    text
    copied!<p>The following code used to work fine under Java 6 (and earlier) but it stopped working after updating to JRE 7 (Java 7).</p> <p>The URL is an FTP file:</p> <blockquote> <p><a href="ftp://ftp-private.ncbi.nlm.nih.gov/pubchem/.fetch/96/4133257873201306969.sdf.gz" rel="nofollow">ftp://ftp-private.ncbi.nlm.nih.gov/pubchem/.fetch/96/4133257873201306969.sdf.gz</a></p> </blockquote> <p>Here is the output I get:</p> <blockquote> <p>application/octet-stream -1 [Ljava.lang.StackTraceElement;@5419f97c</p> </blockquote> <p>And here is my code:</p> <pre><code>public static void store(URL url, File targetFile){ try { System.out.println(url); URLConnection uc = url.openConnection(); String contentType = uc.getContentType(); System.out.println(contentType); int contentLength = uc.getContentLength(); System.out.println(contentLength); Settings.setDownloadSize(contentLength); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); } InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; StatusPanel.updateProgrssBar(bytesRead); int offset = 0; while (offset &lt; contentLength) { bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) { break; } offset += bytesRead; StatusPanel.updateProgrssBar(offset); } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } FileOutputStream out = new FileOutputStream(targetFile); out.write(data); out.flush(); out.close(); //StatusPanel.setStatus("File has been stored at " + targetFile.toString()); //System.out.println("file has been stored at " + targetFile.toString()); } </code></pre> <p>The content length returns -1: </p> <pre><code>Area: API: Networking Synopsis: Server Connection Shuts Down when Attempting to Read Data When http Response Code is -1 </code></pre> <p>How do I make this code compatible with Java 7?</p> <p>Description: As a result of the bug fix for CR 6886436, the HTTP protocol handler will close the connection to a server that sends a response without a valid HTTP status line. When this occurs, any attempt to read data on that connection results in an IOException. </p> <p>For example, the following code is problematic:</p> <pre><code>public static void test () throws Exception { ..... HttpURLConnection urlc = (HttpURLConnection)url.openConnection(); .... System.out.println ("Response code: " + urlc.getResponseCode()); /** Following line throws java.io.IOException: Invalid Http response * when Response Code returned was -1 */ InputStream is = urlc.getInputStream(); // PROBLEMATIC CODE </code></pre> <p>To work around this problem, check the return value from the getResponseCode method and deal with a -1 value appropriately; perhaps by opening a new connection, or invoking getErrorStream on the stream. Nature of incompatibility: behavioral RFE: 7055058</p> <p>The problem is definitely with <code>getContentLength()</code> method.</p> <p>With JRE6, this method returns a value, but with JRE7 I get -1.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload