/**
* @param
* @Description: 读取txt文件,写入excel文件
* @date 2022/06/16
*/
@PostMapping(value = "/toExcel")
@ResponseBody
public String toExcel(MultipartFile[] file, HttpServletResponse response) throws ServletException, IOException {
JSONObject object = new JSONObject();
try {
File files = null;
//生成完以后的下载路径
SimpleDateFormat formate = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");
String date = formate.format(new Date());
String localPath = route + date + ".xlsx";
File path = new File(localPath);
//创建工作簿
WritableWorkbook workbook = Workbook.createWorkbook(path);
//创建工作表
WritableSheet sheet = workbook.createSheet("first", 0);
int n = 0;// excel列数
//设置列的宽度
sheet.setColumnView(n++, 30);
sheet.setColumnView(n++, 120);
//设置标题
sheet.addCell(new Label(0, 0, "文本标题"));
sheet.addCell(new Label(1, 0, "待检测文本内容"));
//if(file != null && file.length > 0){
//循环获取file数组中得文件
int i=0;
for (MultipartFile multipartFile : file) {
String originalFilename = multipartFile.getOriginalFilename();
String[] filename = originalFilename.split("\\.");
//编码格式
String encoding = "UTF-8";
//创建流对象 指定编码
InputStreamReader read = new
InputStreamReader(multipartFile.getInputStream(), encoding);
//从字符输入流中读取文本并缓冲字符
BufferedReader bufferedReader = new BufferedReader(read);
//一些临时变量,用于写到excel中
String lineTxt = null;
StringBuilder buff = new StringBuilder();
while ((lineTxt = bufferedReader.readLine()) != null) {
buff = buff.append(lineTxt);
}
String text = buff.substring(buff.indexOf("</head>"));
text = text.replaceAll("\\&[a-zA-Z]{1,10};", "") //去除类似< > 的字串
.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "") //去除开始标签及没有结束标签的标签
.replaceAll("</[a-zA-Z]+[1-9]?>", "");
text = text.replaceAll(" ", "");
text = text.replaceAll(",,", "");
//文本标题
String substring = originalFilename.substring(originalFilename.indexOf("_") + 1, originalFilename.indexOf("."));
if(text.length()>2000){
BigDecimal bigDecimal = new BigDecimal(text.length()).divide(new BigDecimal(2000)).setScale(2, BigDecimal.ROUND_HALF_UP);
for (int num=0;num<bigDecimal.intValue()+1;num++){
String text1=null;
String copyText=text;
if(text.length()>2000){
text1=copyText.substring(0,2000);
text=copyText.substring(2000);
}else {
text1=copyText.substring(0);
}
//读取内容写入
sheet.addCell(new Label(0, i+1, substring));
//内容
sheet.addCell(new Label(1, i+1, text1));
i++;
}
}else {
//读取内容写入
sheet.addCell(new Label(0, i+1, substring));
//内容
sheet.addCell(new Label(1, i+1, text));
i++;
}
read.close();
}
//写入文件
workbook.write();
//关闭文件
workbook.close();
object.put("code", 200);
object.put("message", "success");
object.put("data", localPath);
return localPath;
} catch (Exception e) {
log.error("读取文件内容出错");
e.printStackTrace();
object.put("code", 500);
object.put("message", "error");
return "error";
}
}
/**
* @param
* @Description: 读取txt文件,写入excel文件
* @date 2022/06/16
*/
@PostMapping(value = "/uploadFile")
@ResponseBody
public String uploadFile(MultipartFile[] file, HttpServletResponse response) throws ServletException, IOException {
JSONObject object = new JSONObject();
StringBuilder content=new StringBuilder();
try {
File files = null;
//if(file != null && file.length > 0){
//循环获取file数组中得文件
int num=0;
String fileName=null;
for (int i = 0; i < file.length; i++) {
String originalFilename = file[i].getOriginalFilename();
originalFilename=originalFilename.substring(0,originalFilename.indexOf("."));
fileName=originalFilename;
fileName=fileName.substring(0,fileName.indexOf("_"));
//编码格式
String encoding = "UTF-8";
//创建流对象 指定编码
InputStreamReader read = new InputStreamReader(file[i].getInputStream(), encoding);
//从字符输入流中读取文本并缓冲字符
BufferedReader bufferedReader = new BufferedReader(read);
//一些临时变量,用于写到excel中
String lineTxt = null;
StringBuilder buff = new StringBuilder();
content.append(originalFilename);
while ((lineTxt = bufferedReader.readLine()) != null) {
buff = buff.append(lineTxt);
}
String text = buff.substring(buff.indexOf("</head>"));
text = text.replaceAll("\\&[a-zA-Z]{1,10};", "") //去除类似< > 的字串
.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "") //去除开始标签及没有结束标签的标签
.replaceAll("</[a-zA-Z]+[1-9]?>", "");
text = text.replaceAll(" ", "");
text = text.replaceAll(",,", "");
content.append(text);
read.close();
if(num>20){
log.info("文本大小:{}",content.length());
textCheckAPIDemo.textCheck(content.toString(),fileName);
num=0;
content=new StringBuilder();
}else {
num++;
}
}
log.info("文本大小:{}",content.length());
textCheckAPIDemo.textCheck(content.toString(),fileName);
object.put("code", 200);
object.put("message", "success");
object.put("data", "success");
return JSONObject.toJSONString(object);
} catch (Exception e) {
log.error("读取文件内容出错");
e.printStackTrace();
object.put("code", 500);
object.put("message", "error");
return "error";
}
}
@GetMapping("/download")
public void fileDownload(String url, HttpServletResponse response) throws ServletException, IOException {
//要获取下载文件的路径
String realPath = url;
//3.设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西,中文文件名URLEncoder.encode编码,否则可能乱码
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("易盾平台.xlsx", "UTF-8"));
//4.获取下载文件的输入流
FileInputStream in = new FileInputStream(realPath);
//5.创建缓冲区
int len = 0;
byte[] buffer = new byte[1024];
//6.获取OutputStream对象
ServletOutputStream out = response.getOutputStream();
//7.将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
批量读取本地txt文件,批量写入excel文件
最新推荐文章于 2023-02-19 19:52:04 发布