JAVA如何重置附件路径规则,并且实现附件转移到新目录下面
程序员文章站
2024-03-24 19:42:40
...
最近写了一个小工具类,因为最近需求需要附件按新的规则存放。网上找并没有找到现成的符合自己心意的工具类 我的是这样的新的存放:项目编号+项目名称 \文件
首先maven 需要配置如 可能会觉得多,但是这个配置也可以用来爬虫的,因为有 webmagic jar包
在配置数据源,因为大批量的附件转移,是原文件路径是保存在数据库的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.feng</groupId>
<artifactId>spider-learn</artifactId>
<version>1.0.0</version>
<name>spider-learn</name>
<description>learn webmagic project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.7.3</version>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.sourceforge.htmlunit</groupId>-->
<!-- <artifactId>htmlunit</artifactId>-->
<!-- <version>2.14</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpcore</artifactId>-->
<!-- <version>4.4.6</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
数据库表结构:数据库表名是project ,我是处理过原来的数据结构,简化表,如果你们需要加更复杂,自己加一下
那么就要配置数据库 和日志文件 因为小编是用的是mysql8.0以上,驱动的话需要跟小编写的一样
日志配置 :这里的日志配置很重要,你看一**释
log4j.rootLogger=DEBUG,A,CONSOLE
log4j.addivity.org.apache=false
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=Trace
log4j.appender.CONSOLE.layout.ConversionPattern=[boss_sys] %d{yyyy-MM-dd HH\:mm\:ss.SSS} -%-4r [%t] %-5p %x - %m%n
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=utf-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.A=org.apache.log4j.DailyRollingFileAppender
#这个是配置你日志输出位置,别踩坑,如果你是其他盘的项目,你放不了c盘,只能放其他盘或者项目底下
log4j.appender.A.File=${catalina.home}/logs/boss_log/boss
log4j.appender.A.DatePattern=yyyy-MM-dd'.log'
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=[boss_sys] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n
配置讲完了,现在讲代码
有俩段:
checkFile:是检查原先附件是否有漏掉,这个需要从附件服务器下载。放到你本地。我的做法也是放到本地分发,如果不存在,会记录在日志中,执行一遍。检查,可能有些附件不放到现在这个目录的,的小心处理。将处理过的数据导进去数据库的时候save默认是1代表存在,检查完,如果不存在,设置为2。
removeFile:是用来移动的,上述方法,已经检查到存在文件。看注释,注释说的很清楚,也就是将原来目录下的文件移动到新目录下,并删除掉原目录下的文件。 如果你想要保留原来的,用copy(srcPathStr, desPathStr);//将C:\Users\linweiliang\Desktop\file\primaryfile\2016\04\12.rtf文件拷贝到C:\Users\linweiliang\Desktop\file\presentfile\100000002222\12.rtf 这个函数即可
package com.feng;
import com.alibaba.druid.pool.DruidDataSource;
import com.feng.removes.Project;
import lombok.extern.log4j.Log4j;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Remove {
private static Logger logger = Logger.getLogger(Remove.class);
@Test
public void checkFile () throws Exception{
Connection conn = null;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
DruidDataSource dataSource = new DruidDataSource();
//8.0以上的mysql需要配置的驱动 com.mysql.cj.jdbc.Driver
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//8.0以上的mysql需要配置的驱动 useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
dataSource.setUrl("jdbc:mysql://localhost:3306/reptiles?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false");
//数据库账号密码
dataSource.setUsername("");
//数据库密码
dataSource.setPassword("");
try{
// 获得连接:
conn = dataSource.getConnection();
// 编写SQL:
String sql = "select * from project";
pstmt = conn.prepareStatement(sql);
// 执行sql:
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("project_number"));
String project_number = rs.getString("project_number");
System.out.println(rs.getString("project_name"));
String project_name = rs.getString("project_name");
System.out.println(rs.getString("path"));
String path = rs.getString("path");
File f = new File(path);
if (!f.exists()) {
logger.error("项目编码:"+project_number+"-项目名"+project_name+"-文件路径"+path+"没找到");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
/* JdbcTemplate jdbcTemplate = new JdbcTemplate();
*//* String sql = "select * from project";
List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql);
System.out.println(queryForList);*/
}
@Test
public void removeFile () throws Exception{
Connection conn = null;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
DruidDataSource dataSource = new DruidDataSource();
//8.0以上的mysql需要配置的驱动 com.mysql.cj.jdbc.Driver
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//8.0以上的mysql需要配置的驱动 useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
dataSource.setUrl("jdbc:mysql://localhost:3306/reptiles?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false");
//数据库账号密码
dataSource.setUsername("");
//数据库密码
dataSource.setPassword("");
try{
// 获得连接:
conn = dataSource.getConnection();
// 编写SQL:
String sql = "select * from project where save = '1'";
pstmt = conn.prepareStatement(sql);
// 执行sql:
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("project_number"));
String project_number = rs.getString("project_number");
System.out.println(rs.getString("project_name"));
String project_name = rs.getString("project_name");
System.out.println(rs.getString("path"));
File f = new File(rs.getString("path"));
String filename = f.getName(); // 老的文件 C:\Users\linweiliang\Desktop\file\primaryfile\2016\04\12.rtf
//判断一下是否有目录,在判断一下是否有这个文件,有这个文件就跳过,没有就复制到文件目录下
//文件规则: C:\Users\linweiliang\Desktop\file\presentfile\\项目编码+项目名称\\文件
String presentfilePath = "C:\\Users\\linweiliang\\Desktop\\file\\presentfile";
presentfilePath += "\\"+project_number+project_name+"";
String presentfilePathmudi = presentfilePath;
//新的文件目录是否存在,不存在新建一个文件目录
File filePresentPath = new File(presentfilePath);
if (!filePresentPath.exists()) {
filePresentPath.mkdirs();
}
presentfilePath +="\\"+filename+"";
//
File Presentfile = new File(presentfilePath);
//新的文件目录是否存在,不存在就将原文件目录下的文件移动到新的文件目录,那么原文件目录下的文件就会不见
if (!Presentfile.exists()) {
String newPath = presentfilePath;
File fnew = new File(newPath);
f.renameTo(fnew);
}
System.out.println(f.getName());
}
}catch(Exception e){
e.printStackTrace();
}finally{
}
JdbcTemplate jdbcTemplate = new JdbcTemplate();
/* String sql = "select * from project";
List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(sql);
System.out.println(queryForList);*/
}
}