<!-- jxls -->
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.12</version>
</dependency>
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
package com.use;
import com.google.common.base.Splitter;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.use.dao.InvitationDao;
import com.use.entity.Invitation;
import com.use.vo.ImportExcelVo;
import com.use.vo.LinkVo;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImportFileAddressTest {
Logger logger =LoggerFactory.getLogger(ImportFileAddressTest.class);
@Test
public void test() throws IOException {
Map<String,String> map =Maps.newHashMap();
ImportExcelVo vo=
new ImportExcelVo("job.xml","C:\\Users\\sonyic\\Desktop\\krairjob.xls","linkVoList",LinkVo.class);
List<LinkVo> voList =ImportExcelUtil.importData(vo);
List<String> urlList=voList.parallelStream().map(linkVo -> linkVo.getUrl()).collect(Collectors.toList());
// 遍历文件夹下的文件
Files.walk(Paths.get("C:\\Users\\sonyic\\Desktop\\nginx_log"),1)
// 解析 log 文件日志
.filter(file -> file.toString().endsWith(".log"))
.parallel()
.forEach((Path file) -> {
try (Stream<String> lines=Files.lines(file);){
lines.filter(line -> StringUtils.contains(line,"/krairdrop/validate?address")).forEach(
line->{
String validateLine = StringUtils.substringBetween(line," 200 8 \"","\" \"Mozilla/5.0");
String addressUrl = StringUtils.substringBetween(line,"GET","HTTP/1.1");
String address = StringUtils.substringAfterLast(addressUrl,"/krairdrop/validate?");
if (urlList.contains(validateLine)){
logger.info(".......line......{}",line);
if (StringUtils.isNotEmpty(address)){
if (map.get(validateLine) == null) {
Map<String, String> addressMap=Splitter.on("&").withKeyValueSeparator("=").split(address);
map.put(validateLine,Class2MapUtil.convertBean(addressMap,Invitation.class).getBagAddress());
} else {
Map<String, String> addressMap=Splitter.on("&").withKeyValueSeparator("=").split(address);
map.put(validateLine,map.get(validateLine) + " "+Class2MapUtil.convertBean(addressMap,Invitation.class).getBagAddress());
}
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
});
// 写入解析后的内容到文件中
Path path = Paths.get("C:\\Users\\sonyic\\Desktop\\output.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path,StandardOpenOption.APPEND))
{
map.entrySet().forEach(entry->{
try {
writer.append(entry.getKey()+"-------"+entry.getValue()+"\n");
} catch (IOException e) {
e.printStackTrace();
}
});
} catch (IOException e) {
e.printStackTrace();
}
//logger.info("------ url ------{}",URLDecoder.decode("http%3A%2F%2Frcfund.org%2Fkrairdrop%2F%3Fv%3DG0ULvfAP",java.nio.charset.StandardCharsets.UTF_8.name()));
}
}
package com.use.vo;
public class ImportExcelVo<T>{
private String templateFilePath;
private String filePath;
private String importListName;
private Class<T> clazz;
public String getTemplateFilePath() {
return templateFilePath;
}
public void setTemplateFilePath(String templateFilePath) {
this.templateFilePath=templateFilePath;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath=filePath;
}
public String getImportListName() {
return importListName;
}
public void setImportListName(String importListName) {
this.importListName=importListName;
}
public Class<T> getClazz() {
return clazz;
}
public void setClazz(Class<T> clazz) {
this.clazz=clazz;
}
public ImportExcelVo() {
}
public ImportExcelVo(String templateFilePath,String filePath,String importListName,Class<T> clazz) {
this.templateFilePath=templateFilePath;
this.filePath=filePath;
this.importListName=importListName;
this.clazz=clazz;
}
}
package com.use.vo;
public class LinkVo {
private String id;
private String name;
private String url;
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url=url;
}
@Override
public String toString() {
return "LinkVo{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", url='" + url + '\'' +
'}';
}
}
package com.use;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.util.Map;
public class Class2MapUtil {
private static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
public static <T> Map<String, String> convertMap(T t) {
String json=gson.toJson(t,new TypeToken<T>() {
}.getType());
return gson.fromJson(json,new TypeToken<Map<String, String>>() {
}.getType());
}
public static <T> T convertBean(Map<String, String> map,Class<T> clazz) {
String json=gson.toJson(map,new TypeToken<Map<String, String>>() {
}.getType());
return gson.fromJson(json,clazz);
}
}
package com.use;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.use.vo.ImportExcelVo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReadStatus;
import org.jxls.reader.XLSReader;
import org.xml.sax.SAXException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
// 将 excel 文件导入
public class ImportExcelUtil {
public static <T> List<T> importData(ImportExcelVo<T> vo){
XLSReader mainReader;
try {
InputStream inputXML=new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(vo.getTemplateFilePath()));
mainReader=ReaderBuilder.buildFromXML(inputXML);
InputStream inputXLS=new BufferedInputStream(new FileInputStream(vo.getFilePath()));
List<T> dataList=Lists.newArrayList();
Map<String, Object> beans=Maps.newHashMap();
beans.put(vo.getImportListName(),dataList);
XLSReadStatus readStatus=mainReader.read(inputXLS,beans);
return dataList;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
return Lists.newArrayList();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<workbook>
<worksheet name="Sheet1">
<section startRow="0" endRow="0"/>
<loop startRow="1" endRow="1" items="linkVoList" var="linkVo" varType="com.use.vo.LinkVo">
<section startRow="1" endRow="1">
<mapping row="1" col="0">linkVo.id</mapping>
<mapping row="1" col="1">linkVo.name</mapping>
<mapping row="1" col="2">linkVo.url</mapping>
</section>
<loopbreakcondition>
<rowcheck offset="0">
<cellcheck offset="0"></cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>