网址:http://www.teamliquid.net/replay/download.php?replay=1830是指向.rep文件的下载链接。
我的问题是:如何在java中下载这个内容,知道原始rep文件的名称,以便用定义的前缀保存它,比如path / _.rep
//我试图从java运行wget但是我没有看到如何获取原始文件的名称。
答案 0 :(得分:1)
获取重定向的网址
http://www.teamliquid.net/replay/upload/coco%20vs%20snssoflsekd.rep
您可以从此网址获取文件名。
获取重定向的URL很棘手。请参阅我对如何使用Apache HttpClient 4进行此问题的回答,
HttpClient 4 - how to capture last redirect URL
编辑:以下是使用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