我正在尝试直接从远程URL读取zip文件 我试过这种方式
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class Utils {
public static void main(String args[]) throws Exception {
String ftpUrl = "http://wwwccc.zip";
URL url = new URL(ftpUrl);
unpackArchive(url);
}
public static void unpackArchive(URL url) throws IOException {
String ftpUrl = "http://www.vvvv.xip";
File zipFile = new File(url.toString());
ZipFile zip = new ZipFile(zipFile);
InputStream in = new BufferedInputStream(url.openStream(), 1024);
ZipInputStream zis = new ZipInputStream(in);
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("entry: " + entry.getName() + ", "
+ entry.getSize());
BufferedReader bufferedeReader = new BufferedReader(
new InputStreamReader(zip.getInputStream(entry)));
String line = bufferedeReader.readLine();
while (line != null) {
System.out.println(line);
line = bufferedeReader.readLine();
}
bufferedeReader.close();
}
}
}
我的异常是
Exception in thread "main" java.io.FileNotFoundException: http:\www.nseindia.com\content\historical\EQUITIES\2015\NOV\cm03NOV2015bhav.csv.zip (The filename, directory name, or volume label syntax is incorrect)
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at java.util.zip.ZipFile.<init>(Unknown Source)
at Utils.unpackArchive(Utils.java:30)
at Utils.main(Utils.java:19)
从浏览器运行时,zip文件的URL工作正常。
答案 0 :(得分:2)
HttpURLConnection
类不适用于远程文件。它仅支持本地文件系统上可用的文件。要在远程文件上打开流,您可以使用HttpURLConnection
。
在String url= "http://www.nseindia.com/content/historical/EQUITIES/2015/NOV/cm03NOV2015bhav.csv.zip";
InputStream is = new URL(url).openConnection().getInputStream();
实例上调用getInputStream()
以获取可以进一步处理的输入流。
示例:
{{1}}
答案 1 :(得分:2)
以上都不适合我。
做了什么 像魅力一样工作,是这样的:
InputStream inputStream = new URL( urlString ).openStream();
答案 2 :(得分:1)
使用
行File zipFile = new File(url.toString());
您正在尝试创建一个名为URL的文件,其中包含不允许使用的字符。
该文件的名称应该更简单,如
File zipFile = new File("zipfile.csv.zip");
编译器告诉你,以及:
(文件名,目录名或卷标语法不正确)
我确信这就是你收到错误的原因。但我不确定其余的代码。
答案 3 :(得分:0)
读取存储在远程位置的Zip文件。只需在下面的代码库中更改您的远程位置的zip文件详细信息即可。
import java.io.*;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ReadZipFileFromRemote{
public static void main(String args[]){
String url="https://test-po.s3.ap-south-1.amazonaws.com/dev/coding/76/55/14/1/1587736321256.zip";
String content=readZipFileFromRemote(url);
System.out.println(content);
}
public String readZipFileFromRemote(String remoteFileUrl) {
StringBuilder sb = new StringBuilder();
try {
URL url = new URL(remoteFileUrl);
InputStream in = new BufferedInputStream(url.openStream(), 1024);
ZipInputStream stream = new ZipInputStream(in);
byte[] buffer = new byte[1024];
ZipEntry entry;
while ((entry = stream.getNextEntry()) != null) {
int read;
while ((read = stream.read(buffer, 0, 1024)) >= 0) {
sb.append(new String(buffer, 0, read));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
答案 4 :(得分:-2)
你不能像这样从url创建文件 试试这个:
网址ftpUrl =新网址(“http://www.nseindia.com/content/historical/EQUITIES&gt; /2015/NOV/cm03NOV2015bhav.csv.zip”);
文件zipFile =新文件(“本地驱动器上的某个位置”);
FileUtils.copyURLToFile(ftpUrl,zipFile);
ZipFile zip = new ZipFile(zipFile);