我们有一个带有 Kerberos 身份验证的站点。我以前从未使用过 Java 和 Eclipse,因此我的知识非常有限。
我希望实现的目标就像我对 python 所做的那样:
def DownloadFileFromSite(url):
FileName = ''
if 'download' not in url:
print ('ERROR: Change the following url to the proper download link.\n' + url)
else:
try:
# Disable SSL warnings
requests.packages.urllib3.disable_warnings()
kerberos_auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
response = requests.get(url, auth=kerberos_auth)
response.raise_for_status() # Check for exceptions
except:
print ('ERROR: HTTP Connection error. Check the network connection, and the inside links in CQ.')
# If the response has Content-Disposition, we take file name from it
if 'content-disposition' in response.headers:
FileName = re.findall("filename=(.+)", response.headers['content-disposition'])
if FileName:
FileName = FileName[0]
# if we were redirected, we take the real file name from the final URL
else:
FileName = re.search(r'/([^/;]+)(;.*)?$', response.url)
if FileName:
FileName = FileName.group(1)
# Save the downloaded file to C:\temp
if FileName:
try:
with open(strWorkspace + '\\' + FileName, 'wb') as DownloadedFile:
DownloadedFile.write(response.content)
except:
print ('\nError occured while saving downloaded contents from site.\n')
else:
print ('ERROR: ' + url)
我想在 Java 中实现这一点,但是我不知道如何开始。我看过一些例子,但我是 Java 的新手,我不知道如何处理它们。