下面有三个方法都是获取到zip里面的图片的inputStream,因为我只需要后去
目录
1http://xxxxxxxxxxxxxxx/xxxx.zip
2.http://xxxxxxxxxxxxxx/xxxxx 下载是个文件流
3.本地文件获取
两者的区别,第一种的比较慢,第二种比较快
因为第一种的.zip可以直接使用URLConnection来获取到文件
而第二种则需要通过http请求之后获取到文件内容所以比较慢
第一种.zip的下载链接
public static void downloadFile(String urlPath) throws Exception {
File file = null;
BufferedInputStream bin = null;
OutputStream out = null;
HttpURLConnection httpURLConnection = null;
try {
// 统一资源
URL url = new URL(urlPath);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setDoOutput(true);// http正文内,因此需要设为true, 默认情况下是false;
httpURLConnection.setDoInput(true);
int responseCode = httpURLConnection.getResponseCode();//查看请求状态
System.out.println(responseCode);
// // 设定请求的方法,默认是GET
// httpURLConnection.setRequestMethod("GET");
// 设置字符编码
httpURLConnection.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
httpURLConnection.connect();
// 文件大小
int fileLength = httpURLConnection.getContentLength();
System.out.println(fileLength);
//后面参数要字符编码要设置,不然可能会读不到
ZipInputStream zipI=new ZipInputStream(httpURLConnection.getInputStream(),Charset.forName("GBK"));
ZipEntry entry;
while((entry=zipI.getNextEntry())!=null) {
byte[] data = getByte(zipI); // 获取当前条目的字节数组
InputStream is = new ByteArrayInputStream(data); //获取到zip内容的InputStream 之后就可以爱干嘛干嘛了
FileUtil.filter(is, "pdf");
}
} catch (Exception e) {
// TODO Auto-generated catch block
// throw new CustomException(e.toString()+"文件下载异常,请检查下载链接$$"+urlPath);
} finally {
if (bin != null)
bin.close();
if (out != null)
out.close();
}
}
获取当前条目的字节数组
private static byte[] getByte(ZipInputStream zipI) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
byte[] buf = null;
int length = 0;
while ((length = zipI.read(temp, 0, 1024)) != -1) {
bout.write(temp, 0, length);
}
buf = bout.toByteArray();
bout.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//判断文件类型
public static boolean filter(InputStream in,String type) throws IOException {
byte[] b = new byte[3];
try {
in.read(b, 0, b.length);
} catch (IOException e) {
e.printStackTrace();
return false;
}
String xxx = bytesToHexString(b);
logger.error("文件类型++++=="+xxx);
xxx = xxx.toUpperCase();
if(type.equals(checkType(xxx))) {
return true;
}
return false;
}
private static String checkType(String xxxx) {
switch (xxxx) {
case "FFD8FF":
logger.debug("类型是:JPEG" );
return "photo";
case "89504E":
logger.debug("类型是:PNG");
return "photo";
case "255044":
logger.debug("类型是:PDF");
return "pdf";
default:
logger.debug("文件头:"+xxxx);
return null;
}
}
zip内容
程序运行结果
获取到了两个InputStream 都是pdf类型的
非zip后缀的处理方法
需要使用到的是
public static void downloadFile(String urlPath) throws Exception {
File file = null;
BufferedInputStream bin = null;
OutputStream out = null;
HttpURLConnection httpURLConnection = null;
try {
long startTime=System.currentTimeMillis();
ZipInputStream zipI=new ZipInputStream(new ByteArrayInputStream(AllRequest.GetStringtobyte(urlPath)),Charset.forName("GBK"));
long endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);
ZipEntry entry;
while((entry=zipI.getNextEntry())!=null) {
byte[] data = getByte(zipI); // 获取当前条目的字节数组
InputStream is = new ByteArrayInputStream(data);
FileUtil.filter(is, "pdf");
}
} catch (Exception e) {
// TODO Auto-generated catch block
// throw new CustomException(e.toString()+"文件下载异常,请检查下载链接$$"+urlPath);
} finally {
if (bin != null)
bin.close();
if (out != null)
out.close();
}
}
请求获取到byte
public static byte[] GetStringtobyte(String url) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("content-type", "application/json");
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
byte[] back = EntityUtils.toByteArray(httpEntity);
return back;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
107s
还有一种是本地文件读取使用FileZip
public static void main(String[] args) throws IOException {
ZipInputStream zi=new ZipInputStream(InputStream());
ZipFile zipFile = new ZipFile("D:\\AAAAA.zip",Charset.forName("GBK"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
FileUtil.filter(stream, "pdf");
}
}