从HTTPS URL下载大文件时出现Java问题

时间:2013-12-26 14:34:40

标签: java apache tomcat ssl ssl-certificate

我在从“HTTPS URL”下载文件(超过200mb大小)时遇到问题。 问题还在于认证和下载问题。我也使用了 TrustManager SSL安全等代码。但问题仍然存在,主要是因为我无法通过证书。

例如:我的HTTPS网址为: - https://v1/xyz/abc.avi。现在我想下载此文件但无法下载。从我的应用程序的以下代码下载小文件: -

.....if (contentLength > 0) {  
       try {  
          InputStream in = urlConn.getInputStream();  
          while ( (ch = in.read()) != -1) {  
              rec.append((char)ch);  
          }  
      } ....

请帮助。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码下载:

private void downloadFile() throws Exception {

        URL fileUrl = new URL("https://.....");
        URLConnection urlConnection = fileUrl.openConnection();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());

        FileOutputStream fos = new FileOutputStream("c:/file.avi");
        BufferedOutputStream dest = new BufferedOutputStream(fos);

        int count;
        byte data[] = new byte[2048];
        while((count = bufferedInputStream.read(data)) != -1) {
            dest.write(data, 0, count);
        }

        dest.flush();
        dest.close();
    }