我一直在试着用java写一个程序,它可以帮我从一个在线的ftp服务器上下载一些.gz文件。这是我一直在使用的:
public static void main(String[] args) throws IOException {
URL url = new URL("ftp://ftp.ncbi.nih.gov/pub/geo/DATA/SOFT/by_series/GSE10/");
URLConnection con = url.openConnection();
BufferedInputStream in = new BufferedInputStream(con.getInputStream());
FileOutputStream out = new FileOutputStream("GSE10_family.soft.gz");
int i = 0;
byte[] bytesIn = new byte[3000000];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
in.close();
}程序可以正常运行并下载文件。但是,当我尝试解压缩.gz文件时,它会解压缩为.cpgz文件,这会导致.cpgz到.gz的无限循环,以此类推。
当我手动下载这个文件时,它会解压,一切都很好,所以我知道这不是文件的问题。
如有任何建议,我们将不胜感激!非常感谢!
发布于 2011-06-09 23:25:08
当我打印出您下载的文件的内容时,它显示为
-r--r--r-- 1 ftp anonymous 2311751 Jan 29 16:45 GSE10_family.soft.gz如果我下载文件而不是目录,我会得到一个gzip文件。
$ gunzip GSE10_family.soft.gz
$ ls -l GSE10_family.soft
----------+ 1 peter None 7698802 2011-06-09 16:24 GSE10_family.soft发布于 2019-12-05 05:48:57
不是很漂亮,但会完成工作的
void downloadGzip(String dir, String url, String user, String pwd) throws Exception {
//Authentication
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pwd);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(new URI(url).getHost(), AuthScope.ANY_PORT), creds);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
CloseableHttpResponse response = HttpClientBuilder.create().build().execute(new HttpGet(url), context);
//get remote file name
Header[] headers = response.getHeaders("Content-Disposition");
String file = headers[0].getValue().substring(headers[0].getValue().lastIndexOf("=") + 1);
String out = dir + file.substring(0, file.indexOf(".gz"));
GzipCompressorInputStream stream = new GzipCompressorInputStream(response.getEntity().getContent());
FileUtils.copyInputStreamToFile(stream, new File(out));
stream.close();
}https://stackoverflow.com/questions/6295220
复制相似问题