Sunday, 15 September 2013

java - HttpURLConnection to download file from Servlet not working, direct link works -


After

PDF files have a list of 8,000+ HTTP links for download. Reading the file and downloading my method in each link () and saving it to a local window on $ PC. Coming in 2 link formats:

  • The first type (direct) always works. The second does not work

      & lt ; Div id = "error" & gt ;: When it saves the file as a PDF, it looks like & lt; Ul & gt; & Lt; / Ul & gt; & Lt; / Div & gt; & Lt; Form ID = "Downloads" name = "download" method = "post" action = "? / CareManager / DownloadFormController.do AttachmentId = 2000" & gt; & Lt; Input type = "hidden" name = "attachid" value = "2000" & gt; & Lt; / Form & gt; & Lt; Script language = "javascript" type = "text / javascript" & gt; Document.forms [0] .submit (); & Lt; / Script & gt; & Lt; / Body & gt; & Lt; / Div & gt;  

    When I follow the link that does not work in my browser's developer tool, it is being redirected to a HTTPS site by javascript files (changing HTTPS protocol).

    What am I missing?

    I have tried to install cookiehandler, setting the property to http.strictPostRedirect true, setFollowRedirects connection and setting up trueInstanceFollowRedirects to true, create a new URL connection if forwarded / moved, the connection Setting up the setReadTimeout, creating a HttpsURL connection for SSL has not worked for all servlet.

      Public static zero download file (string downloadURL, string filename) throws exceptions {cookieheldler.set default (new cookie manager (empty, cookie polish.accepteel)); // string cookie = CookieManager.getInstance () getCookie (downloadUrl.toString ()). Url url = new url (download url); File file = new file ("C: \\ temp \\ smc1 \\" + fileName); HttpURLConnection c = (HttpURLConnection) url.openConnection (); System.setProperty ("http.strictPostRedirect", "true"); Int Response = c.getResponseCode (); InputStream is; If (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || Responsecode == HttpURLConnection.HTTP_SEE_OTHER) {// New URL new URL from the URL of the HTTP Connection Connection = new URL (c.getHeaderField (" place" )); HttpURL connection sc = (HttpURLConnection) newUrl.openConnection (); Sc.setFollowRedirects (true); Sc.setInstanceFollowRedirects (true); ResponseCode = sc.getResponseCode (); // sc.setReadTimeout (15 * 1000); Is = sc.getInputStream (); } And {c.setFollowRirectirects (true); C.setInstanceFollowRedirects (true); Responsecode = c.getResponseCode (); Is = c.getInputStream (); } // System.out.println ("Code:" + responsecode); FileOutputStream fos = New FileOutputStream (file); Int bytes read; Byte [] buffer = new byte [1024]; While ((Bytesrad = Reid (buffer))! = -1) {fos.write (buffer, 0, bytes read); } If (fos! = Null) {fos.flush (); Fos.close (); } If (is! = Null) is {. }}  

    I am the servlet's consumer and only link as access I thank you before time!

  • This ever will not work, no matter what the excess code you have Writes, that Java already does the default when redirecting.

    The HTML page automatically posts a form that causes a download when loaded by a browser. Java code will never execute it.


    No comments:

    Post a Comment