用java编写下载.gz文件的程序?
我一直在尝试用java编写一个程序,可以为我在线从ftp服务器下载大量的.gz文件。以下是我一直在使用的:用java编写下载.gz文件的程序?,java,Java,我一直在尝试用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(); Bu
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的无休止循环,以此类推
当我手动下载这个文件时,它会解压,一切都很好,所以我知道这个文件没有问题
如有任何建议,将不胜感激!非常感谢你 当我打印出您下载的文件内容时,它显示为
-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
不是很漂亮,但会完成任务的
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();
}
在我看来,您正在下载整个文件夹并将其保存为gzip文件。你不应该把服务器上的文件名放在那里的某个地方吗?如果你的问题得到了回答,请标出正确的问题。