Introduce my name Fadli, I work as a bug bounty hunter, I found a vulnerability in an existing gitlab system which allows me to change the gitlab administrator/root password and also be able to view all source code and secret credentials. This finding is Critical, get shell to access server/linux operating system. please reply my email bfadliyanto@gmail.com

Commit cb0fb1e9 authored by 林思聪's avatar 林思聪

First version

parents
Pipeline #165 failed with stages
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
/*
* Copyright 2007-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;
public class MavenWrapperDownloader {
private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";
/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";
/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if (mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if (mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if (!outputFile.getParentFile().exists()) {
if (!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50714
Source Host : localhost:3306
Source Database : slideway
Target Server Type : MYSQL
Target Server Version : 50714
File Encoding : 65001
Date: 2020-06-05 10:12:49
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for file_info
-- ----------------------------
DROP TABLE IF EXISTS `file_info`;
CREATE TABLE `file_info` (
`id` int(19) NOT NULL AUTO_INCREMENT,
`filename` varchar(255) NOT NULL,
`fence` int(6) NOT NULL,
`in_date_time` datetime NOT NULL,
`is_delete` tinyint(2) NOT NULL,
`file_type` tinyint(2) NOT NULL,
PRIMARY KEY (`id`,`filename`)
) ENGINE=InnoDB AUTO_INCREMENT=242 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for subscriber
-- ----------------------------
DROP TABLE IF EXISTS `subscriber`;
CREATE TABLE `subscriber` (
`id` int(19) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL,
`is_delete` tinyint(2) DEFAULT NULL,
`ip_port` varchar(255) NOT NULL,
`subcriber_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`,`ip_port`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
This diff is collapsed.
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%
<?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 https://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.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wsn640</groupId>
<artifactId>slideway-data-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>slideway-data-interface</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-batch</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.batch</groupId>-->
<!-- <artifactId>spring-batch-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.wsn640.slidewaydatainterface;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SlidewayDataInterfaceApplication {
public static void main(String[] args) {
SpringApplication.run(SlidewayDataInterfaceApplication.class, args);
}
}
package com.wsn640.slidewaydatainterface.config.dao;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.beans.PropertyVetoException;
@Configuration
@MapperScan("com.wsn640.slidewaydatainterface.dao")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcurl;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(name="dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcurl);
dataSource.setUser(jdbcUsername);
dataSource.setPassword(jdbcPassword);
//关闭连接后不自动commit
dataSource.setAutoCommitOnClose(false);
return dataSource;
}
}
\ No newline at end of file
package com.wsn640.slidewaydatainterface.config.dao;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class SessionFactoryConfiguration {
@Value("${mybatis_config_file}")
private String mybatisConfigFilePath;
@Value("${mapper_path}")
private String mapperPath;
@Value("${entity_package}")
private String entityPackage;
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException{
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath));
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath;
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath));
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
return sqlSessionFactoryBean;
}
}
\ No newline at end of file
package com.wsn640.slidewaydatainterface.config.schedule;
import com.wsn640.slidewaydatainterface.schedule.job.DemoJob;
import com.wsn640.slidewaydatainterface.schedule.job.SubscriberJob;
import com.wsn640.slidewaydatainterface.schedule.jobfactory.DemoAdaptableJobFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
@Configuration
public class QuartzConfig {
/**
* 1.创建Job对象
*/
@Bean
public JobDetailFactoryBean jobDetailFactoryBean(){
JobDetailFactoryBean factory = new JobDetailFactoryBean();
//关联我们自己的Job类
factory.setJobClass(SubscriberJob.class);
return factory;
}
/**
* 2.创建Trigger对象
* 简单的Trigger
*/
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){
SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
//关联JobDetail对象
factory.setJobDetail(jobDetailFactoryBean.getObject());
//该参数表示一个执行的毫秒数
// 2个钟执行一次
factory.setRepeatInterval(1* 60 * 60 * 1000);
//重复次数
factory.setRepeatCount(-1);
return factory;
}
// /**
// * 3.创建Scheduler对象
// */
// @Bean
// public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean){
// SchedulerFactoryBean factory = new SchedulerFactoryBean();
// //关联trigger
// factory.setTriggers(simpleTriggerFactoryBean.getObject());
//
// return factory;
// }
/**
* 3.创建Scheduler对象
*/
@Bean
public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean, DemoAdaptableJobFactory demoAdaptableJobFactory){
SchedulerFactoryBean factory = new SchedulerFactoryBean();
//关联trigger
factory.setTriggers(simpleTriggerFactoryBean.getObject());
factory.setJobFactory(demoAdaptableJobFactory);
return factory;
}
}
package com.wsn640.slidewaydatainterface.config.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
\ No newline at end of file
package com.wsn640.slidewaydatainterface.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
@RestController
@RequestMapping("/demo") // 根路由
public class DemoController {
@RequestMapping(value="/hellospringboot", method= RequestMethod.GET)
public String hello(){
return "Hello SpringBoot!";
}
public String downloadFile(HttpServletResponse response) throws UnsupportedEncodingException{
return null;
}
}
package com.wsn640.slidewaydatainterface.controller;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import com.wsn640.slidewaydatainterface.service.SlidewayDataService;
import com.wsn640.slidewaydatainterface.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/data") // 根路由
public class SlidewayDataControllor {
@Autowired
private SlidewayDataService slidewayDataService;
@Value("${date_time_pattern}")
private String dateTimePattern;
@RequestMapping(value="/test", method=RequestMethod.GET)
public String test(){
// slidewayDataService.insertAllFileInfo();
sendFilesTest();
return "Test Finish!";
}
// 查询接口
@RequestMapping(value = "/listSlidewayDataInfo", method = RequestMethod.GET)
private Map<String, Object> listSlidewayDataInfo(){
Map<String, Object> modelMap = new HashMap<String, Object>();
List<FileInfo> list = slidewayDataService.getAllSlidewayData();
modelMap.put("FileInfoList", list);
return modelMap;
}
// @RequestMapping(value = "/querySlidewayDataByDate", method = RequestMethod.GET)
// private Map<String, Object> querySlidewayDataByDate(){
// Map<String, Object> modelMap = new HashMap<String, Object>();
// List<FileInfo> list = slidewayDataService.getAllSlidewayData();
// modelMap.put("FileInfoList", list);
// return modelMap;
// }
// 拉取接口
// @RequestMapping(value="/getAloneFile", method=RequestMethod.POST)
// @ResponseBody
// public void getFile(HttpServletRequest request, HttpServletResponse response){
// String filename = request.getParameter(filename);
// System.out.println(filename);
//
//
// }
@RequestMapping(value = "/getAloneFile", method = RequestMethod.GET)
public ResponseEntity<FileSystemResource> getAloneFile(String filename) {
return sendFile(new File(slidewayDataService.getFileDir() + "/" + filename));
}
@RequestMapping(value = "/getFileZip", method = RequestMethod.POST)
public void getFileZip(@RequestBody List<String> filenameList, HttpServletResponse response) {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=uchainfile.zip");
try {
Tool.toZip(slidewayDataService.getFileDir(), filenameList, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
// 封装的文件返回实体
public ResponseEntity<FileSystemResource> sendFile(File file) {
if (file == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity .ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new FileSystemResource(file));
}
// 测试接口
/**
* 用于 向订阅者 端口 发送文件测试
*/
public void sendFilesTest(){
final String filename = "202001040427090790000000.jpg";
final String filepath = "E:/2020/slideway-test" + "/" + filename;
final String url = "http://localhost:8088/slideway/data/download";
RestTemplate restTemplate = new RestTemplate();
FileSystemResource fileSystemResource = new FileSystemResource(filepath);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("filename", filename);
form.add("file", fileSystemResource);
System.out.println("!!!!!!!!!!!!!");
HttpEntity<MultiValueMap<String, Object> > files = new HttpEntity<>(form, headers);
String s = restTemplate.postForObject(url, files, String.class);
System.out.println(s);
}
/**
* 用于 订阅者 端口 接收文件测试
*/
@RequestMapping(value="/download", method= RequestMethod.POST)
@ResponseBody
public String downloadFiles(@RequestParam("file") MultipartFile file){
System.out.println("有被访问到");
final String filePath = "E:/2020/slideway-test2";
boolean isCreate = createLocalFile(filePath, file);
if(!isCreate){
System.out.println("Create local file failed!");
return "Create local file failed!";
}
System.out.println("Create local file successfully");
return "Create local file successfully";
}
private boolean createLocalFile(String filePath, MultipartFile file) {
File localFile = new File(filePath);
//先创建目录
localFile.mkdirs();
String originalFilename = file.getOriginalFilename();
String path = filePath+"/"+originalFilename;
System.out.println("createLocalFile path = " + path);
localFile = new File(path);
FileOutputStream fos = null;
InputStream in = null;
try {
if(localFile.exists()){
//如果文件存在删除文件
boolean delete = localFile.delete();
if (delete == false){
System.out.println("Delete exist file \"" + path + "\" failed!!!");
}
}
//创建文件
if(!localFile.exists()){
//如果文件不存在,则创建新的文件
localFile.createNewFile();
System.out.println("Create file successfully,the file is " + path);
}
//创建文件成功后,写入内容到文件里
fos = new FileOutputStream(localFile);
in = file.getInputStream();
byte[] bytes = new byte[1024];
int len = -1;
while((len = in.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.flush();
System.out.println("Reading uploaded file and buffering to local successfully!");
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
if(fos != null) {
fos.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
System.out.println("InputStream or OutputStream close error :" + e.toString());
return false;
}
}
return true;
}
}
package com.wsn640.slidewaydatainterface.controller;
import com.wsn640.slidewaydatainterface.service.SubscriberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/subscriber") // 根路由
public class SubscriberController {
@Autowired
SubscriberService subscriberService;
@RequestMapping(value="/register", method= RequestMethod.POST)
public String register(String name, String ipPort){
return subscriberService.registerSubscriber(name, ipPort);
}
@RequestMapping(value="/logout", method= RequestMethod.POST)
public String logout(Integer id){
return subscriberService.deleteSubscriber(id);
}
}
package com.wsn640.slidewaydatainterface.dao;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import org.apache.ibatis.annotations.Param;
import org.mybatis.spring.annotation.MapperScan;
import java.util.Date;
import java.util.List;
public interface FileInfoDao {
FileInfo queryFileInfoById(int id);
FileInfo queryFileInfoByFileName(String filename);
List<FileInfo> queryFileInfoList();
List<String> queryFileInfoListOnlyFilename();
List<FileInfo> queryFileInfoListByDate(@Param("st")Date st, @Param("ed")Date ed);
List<FileInfo> queryFileInfoListByFence(int fence);
boolean insertFileInfo(FileInfo fileInfo);
boolean updateFileInfoById(FileInfo fileInfo);
boolean updateFileInfoByFilename(FileInfo fileInfo);
boolean deleteFileInfo(FileInfo fileInfo);
}
package com.wsn640.slidewaydatainterface.dao;
import com.wsn640.slidewaydatainterface.entity.Subscriber;
import java.util.List;
public interface SubscriberDao {
Subscriber querySubscriber(int id);
Subscriber querySubsriberByName(String name);
Subscriber querySubsriberByIpPort(String ipPort);
List<Subscriber> querySubscriberList();
boolean insertSubscriber(Subscriber subscriber);
boolean deleteSubscriber(Subscriber subscriber);
}
package com.wsn640.slidewaydatainterface.entity;
import java.util.Date;
public class FileInfo {
private Integer id; // 键值
private String filename; // 文件名, 格式为 YYYYMMDDhhmmssnnntrrrrrr;
private Date inDateTime; // 日期时间
private Integer fence; // 栏位
private Integer fileType; // 类型
private Integer isDelete; // 删除标志位
/*Mybatis 实体类必须有一个无参构造方法*/
public FileInfo() {
}
public FileInfo(Integer id, String filename, Date inDateTime, Integer fence, Integer fileType, Integer isDelete) {
this.id = id;
this.filename = filename;
this.inDateTime = inDateTime;
this.fence = fence;
this.fileType = fileType;
this.isDelete = isDelete;
}
public FileInfo(String filename, Date inDateTime, Integer fence, Integer fileType, Integer isDelete) {
this.filename = filename;
this.inDateTime = inDateTime;
this.fence = fence;
this.fileType = fileType;
this.isDelete = isDelete;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public Date getInDateTime() {
return inDateTime;
}
public void setInDateTime(Date inDateTime) {
this.inDateTime = inDateTime;
}
public Integer getFence() {
return fence;
}
public void setFence(Integer fence) {
this.fence = fence;
}
public Integer getFileType() {
return fileType;
}
public void setFileType(Integer fileType) {
this.fileType = fileType;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
}
package com.wsn640.slidewaydatainterface.entity;
import java.util.Date;
public class Subscriber {
private Integer id;
private String ipPort;
private Date createTime;
private String subscriberName;
private Integer isDelete; // 删除标志位
public Subscriber() {
}
public Subscriber(Integer id, String ipPort, Date createTime, String subscriberName, Integer isDelete) {
this.id = id;
this.ipPort = ipPort;
this.createTime = createTime;
this.subscriberName = subscriberName;
this.isDelete = isDelete;
}
public Subscriber(String ipPort, String subscriberName) {
this.ipPort = ipPort;
this.subscriberName = subscriberName;
createTime = new Date();
isDelete = 0;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIpPort() {
return ipPort;
}
public void setIpPort(String ipPort) {
this.ipPort = ipPort;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getSubscriberName() {
return subscriberName;
}
public void setSubscriberName(String subscriberName) {
this.subscriberName = subscriberName;
}
public Integer getIsDelete() {
return isDelete;
}
public void setIsDelete(Integer isDelete) {
this.isDelete = isDelete;
}
}
package com.wsn640.slidewaydatainterface.schedule.job;
import com.wsn640.slidewaydatainterface.service.DemoService;
import com.wsn640.slidewaydatainterface.service.SlidewayDataService;
import com.wsn640.slidewaydatainterface.service.SubscriberService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.Date;
public class DemoJob extends QuartzJobBean {
@Autowired
DemoService demoService;
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
demoService.SayHello();
}
}
package com.wsn640.slidewaydatainterface.schedule.job;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import com.wsn640.slidewaydatainterface.entity.Subscriber;
import com.wsn640.slidewaydatainterface.service.SlidewayDataService;
import com.wsn640.slidewaydatainterface.service.SubscriberService;
import com.wsn640.slidewaydatainterface.tool.Tool;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class SubscriberJob extends QuartzJobBean {
@Autowired
SlidewayDataService slidewayDataService;
@Autowired
SubscriberService subscriberService;
private static String zipDir = "E:\\2020\\slidewayzip_dir";
@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
List<String> newFilenameList = slidewayDataService.getNewFileList();
if(newFilenameList.size() > 0){
List<FileInfo> newFileInfoList = new ArrayList<FileInfo>();
for (String filename : newFilenameList
) {
newFileInfoList.add(slidewayDataService.decodeFilename(filename));
}
slidewayDataService.insertNewFileInfo(newFileInfoList);
List<Subscriber> subscriberList = subscriberService.getSubscriberList();
if(subscriberList.size() > 0){
String zipName = Tool.toZipFile(slidewayDataService.getFileDir(), newFilenameList);
for (Subscriber subscriber: subscriberList) {
RestTemplate restTemplate = new RestTemplate();
FileSystemResource fileSystemResource = new FileSystemResource(zipDir + "/" + zipName);
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("filename", zipName);
form.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object> > files = new HttpEntity<>(form, headers);
String s = restTemplate.postForObject(subscriber.getIpPort(), files, String.class);
System.out.println("______" + s);
}
Tool.deleteZip(zipName);
}
}
}
}
package com.wsn640.slidewaydatainterface.schedule.jobfactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component("DemoAdaptableJobFactory")
public class DemoAdaptableJobFactory extends AdaptableJobFactory {
//AutowireCapableBeanFactory 可以将一个对象添加到SpringIOC容器中,并且完成该对象注入
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
/**
* 该方法需要将实例化的任务对象手动的添加到springIOC容器中并且完成对象的注入
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object obj = super.createJobInstance(bundle);
//将obj对象添加Spring IOC容器中,并完成注入
this.autowireCapableBeanFactory.autowireBean(obj);
return obj;
}
}
package com.wsn640.slidewaydatainterface.service;
public interface DemoService {
void SayHello();
}
package com.wsn640.slidewaydatainterface.service;
import com.sun.deploy.net.HttpResponse;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
public interface SlidewayDataService {
String getFileDir();
List<FileInfo> getAllSlidewayData();
FileInfo decodeFilename(String filename);
List<FileInfo> checkNewFileList();
List<String> getNewFileList();
void insertAllFileInfo();
void insertNewFileInfo(List<FileInfo> newFileList);
}
package com.wsn640.slidewaydatainterface.service;
import com.wsn640.slidewaydatainterface.entity.Subscriber;
import java.util.List;
public interface SubscriberService {
String registerSubscriber(String name, String ipPort);
List<Subscriber> getSubscriberList();
// Subscriber searchSubcriberByIpPort(String ipPort);
String deleteSubscriber(Integer id);
}
package com.wsn640.slidewaydatainterface.service.impl;
import com.wsn640.slidewaydatainterface.service.DemoService;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class DemoServiceImp implements DemoService {
@Override
public void SayHello() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()) + " : Hello World!");
}
}
package com.wsn640.slidewaydatainterface.service.impl;
import com.wsn640.slidewaydatainterface.dao.FileInfoDao;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import com.wsn640.slidewaydatainterface.service.SlidewayDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class SlidewayDataServiceImpl implements SlidewayDataService {
@Value("${load_path}")
private String fileDir;
@Value("${date_time_pattern}")
private String dateTimePattern;
@Autowired
private FileInfoDao fileInfoDao;
@Override
public String getFileDir() {
return fileDir;
}
public String getResolutePath(String filename) {
return fileDir + '/' + filename;
}
@Override
public List<FileInfo> getAllSlidewayData() {
return fileInfoDao.queryFileInfoList();
}
@Override
public FileInfo decodeFilename(String filename) {
SimpleDateFormat sdf = new SimpleDateFormat(dateTimePattern);
String prefix = filename.substring(0, filename.lastIndexOf("."));
String dateStr = prefix.substring(0, 14);
int fence = Integer.parseInt(prefix.substring(14, 17));
int fileType = Integer.parseInt(prefix.substring(17, 18));
Date inDateTime = null;
try {
inDateTime = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
System.out.println("转换出错了:" + dateStr);
}
return new FileInfo(filename, inDateTime, fence, fileType, 0);
}
@Transactional
@Override
public List<FileInfo> checkNewFileList() {
List<String> newFilenameList = getNewFileList();
List<FileInfo> newFileInfoList = new ArrayList<FileInfo>();
for (String filename : newFilenameList
) {
newFileInfoList.add(decodeFilename(filename));
}
return newFileInfoList;
}
@Transactional
@Override
public List<String> getNewFileList() {
List<String> systemFileList = Arrays.asList((new File(fileDir)).list());
List<String> datasetFileList = fileInfoDao.queryFileInfoListOnlyFilename();
Map<String, String> dataMap = new HashMap<String, String>();
List<String> newDataList = new ArrayList<>();
if (datasetFileList != null && !systemFileList.isEmpty()) {
System.out.println("数据库记录:" + datasetFileList.size());
for (String filename : datasetFileList) {
dataMap.put(filename, filename);
// System.out.println(filename);
}
System.out.println("系统文件:" + systemFileList.size());
for (String filename : systemFileList) {
if (!dataMap.containsKey(filename)) {
newDataList.add(filename);
}
// System.out.println(filename);
}
}
System.out.println("新增数据:" + newDataList.size());
// for (String filename : newDataList) {
// System.out.println(filename);
// }
return newDataList;
}
@Transactional
@Override
public void insertAllFileInfo() {
System.out.println(fileDir);
String[] fileList = new File(fileDir).list();
for (String filename : fileList) {
System.out.println(filename);
fileInfoDao.insertFileInfo(decodeFilename(filename));
}
}
@Transactional
@Override
public void insertNewFileInfo(List<FileInfo> newFileList) {
for (FileInfo newFile : newFileList) {
fileInfoDao.insertFileInfo(newFile);
}
}
}
package com.wsn640.slidewaydatainterface.service.impl;
import com.wsn640.slidewaydatainterface.dao.SubscriberDao;
import com.wsn640.slidewaydatainterface.entity.Subscriber;
import com.wsn640.slidewaydatainterface.service.SubscriberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SubscriberServiceImpl implements SubscriberService {
@Autowired
SubscriberDao subscriberDao;
@Override
public String registerSubscriber(String name, String ipPort) {
Subscriber subscriber = new Subscriber(ipPort, name);
boolean result = subscriberDao.insertSubscriber(subscriber);
if(result){
return " " + subscriber.getId();
}
return "register Failed";
}
@Override
public List<Subscriber> getSubscriberList() {
return subscriberDao.querySubscriberList();
}
// @Override
// public Subscriber searchSubcriberByIpPort(String ipPort) {
// return null;
// }
@Override
public String deleteSubscriber(Integer id) {
Subscriber subscriber = subscriberDao.querySubscriber(id);
boolean restult = subscriberDao.deleteSubscriber(subscriber);
if(restult){
return "Logout Success";
}
return "Logout Failed";
}
}
package com.wsn640.slidewaydatainterface.tool;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Tool {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
private static String zipDir = "E:\\2020\\slidewayzip_dir";
private static final int BUFFER_SIZE = 2 * 1024;
public static FileInfo decodeFilename(String filename){
String prefix = filename.substring(0, filename.lastIndexOf("."));
int fence = Integer.parseInt(prefix.substring(14, 17));
int type = Integer.parseInt(prefix.substring(17, 18));
int isDelete = 0;
Date date = null;
try{
date = sdf.parse(prefix.substring(0, 14));
}catch (ParseException e){
e.printStackTrace();
}
return new FileInfo(filename, date, fence, type, isDelete);
}
/**
* 压缩成ZIP
* @param filenameList 需要压缩的文件路径
* @param filenameList 需要压缩的文件名列表
* @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public static void toZip(String srcPath, List<String> filenameList, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (String filename : filenameList) {
File srcfile = new File(srcPath + "/" + filename);
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcfile.getName()));
int len;
FileInputStream in = new FileInputStream(srcfile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 生成压缩文件
* @param srcPath
* @param filenameList
* @return
*/
public static String toZipFile(String srcPath, List<String> filenameList){
String zipname = sdf.format(new Date()) + ".zip";
File file = new File(zipDir + "/" + zipname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file); //将FileOutputStream流对象连接到file1代表的文件
toZip(srcPath, filenameList, fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
if( fos != null )
try {
fos.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
return zipname;
}
/**
* 删除压缩文件
* @param zipname
*/
public static void deleteZip(String zipname){
File file = new File(zipDir + "/" + zipname);
file.delete();
}
/**
* 拷贝文件
*
* @param oldFilePath 源文件
* @param newPath 指定文件
*/
public static void copyFile(String oldFilePath, String newPath) throws IOException {
File oldFile = new File(oldFilePath);
File file = new File(newPath);
FileInputStream in = new FileInputStream(oldFile);
FileOutputStream out = new FileOutputStream(file);;
byte[] buffer=new byte[2097152];
while((in.read(buffer)) != -1){
out.write(buffer);
}
in.close();
out.close();
}
public static void main(String[] args){
deleteZip("20200605053616.zip");
}
}
# server config
server.port=8088
server.servlet.context-path=/slideway
# dataSource
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/slideway?serverTimezone=UTC&&useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123456
# Mybatis
mybatis_config_file=mybatis-config.xml
mapper_path=/mapper/**.xml
entity_package=com.wsn640.slidewaydatainterface.entity
# path config
load_path=E:\\2020\\slideway-test
date_time_pattern=yyyyMMddHHmmss
\ No newline at end of file
<?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.wsn640.slidewaydatainterface.dao.FileInfoDao">
<select id="queryFileInfoById" resultType="com.wsn640.slidewaydatainterface.entity.FileInfo">
SELECT *
FROM file_info
WHERE id=#{id} and is_delete=0
</select>
<select id="queryFileInfoByFileName" resultType="com.wsn640.slidewaydatainterface.entity.FileInfo">
SELECT *
FROM file_info
WHERE filename=#{filename} and is_delete=0
</select>
<select id="queryFileInfoList" resultType="com.wsn640.slidewaydatainterface.entity.FileInfo">
SELECT *
FROM file_info
where is_delete=0
ORDER BY id
</select>
<select id="queryFileInfoListOnlyFilename" resultType="String" parameterType="map">
SELECT filename
FROM file_info
where is_delete=0
ORDER BY id
</select>
<select id="queryFileInfoListByDate" resultType="com.wsn640.slidewaydatainterface.entity.FileInfo">
SELECT *
FROM file_info
where in_date_time > #{st} and in_date_time > #{ed} and is_delete=0
ORDER BY id
</select>
<select id="queryFileInfoListByFence" resultType="com.wsn640.slidewaydatainterface.entity.FileInfo">
SELECT *
FROM file_info
where fence=#{fence} and is_delete=0
ORDER BY id
</select>
<insert id="insertFileInfo" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.wsn640.slidewaydatainterface.entity.FileInfo">
INSERT INTO file_info(filename, fence, in_date_time, file_type, is_delete)
VALUES (#{filename}, #{fence}, #{inDateTime}, #{fileType}, #{isDelete})
</insert>
<update id="updateFileInfoById" parameterType="com.wsn640.slidewaydatainterface.entity.FileInfo">
UPDATE file_info
<set>
<if test="filename!=null">filename=#{filename},</if>
<if test="fence!=null">fence=#{fence},</if>
<if test="inDateTime!=null">in_date_time=#{inDateTime},</if>
<if test="fileType!=null">file_type=#{fileType},</if>
<if test="isDelete!=null">is_delete=#{isDelete}</if>
</set>
WHERE id=#{id}
</update>
<update id="updateFileInfoByFilename" parameterType="com.wsn640.slidewaydatainterface.entity.FileInfo">
UPDATE file_info
<set>
<if test="fence!=null">fence=#{fence},</if>
<if test="inDateTime!=null">in_date_time=#{inDateTime},</if>
<if test="fileType!=null">file_type=#{fileType},</if>
<if test="isDelete!=null">is_delete=#{isDelete}</if>
</set>
WHERE filename=#{filename}
</update>
<update id="deleteFileInfo" parameterType="com.wsn640.slidewaydatainterface.entity.FileInfo">
UPDATE file_info
<set>
<if test="fence!=null">fence=#{fence},</if>
<if test="inDateTime!=null">in_date_time=#{inDateTime},</if>
<if test="fileType!=null">file_type=#{fileType},</if>
<if test="isDelete!=null">is_delete=1</if>
</set>
WHERE filename=#{filename}
</update>
</mapper>
\ No newline at end of file
<?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.wsn640.slidewaydatainterface.dao.SubscriberDao">
<select id="querySubscriber" resultType="com.wsn640.slidewaydatainterface.entity.Subscriber">
SELECT *
FROM subscriber
WHERE id=#{id} and is_delete=0
</select>
<select id="querySubsriberByName" resultType="com.wsn640.slidewaydatainterface.entity.Subscriber">
SELECT *
FROM subscriber
WHERE subcriber_name=#{subscriberName} and is_delete=0
</select>
<select id="querySubsriberByIpPort" resultType="com.wsn640.slidewaydatainterface.entity.Subscriber">
SELECT *
FROM subscriber
WHERE ip_port=#{ipPort} and is_delete=0
</select>
<select id="querySubscriberList" resultType="com.wsn640.slidewaydatainterface.entity.Subscriber">
SELECT *
FROM subscriber
where is_delete=0
ORDER BY id
</select>
<insert id="insertSubscriber" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.wsn640.slidewaydatainterface.entity.Subscriber">
INSERT INTO subscriber(ip_port, create_time, subcriber_name, is_delete)
VALUES (#{ipPort}, #{createTime}, #{subscriberName}, #{isDelete})
</insert>
<update id="deleteFileInfo" parameterType="com.wsn640.slidewaydatainterface.entity.Subscriber">
UPDATE subscriber
<set>
<if test="ipPort!=null">ip_port=#{ipPort},</if>
<if test="createTime!=null">create_time=#{createTime},</if>
<if test="SubscriberName!=null">subcriber_name=#{subscriberName},</if>
<if test="isDelete!=null">is_delete=1</if>
</set>
WHERE id=#{id}
</update>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org// DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列标签替换别名 默认:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 开启驼峰命名转换: Table{create_time}->Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
package com.wsn640.slidewaydatainterface;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SlidewayDataInterfaceApplicationTests {
@Test
void contextLoads() {
}
}
package com.wsn640.slidewaydatainterface.dao;
import com.wsn640.slidewaydatainterface.entity.FileInfo;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.platform.commons.util.CollectionUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.lang.reflect.Array;
import java.util.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileInfoDaoTest {
@Autowired
private FileInfoDao fileInfoDao;
@Test
@Ignore
public void queryFileInfoById() {
}
@Test
@Ignore
public void queryFileInfoByFileName() {
}
@Test
@Ignore
public void queryFileInfoList() {
List<FileInfo> fileInfoList = fileInfoDao.queryFileInfoList();
System.out.println(fileInfoList.size());
for (FileInfo fileInfo: fileInfoList
) {
System.out.println(fileInfo.getId());
System.out.println(fileInfo.getFence());
System.out.println(fileInfo.getInDateTime());
System.out.println(fileInfo.getIsDelete());
System.out.println(fileInfo.getFileType());
}
}
@Test
@Ignore
public void queryFileInfoListByDate() {
}
@Test
@Ignore
public void queryFileInfoListByFence() {
}
@Test
@Ignore
public void insertFileInfo() {
}
@Test
@Ignore
public void updateFileInfoById() {
}
@Test
@Ignore
public void updateFileInfoByFilename() {
}
@Test
@Ignore
public void deleteFileInfo() {
}
@Test
public void queryFileInfoListOnlyFilename() {
String path = "E:\\2020\\slideway-test";
List<String> systemFileList = Arrays.asList(new File(path).list());
List<String> datasetFileList = fileInfoDao.queryFileInfoListOnlyFilename();
Map<String, String> dataMap = new HashMap<String, String>();
List<String> newDataList = new ArrayList<>();
if(datasetFileList != null && !systemFileList.isEmpty()){
System.out.println("数据库记录" + datasetFileList.size());
for (String filename : datasetFileList){
dataMap.put(filename, filename);
System.out.println(filename);
}
System.out.println("______________");
System.out.println("系统文件" + systemFileList.size());
for(String filename: systemFileList){
if(!dataMap.containsKey(filename)){
newDataList.add(filename);
}
System.out.println(filename);
}
}
System.out.println("______________");
System.out.println(newDataList.size());
for(String filename: newDataList){
System.out.println(filename);
}
}
}
\ No newline at end of file
package com.wsn640.slidewaydatainterface.service.impl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SlidewayDataServiceImplTest {
@Test
public void getFileList() {
SlidewayDataServiceImpl slidewayDataService = new SlidewayDataServiceImpl();
System.out.println(slidewayDataService.getFileDir());
}
@Test
public void uncodeFileName(){
String path = "E:\\2020\\slideway-test";
String[] fileList = new File(path).list();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
for (String fileNnameTmp: fileList) {
String prefix = fileNnameTmp.substring(0, fileNnameTmp.lastIndexOf("."));
int num = Integer.parseInt(prefix.substring(14, 17));
int type = Integer.parseInt(prefix.substring(17, 18));
Date date = null;
try{
date = sdf.parse(prefix.substring(0, 14));
}catch (ParseException e){
e.printStackTrace();
}
String reserve = fileNnameTmp.substring(18);
System.out.println(prefix + " " + prefix.length() + " " + date + " " + num + " " + type + " " + reserve);
}
}
}
\ 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