1

URL:http : //www.teamliquid.net/replay/download.php?replay=1830は、.repファイルへのダウンロードリンクです。

私の質問は、path / _。repのような定義されたプレフィックスで保存するために、元のrepファイルの名前を知っているJavaでこのコンテンツをダウンロードする方法です。

// Javaからwgetを実行しようとしましたが、元のファイルの名前を取得する方法がわかりません。

4

1 に答える 1

1

リダイレクトされたURLを取得します。

http://www.teamliquid.net/replay/upload/coco%20vs%20snssoflsekd.rep

このURLからファイル名を取得できます。

リダイレクトされたURLを取得するのは難しいです。Apache HttpClient 4でそれを行う方法については、この質問に対する私の回答を参照してください。

HttpClient4-最後のリダイレクトURLをキャプチャする方法

編集:これはHttpClient4.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
于 2010-05-13T16:41:51.963 に答える