[英]Downloading multimedia files of a directory into my local machine using java code
[英]Downloading multimedia content in java from php pages
網址: http : //www.teamliquid.net/replay/download.php? replay=1830是指向.rep文件的下載鏈接。
我的問題是:如何在Java中下載此內容(知道原始rep文件的名稱),以便使用已定義的前綴將其保存,例如path / _。rep
//我試圖從Java運行wget,但是我看不到如何獲取原始文件的名稱。
獲取重定向的URL,
http://www.teamliquid.net/replay/upload/coco%20vs%20snssoflsekd.rep
您可以從該URL獲取文件名。
獲取重定向的URL很棘手。 關於如何使用Apache HttpClient 4看到我對這個問題的回答,
編輯:這是使用HttpClient 4.0的示例,
String url = "http://www.teamliquid.net/replay/download.php?replay=1830";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpContext context = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpget, context);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
throw new IOException(response.getStatusLine().toString());
HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
String currentUrl = URLDecoder.decode(currentReq.getURI().toString(), "UTF-8");
int i = currentUrl.lastIndexOf('/');
String fileName = null;
if (i < 0) {
fileName = currentUrl;
} else {
fileName = currentUrl.substring(i+1);
}
OutputStream os = new FileOutputStream("/tmp/" + fileName);
InputStream is = response.getEntity().getContent();
byte[] buf = new byte[4096];
int read;
while ((read = is.read(buf)) != -1) {
os.write(buf, 0, read);
}
os.close();
運行這段代碼后,我得到了這個文件,
/tmp/coco vs snssoflsekd.rep
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.