Java 如何从一个超链接下载多个文件
我正在执行一项任务,需要从一个超链接下载多个文件。当我调用一个API链接时,我希望它返回多个文件 我当前的代码仅下载一个文件:Java 如何从一个超链接下载多个文件,java,android,http,Java,Android,Http,我正在执行一项任务,需要从一个超链接下载多个文件。当我调用一个API链接时,我希望它返回多个文件 我当前的代码仅下载一个文件: try { URL url = new URL(f_url[0]); URLConnection conection = url.openConnection(); conection.connect(); // getting file length int lenghtOfFile = conection.getContent
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
看看这个。它下载多个文件并在屏幕上显示其内容我不熟悉任何允许您在一个响应中发送多个文件的HTTP协议,除非它们已压缩。我很好奇,服务器响应是什么样子的?@323go这会返回一个流对象/文件的多个列表它遵循什么标准?@323go我没有告诉你标准是什么意思?HTTP是一个标准通信协议。你发送一个请求,你就会得到一个响应。请求和响应是定义良好的。此请求->多响应场景遵循什么标准?它的定义是什么?我对HTTP的了解并不全面,但我从未见过这种情况。如果它是HTTP协议的专有扩展,则除非您能够定义服务器,否则在实现客户机方面很可能得不到帮助。