从 Servlet 下载文件的 HttpURLConnection 不工作,直接 link 工作

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

有 8,000 多个 HTTP links 到 PDF 文件的列表可供下载。读取每个 link 并调用我的方法 downloadFile() 并将其保存到本地 Window$ PC。遇到 2 link 种格式:

  • http://example.com/docs/pdfs/Downloadable.pdf
  • http://example.com/docs/download.do?AttchmentId=2000

    第一种(直接)总是有效。第二个不起作用。将文件另存为 pdf 时,它看起来像:

    <div id="error">
        <ul>
        </ul>
    </div>
    <form id="download" name="download" method="post" action="/careManager/DownloadFormController.do?AttachmentId=2000">
    <input type="hidden" name="attachID" value="2000" >  
    </form>
    <script language="Javascript" type="text/javascript">
     document.forms[0].submit();
    </script>
    </body>
    </div>  
    

    当我在浏览器的开发人员工具中关注无效的 links 时,它被 javascript 文件重定向到 HTTPS 站点(将协议更改为 HTTPS)。

    我错过了什么?

    我已尝试设置 cookiehandler,将系统 属性 http.strictPostRedirect 设置为 true,将连接 setFollowRedirects 和 setInstanceFollowRedirects 设置为 true,如果 [=30] 创建一个新的 URL 连接=],设置连接 setReadTimeout,为 SSL 创建 HttpsURLConnection。所有都没有为 servlet 工作。

    public static void downloadFile(String downloadUrl, String fileName) throws Exception {
    
        CookieHandler.setDefault( new CookieManager( null, CookiePolicy.ACCEPT_ALL ) );
        // String cookie = CookieManager.getInstance().getCookie( downloadUrl.toString() );
        URL url  = new URL( downloadUrl );
        File file = new File( "C:\temp\smc1\" + fileName );
    
        HttpURLConnection c = (HttpURLConnection) url.openConnection();
        System.setProperty("http.strictPostRedirect", "true");
    
        int responseCode = c.getResponseCode();
        InputStream is;
    
        if( responseCode == HttpURLConnection.HTTP_MOVED_PERM
                || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
                || responseCode == HttpURLConnection.HTTP_SEE_OTHER ) {
    
            // Get new URL (https) from HttpURLConnection frowarding
            URL newUrl = new URL( c.getHeaderField("Location") );
            HttpURLConnection sc = (HttpURLConnection) newUrl.openConnection();
    
            sc.setFollowRedirects(true);
            sc.setInstanceFollowRedirects(true);
            responseCode = sc.getResponseCode();
            // sc.setReadTimeout(15*1000);
    
            is = sc.getInputStream();
        } else {
            c.setFollowRedirects(true);
            c.setInstanceFollowRedirects(true);
            responseCode = c.getResponseCode();
            is = c.getInputStream();
        }
        // System.out.println( " Code: " + responseCode );
    
        FileOutputStream fos = new FileOutputStream( file );
    
        int bytesRead;
        byte[] buffer = new byte[ 1024 ];
        while( ( bytesRead = is.read(buffer) ) != -1 ) {
            fos.write(buffer, 0, bytesRead);
        }
    
        if( fos != null ) {
            fos.flush();
            fos.close();
        }
        if( is != null ) {
            is.close();
        }
    }  
    

    我是 servlet 的使用者,只有 link 作为访问权限。 提前致谢!

  • 永远不会 工作,无论你写多少额外的代码来做 Java 在重定向时默认已经做的事情。

    HTML 页面自动发布一个表单,该表单在浏览器加载时会导致下载。 Java 代码永远不会执行那个。

    我通过以下方式解决了我的问题:

    1. 编辑默认浏览器的默认下载位置
    2. 开发循环以使用 url 启动浏览器并在默认位置下载每个文件

    我的默认浏览器是 chrome。我还观察了打开多少个选项卡以自动关闭浏览器并启动一个新选项卡。