就从数据库写一条线到控制器的内容给大家看吧
@Mapper
public interface IMPosDao {
/**
* 查询所有pos数据
*/
List selectAllPos(Pos pos);
/**
* Mybatis方法,在线读取后直接存入数据,一次插入一条记录
* @param pos
* @return
*/
boolean importFile(Pos pos);
}
@Service
public class FtpService {
@Autowired
private FTPUtils ftpUtils;
//此处我们有个Utils工具类,我会在下面贴里面用到的方法
public List Read(String fileName) throws Exception{
int ftpPathInt = Integer.parseInt(ftpPort);
//读取文件数据
List ftpdata= ftpUtils.readFileFromFTP(ftpUserName,ftpPassword,ftpPath,ftpHost,ftpPathInt,fileName);
return string2pos(ftpdata);
}
//截取字符串并保存在对象
public List string2pos(List ftpDatas) throws IOException{
List listPos=new ArrayList();
//String
for(String ftpData:ftpDatas) {
Pos pos=new Pos();
pos.setZoneno(ftpData.substring(0, 5));
pos.setBrno(ftpData.substring(5, 10));
pos.setCenbrno(ftpData.substring(10, 15));
pos.setTellerno(ftpData.substring(15, 20));
pos.setWorkdate(ftpData.substring(20, 30));
pos.setCarddeno(ftpData.substring(30, 49));
pos.setCarddety(ftpData.substring(49, 52));
System.out.println(pos);
listPos.add(pos);
posDao.importFile(pos);
}
return listPos;
}
}
public static List readFileFromFTP(String ftpUserName, String ftpPassword,
String ftpPath, String ftpHost, int ftpPort, String fileName) throws Exception {
List Ftplist=new ArrayList();
StringBuffer resultBuffer = new StringBuffer();
InputStream in = null;
FTPClient ftpClient = null;
logger.info(“开始从绝对路径:” + ftpPath + “读取文件!”);
try {
ftpClient = FTPUtils.getFTPClient(ftpHost, ftpUserName,ftpPassword, ftpPort);
//ftpClient.setControlEncoding(“UTF-8”); // 中文支持
//ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
in = ftpClient.retrieveFileStream(fileName);
} catch (FileNotFoundException e) {
logger.error(“没有找到” + ftpPath + “文件”);
e.printStackTrace();
} catch (SocketException e) {
logger.error(“连接FTP失败!”);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
logger.error(“文件读取错误!”);
e.printStackTrace();
}
if (in != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(in,"gb2312"));
String data = null;
try {
while ((data = br.readLine()) != null) {
Ftplist.add(data);
}
} catch (IOException e) {
logger.error("文件读取错误。");
e.printStackTrace();
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Ftplist;
}
/**
* ftp读取入库
* @param fileName
* @return
* @throws Exception
*/
@ApiOperation(value = “ftp在线读取”, notes = “ftp在线读取”)
@GetMapping("/import")
public JsonResponse Download(@RequestParam String fileName) throws Exception {
return JsonResponseUtil.success(ftpService.Read(fileName));
}
总结:上面Utils里面的ftpHost和其他的都是ftp的端口号和地址这些,可以在
ftphost: 192.168.1.126
ftpusername: fanyusong
ftppassword: 123456
ftpport: 21
ftppath: “/”
ftplocalpath: F:/FTPDownload
#db
datasource:
url: jdbc:oracle:thin:@49.4.88.115:1521:ejkj
username: ICBC_POS
password: ICBC_POS
driver-class-name: oracle.jdbc.OracleDriver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
该功能测试的时候数据字段有59个,所以比较多,我只截取了一部分,千万不要盲目复制,需要就看看做个参考,我是实现了的,并且是插入到Oracle数据库,因为甲方没有明确需求,所以我的实体类都是String类型,并且数据库设计都是VARCHAR字段,如果有NUMBER字段注意要在实体类里做相应的改动。