Commit 10419724 by wang_jiaxing

湖南读取本地文件入库

parent 6e4f4e94
......@@ -26,23 +26,47 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.7.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.com.winmage</groupId>
......@@ -101,6 +125,12 @@
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ftp -->
<dependency>
......@@ -237,7 +267,7 @@
<!-- 公司的maven plugin仓库 -->
<pluginRepository>
<id>founder</id>
<url>http://39.99.224.27:8081/nexus/content/groups/public/</url>
<url>http://47.92.223.200:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
......
package com.founder.controller;
import com.founder.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FileService fileService;
@RequestMapping("/getFileToOracle")
public String getFileToOracle(){
try {
return fileService.getFileToOracle();
} catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
}
}
package com.founder.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface FileDao {
void updateFlwsnr(@Param("asjbh") String asjbh, @Param("wslb") String wslb, @Param("fileType") String fileType, @Param("byteByFile") byte[] byteByFile);
}
package com.founder.service;
public interface FileService {
String getFileToOracle();
}
package com.founder.service.impl;
import com.founder.dao.FileDao;
import com.founder.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
@Service
public class FileServiceImpl implements FileService {
@Autowired
private FileDao fileDao;
@Value("${fileDir}")
private String fileDir;
@Value("${outDir}")
private String outDir;
@Value("${isCs}")
private String isCs;
@Value("${poorSize}")
private Integer poorSize;
private final ExecutorService cachedThreadPool;
{
cachedThreadPool = Executors.newCachedThreadPool();
}
@Override
public String getFileToOracle() {
List<Map<String, Object>> list = new ArrayList<>();
List<Map<String, Object>> fileList = traverseFolder(fileDir, null, list);
if (fileList == null || fileList.size() == 0){
return "null";
}
int threadCount = (fileList.size() / poorSize) + 1;
for (int i = 0; i < threadCount; i++) {
int begin = i * poorSize;
int end = Math.min(begin + poorSize, fileList.size());
FutureTask<String> task = new FutureTask<>(()->{
for (int index = begin; index < end; index++) {
Map<String, Object> map = fileList.get(index);
String asjbh = map.get("asjbh").toString();
String wslb = map.get("wslb").toString();
String fileType = map.get("fileType").toString();
File file = (File) map.get("file");
// System.out.println("asjbh=" + asjbh + ";wslb=" + wslb + ";wjgs=" + fileType);
try {
if ("true".equals(isCs)){
copyFileUsingFileChannels(file, new File(outDir + asjbh + "#" + wslb + fileType));
} else {
fileDao.updateFlwsnr(asjbh, wslb, fileType, getByteByFile(file));
}
} catch (Exception e){
e.printStackTrace();
}
double fm = end - begin;
int i1 = index - begin;
double fz = i1 + 1;
System.out.println("====================线程" + Thread.currentThread().getName() + "执行了" + String.format("%.2f", ((fz/fm)*100)) + "%");
}
return "success";
});
cachedThreadPool.submit(task);
}
return "success";
}
public List<Map<String, Object>> traverseFolder(String path, String parentName, List<Map<String, Object>> fileList) {
File file = new File(path);
if (file.exists()){
if (file.isDirectory()){
File[] files = file.listFiles();
if (files == null || files.length == 0){
System.out.println("文件夹是空的!");
return fileList;
}
for (File file1 : files) {
// System.out.println("路径:" + file1.getAbsolutePath());
String fileName = file.getName();
traverseFolder(file1.getAbsolutePath(), fileName, fileList);
}
} else {
String fileName = file.getName();
// System.out.println(fileName);
if (fileName.startsWith("A")) {
String asjbh = fileName.substring(fileName.indexOf("A"), 23);
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (parentName != null && parentName.startsWith("ok_")) {
String wslb = parentName.substring(parentName.lastIndexOf("#") + 1);
HashMap<String, Object> map = new HashMap<>();
map.put("asjbh", asjbh);
map.put("wslb", wslb);
map.put("fileType", fileType);
map.put("file", file);
fileList.add(map);
}
}
}
}
return fileList;
}
private byte[] getByteByFile(File file){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e){
e.printStackTrace();
}
return buffer;
}
private void copyFileUsingFileChannels(File source, File dest) throws IOException {
try (FileChannel inputChannel = new FileInputStream(source).getChannel(); FileChannel outputChannel = new FileOutputStream(dest).getChannel()) {
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
}
}
}
spring:
datasource:
xzxt:
driver-class-name: oracle.jdbc.OracleDriver
username: XZXT
password: XzPwd#11022
jdbc-url: jdbc:oracle:thin:@47.92.129.99:1560:orcl
# driver-class-name: oracle.jdbc.driver.OracleDriver
# username: XZXT
# password: XzPwd#11022
# jdbc-url: jdbc:oracle:thin:@47.92.129.99:1560:orcl
# type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
username: zhyy
password: zh_HNxz43
jdbc-url: jdbc:oracle:thin:@10.142.16.194:1521:orcl
type: com.alibaba.druid.pool.DruidDataSource
manager:
driver-class-name: oracle.jdbc.OracleDriver
......@@ -17,11 +22,17 @@ spring:
jpa:
show-sql: true
redis:
host: 47.92.223.200
port: 7779
password: p!ssw0rd1
host: 127.0.0.1
port: 6379
# password: p!ssw0rd1
#fileDir: /Users/lystar/Software/fileUpLoad/fileDir/
#outDir: /Users/lystar/Software/fileUpLoad/outDir/
fileDir: D:/fileDir/
outDir: D:/outDir/
#isCs: true
isCs: false
poorSize: 100
ftpserverip: 39.99.224.27
nginxport: 9053
......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.founder.dao.FileDao">
<update id="updateFlwsnr">
update TB_ST_ASJ_FLWS
set FLWS_DZWJNR = #{byteByFile},
flws_dzwjgs = #{fileType},
gxsj = sysdate
where asjbh = #{asjbh} and flws_asjflwsdm =#{wslb} and xxsc_pdbz = '0'
and (dbms_lob.getLength(FLWS_DZWJNR) = 0 or FLWS_DZWJNR is null)
</update>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment