Java 如何下载嵌入到网站中的文件?
目前,我只能下载具有这种格式的文件: 但是如何下载url文件中不可见的文件? 例如,Skype的url路径: 正如你们所看到的,我无法使用Java 如何下载嵌入到网站中的文件?,java,httpurlconnection,Java,Httpurlconnection,目前,我只能下载具有这种格式的文件: 但是如何下载url文件中不可见的文件? 例如,Skype的url路径: 正如你们所看到的,我无法使用 filePath.subString(filePath.lastIndexOf("/") + 1); 那么还有其他的方法吗?我确实使用FireBug找到了嵌入在页面中的文件,FireBug是 我的问题是,我可以通过编程方式浏览页面并访问该文件吗 下面是代码,可以很好地下载 public static void fileDownload(String
filePath.subString(filePath.lastIndexOf("/") + 1);
那么还有其他的方法吗?我确实使用FireBug找到了嵌入在页面中的文件,FireBug是
我的问题是,我可以通过编程方式浏览页面并访问该文件吗
下面是代码,可以很好地下载
public static void fileDownload(String urlFile) throws IOException {
URL url = new URL(urlFile);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpURLConnection.getHeaderField("Content-Disposition");
String contentType = httpURLConnection.getContentType();
int contentLength = httpURLConnection.getContentLength();
if (disposition != null) {
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
} else {
fileName = urlFile.substring(urlFile.lastIndexOf("/") + 1, urlFile.length());
}
System.out.println("Content-type= " + contentType);
System.out.println("Disposition= " + disposition);
System.out.println("Content-length= " + contentLength);
System.out.println("File name= " + fileName);
InputStream inputStream = httpURLConnection.getInputStream();
String saveFilePath = getDesiredPath() + File.separator + fileName;
FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath);
int byteRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((byteRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, byteRead);
}
fileOutputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied httpCode=" + responseCode);
}
httpURLConnection.disconnect();
}
这是我第一次使用文件管理,这段代码实际上取自。如果页面中嵌入了文件下载链接,则可以下载文件 网页html中的类似内容:
. . .
<a href="Skype.exe">Download Skype</a>
. . .
你是在问如何跟踪重定向吗?@RC。是的,我很抱歉我没有术语。但这似乎是正确的。
Document doc = Jsoup.connect("http://example.com/").get();
Elements anchors = doc.select("a");
// Untested code
for (var anchor of anchors) // ECMA 6 (i think)
{
if (anchor.href.endsWith(".exe")
{
// if href is not full url i.e. not starting with http://
var downloadLink = url + anchor.href;
// Download the file with the about url
}
}