使用Java在密码保护的站点上下载大文件时出现问题

时间:2011-03-13 23:02:16

标签: java security

我已经能够通过使用java的密码验证器类连接并从受密码保护的站点下载文本和图像等文件并覆盖它。但是现在我遇到从同一站点下载zip文件的问题。 我认为它可能与尺寸有关,因为拉链大约是3 MB,而图像大小约为30KB。因为我可以通过我的网络浏览器下载它们,所以应该使用相同的用户名和密码,这应该不是问题。没有登录名和密码,拉链下载得很好,所以我不认为它与我的下载代码有关。

我的理论是存在某种身份验证超时或与较大文件相关的内容,或者在下载了一定数量的信息之后存在某种重新查询。然而,这是我第一次处理这个问题,所以我不知道究竟是什么问题或如何解决它。任何帮助表示赞赏。

以下是相关代码:

  private class mAuthenticator extends Authenticator {
  private int numtries = 0;
  protected PasswordAuthentication getPasswordAuthentication(){
        String promptString = getRequestingPrompt();
        String hostname = getRequestingHost();
         InetAddress ipaddr = getRequestingSite();
         int port = getRequestingPort();
         String username = "foo";
         String password = "bar";
         return new PasswordAuthentication(username, password.toCharArray());
  }
 }



public String DownloadFile(String url, String placetostore, String filename){
String mString = null;
URL myURL = 
new URL(url); 
URLConnection ucon = myURL.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = 
new BufferedInputStream(is); 
ByteArrayBuffer baf = 
new ByteArrayBuffer(50); 
int current = 0; 
while((current = bis.read()) != -1){ 
baf.append((
byte)current); 
}
String fileloc = placetostore + filename;
File file = 
new File(fileloc); 
FileOutputStream fos = 
new FileOutputStream(file); 
fos.write(baf.toByteArray());
fos.close();
} 
catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace();
}
return mString; 
}

1 个答案:

答案 0 :(得分:1)

  

我的理论是存在某种身份验证超时或与较大文件相关的内容......

它不会是身份验证超时。除非服务器端以最特殊的方式实现,否则在服务器开始向您的GET请求发送响应之前检查身份验证...然后不再重复。

但是文件传递可能会有一个简单的超时。如果您的客户端读取文件的时间过长,则服务器可能只关闭该流。 (可以采取这种方式来节省服务器端资源,防止意外或故意的DoS攻击等)。

用户/密码与非密码之间的行为差​​异可能是使用TLS,并且服务器配置了不同的超时以用于不同的邮件传输。

  

......或者在下载了一定数量的信息之后会有某种重新查询。

这是不可能的。该文件在单个HTTP响应消息中传递,并且HTTP没有在响应中途重新进行身份验证的条款。


如果您发布了客户端下载代码,以便我们可以看到您是否做了一些不理想的事情,那将是一个好主意。