根据指定得url下载到指定的文件夹里面 ,读取压缩文件和根据压缩文件名称删除指定的压缩文件(读取文件指定内容)
/**
* 根据指定得url 下载到指定的文件夹里面
* @param fileUrl 下载路径 http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=trade&userId=20880026712448620156&fileType=csv.zip&bizDates=20191014&downloadFileName=20880026712448620156_20191014.csv.zip&fileId=%2Ftrade%2F20880026712448620156%2F20191014.csv.zip×tamp=1571109766&token=89cc5a6058859cb1384390481184dc22
* @param savePath 存放地址 D:/测试对账单下载
* @return
*/
public static void downloadFile(String fileUrl, String savePath) {
FileOutputStream fos = null;
InputStream inputStream = null;
try {
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为30秒
conn.setConnectTimeout(30 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
String downloadFileName = System.currentTimeMillis() + ".zip";//根据URL下载到指定的文件夹然后给下载的文件起个名字
File file = new File(saveDir + File.separator + downloadFileName);
fos = new FileOutputStream(file);
fos.write(getData);
System.out.println("info:" + url + " download success");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
/**
* 读取压缩文件里面的文件指定的内容
* @param url 要读取文件的地址 D:/测试对账单下载/xxxxxxx_xxxxxxx.csv.zip
* @return
*/
public static List<Map<String, Object>> readStatement(String url) {
File file = new File(url);
//获取文件输入流
ZipInputStream zipInputStream = null;
FileInputStream input = null;
List<Map<String, Object>> list = new ArrayList<>();
try {
input = new FileInputStream(url);
//获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
//定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
ZipEntry ze = null;
//循环遍历
while ((ze = zipInputStream.getNextEntry()) != null) {
String[] split = ze.getName().split("_");
if (split.length == 3) {
if ("业务明细".equals(split[2].substring(0, split[2].lastIndexOf(".")))) {
// System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream, Charset.forName("GBK")));
String line;
//内容不为空,输出
while ((line = br.readLine()) != null) {
System.err.println("读取出来得数据是:" + line.trim());
//下面是读取读取文件的部分指定内容,读出来的文件按以数字开头并且开头不可以是“#”号,才满足我们的条件
if (isNumbers(line.trim()) && !line.trim().substring(0, 1).contains("#")) {
Map<String, Object> map = new HashMap<>();
String[] abc = line.trim().split(",");//按逗号分隔
map.put("alipay_transaction_number", abc[0]); //支付宝交易号
map.put("merchant_order_number", abc[1]); //商户订单号
map.put("business_type", abc[2]); //业务类型
map.put("trade_name", abc[3]); //商品名称
map.put("create_time", abc[4]); //创建时间
map.put("completion_time", abc[5]); //完成时间
map.put("shop_number", abc[6]); //门店编号
map.put("name_of_number", abc[7]); //门店名称
map.put("operator", abc[8]); //操作员
map.put("terminal_number", abc[9]); //终端号
map.put("counter_account", abc[10]); //对方账户
map.put("order_amount", abc[11]); //订单金额
map.put("merchant_receipt ", abc[12]); //商家实收
map.put("alipay_red_envelope", abc[13]); //支付宝红包
map.put("ji_bao_bao", abc[14]); //集分宝
map.put("alipay_discount", abc[15]); //支付宝优惠
map.put("merchant_preferences", abc[16]); //商家优惠
map.put("name_of_coupon", abc[17]); //券名称
map.put("business_red_packet_consumption ", abc[18]); //商家红包消费
map.put("card_consumption_amount", abc[19]); //卡消费金额
map.put("refund_batch_no", abc[20]); //退款批次号
map.put("service_charge", abc[21]); //服务费
map.put("share_in_the_benefit_or_profit ", abc[22]); //分润
map.put("remarks", abc[23]); //备注
list.add(map);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
try {
zipInputStream.closeEntry();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
JSONArray jsonObject = JSONArray.fromObject(list);
System.err.println(jsonObject);
String savePath = "D:\\测试对账单下载";
if (!deleteDir(savePath ,file.getName() )) {
System.err.println("删除压缩文件失败。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
}
return list;
}
/**
* 根据文件名删除指定的压缩文件
* @param name 要删除的文件名字 1234567890123.zip
* @param savePatha 文件所在的地址路径 D:\\测试对账单下载
* @return
*/
public static boolean deleteDir(String savePatha, String name) {
try {
int count = 0;
File dir = new File(savePatha);
File[] files = dir.listFiles();
if (null == files || files.length == 0) {
System.out.println("该目录下没有任何一个文件!");
return false;
}
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDir(files[i].getAbsolutePath(), name);
} else {
String strFileName = files[i].getAbsolutePath().toLowerCase();
if (files[i].isFile()) {
if (strFileName.endsWith(name.toLowerCase())) {
System.out.println("正在删除:" + strFileName);
boolean delete = files[i].delete();
if (!delete) {
System.err.println("删除失败。。。。。。。。。。。" + name);
return false;
}
count++;
}
}
}
}
System.err.println("共删除"+ count +"个文件");
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 判断是不是数字
* @param businesNumbers
* @return
*/
public static boolean isNumbers(String businesNumbers) {
Pattern pattern = Pattern.compile("[0-9].*");
Matcher isNum = pattern.matcher(businesNumbers.charAt(0) + "");
if (!isNum.matches()) {
return false;
}
return true;
}
/**
* 判断是不是文件
* @param path 文件地址
* @return
*/
public static LinkedList<File> isFile(String path){
LinkedList<File> linkedListisFile = null;
LinkedList<File> linkedLNotisFile = null;
try {
File file = new File(path);
if(file.exists()){
File[] listFiles = file.listFiles();
for (File listFile : listFiles) {
linkedListisFile = new LinkedList<File>();
linkedLNotisFile = new LinkedList<File>();
if(listFile.isDirectory()){
linkedLNotisFile.add(listFile);
}else{
linkedListisFile.add(listFile);
}
}
File file_temo;
while (!linkedLNotisFile.isEmpty()){
linkedListisFile = new LinkedList<File>();
linkedLNotisFile = new LinkedList<File>();
file_temo = linkedLNotisFile.removeFirst();
File[] listFiles1 = file_temo.listFiles();
for (File file1 : listFiles1) {
if(file1.isDirectory()){
linkedLNotisFile.add(file1);
}else{
linkedListisFile.add(file1);
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return linkedListisFile;
}
posted on 2019-11-19 09:17 UnmatchedSelf 阅读(428) 评论(0) 编辑 收藏 举报