init:宁夏武警跳转初始化

This commit is contained in:
Yuan
2025-09-15 18:52:59 +08:00
commit 53777df214
34 changed files with 5640 additions and 0 deletions

33
.gitignore vendored Normal file
View File

@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### 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/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

310
mvnw vendored Normal file
View File

@@ -0,0 +1,310 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven Start Up Batch script
#
# Required ENV vars:
# ------------------
# JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
# M2_HOME - location of maven2's installed home dir
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
# e.g. to debug Maven itself, use
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
# ----------------------------------------------------------------------------
if [ -z "$MAVEN_SKIP_RC" ] ; then
if [ -f /etc/mavenrc ] ; then
. /etc/mavenrc
fi
if [ -f "$HOME/.mavenrc" ] ; then
. "$HOME/.mavenrc"
fi
fi
# OS specific support. $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
mingw=false
case "`uname`" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true;;
Darwin*) darwin=true
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
export JAVA_HOME="`/usr/libexec/java_home`"
else
export JAVA_HOME="/Library/Java/Home"
fi
fi
;;
esac
if [ -z "$JAVA_HOME" ] ; then
if [ -r /etc/gentoo-release ] ; then
JAVA_HOME=`java-config --jre-home`
fi
fi
if [ -z "$M2_HOME" ] ; then
## resolve links - $0 may be a link to maven's home
PRG="$0"
# need this for relative symlinks
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG="`dirname "$PRG"`/$link"
fi
done
saveddir=`pwd`
M2_HOME=`dirname "$PRG"`/..
# make it fully qualified
M2_HOME=`cd "$M2_HOME" && pwd`
cd "$saveddir"
# echo Using m2 at $M2_HOME
fi
# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --unix "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi
# For Mingw, ensure paths are in UNIX format before anything is touched
if $mingw ; then
[ -n "$M2_HOME" ] &&
M2_HOME="`(cd "$M2_HOME"; pwd)`"
[ -n "$JAVA_HOME" ] &&
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
fi
if [ -z "$JAVA_HOME" ]; then
javaExecutable="`which javac`"
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
# readlink(1) is not available as standard on Solaris 10.
readLink=`which readlink`
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
if $darwin ; then
javaHome="`dirname \"$javaExecutable\"`"
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
else
javaExecutable="`readlink -f \"$javaExecutable\"`"
fi
javaHome="`dirname \"$javaExecutable\"`"
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
JAVA_HOME="$javaHome"
export JAVA_HOME
fi
fi
fi
if [ -z "$JAVACMD" ] ; then
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
else
JAVACMD="`which java`"
fi
fi
if [ ! -x "$JAVACMD" ] ; then
echo "Error: JAVA_HOME is not defined correctly." >&2
echo " We cannot execute $JAVACMD" >&2
exit 1
fi
if [ -z "$JAVA_HOME" ] ; then
echo "Warning: JAVA_HOME environment variable is not set."
fi
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
if [ -z "$1" ]
then
echo "Path not specified to find_maven_basedir"
return 1
fi
basedir="$1"
wdir="$1"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
if [ -d "${wdir}" ]; then
wdir=`cd "$wdir/.."; pwd`
fi
# end of workaround
done
echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "$(tr -s '\n' ' ' < "$1")"
fi
}
BASE_DIR=`find_maven_basedir "$(pwd)"`
if [ -z "$BASE_DIR" ]; then
exit 1;
fi
##########################################################################################
# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
# This allows using the maven wrapper in projects that prohibit checking in binary data.
##########################################################################################
if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found .mvn/wrapper/maven-wrapper.jar"
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
fi
if [ -n "$MVNW_REPOURL" ]; then
jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
else
jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
fi
while IFS="=" read key value; do
case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
esac
done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
if [ "$MVNW_VERBOSE" = true ]; then
echo "Downloading from: $jarUrl"
fi
wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
if $cygwin; then
wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
fi
if command -v wget > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found wget ... using wget"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
wget "$jarUrl" -O "$wrapperJarPath"
else
wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath"
fi
elif command -v curl > /dev/null; then
if [ "$MVNW_VERBOSE" = true ]; then
echo "Found curl ... using curl"
fi
if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
curl -o "$wrapperJarPath" "$jarUrl" -f
else
curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
fi
else
if [ "$MVNW_VERBOSE" = true ]; then
echo "Falling back to using Java to download"
fi
javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
# For Cygwin, switch paths to Windows format before running javac
if $cygwin; then
javaClass=`cygpath --path --windows "$javaClass"`
fi
if [ -e "$javaClass" ]; then
if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Compiling MavenWrapperDownloader.java ..."
fi
# Compiling the Java class
("$JAVA_HOME/bin/javac" "$javaClass")
fi
if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
# Running the downloader
if [ "$MVNW_VERBOSE" = true ]; then
echo " - Running MavenWrapperDownloader.java ..."
fi
("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
fi
fi
fi
fi
##########################################################################################
# End of extension
##########################################################################################
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
if [ "$MVNW_VERBOSE" = true ]; then
echo $MAVEN_PROJECTBASEDIR
fi
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
[ -n "$M2_HOME" ] &&
M2_HOME=`cygpath --path --windows "$M2_HOME"`
[ -n "$JAVA_HOME" ] &&
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
[ -n "$CLASSPATH" ] &&
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
export MAVEN_CMD_LINE_ARGS
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
exec "$JAVACMD" \
$MAVEN_OPTS \
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

182
mvnw.cmd vendored Normal file
View File

@@ -0,0 +1,182 @@
@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%

114
pom.xml Normal file
View File

@@ -0,0 +1,114 @@
<?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.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.saye</groupId>
<artifactId>hgd-dmz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>hgd-dmz</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- provided打包时移除tomcat组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--ftp下载用到-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
<!--支付宝-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.16.2.ALL</version>
</dependency>
<!--微信-->
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.2</version>
</dependency>
<!--httpClient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
</dependency>
<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.28</version>
</dependency>
</dependencies>
<build>
<!-- 项目名 默认为项目名+版本号+SNAPSHOT -->
<finalName>ROOT</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package com.saye.hgddmz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HgdDmzApplication {
public static void main(String[] args) {
SpringApplication.run(HgdDmzApplication.class, args);
}
}

View File

@@ -0,0 +1,13 @@
package com.saye.hgddmz;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HgdDmzApplication.class);
}
}

View File

@@ -0,0 +1,120 @@
package com.saye.hgddmz.commons;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
/**
* 用于封装AJAX调用以后的JSON返回值
* 其中正确返回值:
* {state:0, data:返回数据, message:错误消息}
* 错误返回值:
* {state:1, data:null, message:错误消息}
**/
public class JsonResult implements Serializable {
private static final long serialVersionUID = -3644950655568598241L;
//定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 返回是否成功的状态, 0表示成功,
* 1或其他值 表示失败
*/
private boolean state;
/**
* 成功时候,返回的JSON数据
*/
private Object data;
/**
* 是错误时候的错误消息
*/
private String message;
public JsonResult() {
}
public JsonResult(boolean state, Object data, String message) {
this.state = state;
this.data = data;
this.message = message;
}
public JsonResult(Throwable e){
state = false;
data=null;
message=e.getMessage();
}
public JsonResult(Object data){
state = true;
this.data=data;
message="";
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public static String jsonResultSuccess(Object data, String message){
JsonResult jsonResult=new JsonResult();
jsonResult.setState(true);
jsonResult.setMessage(message);
jsonResult.setData(data);
return ObjectToJson(jsonResult);
}
public static String jsonResultFalse(String message){
JsonResult jsonResult=new JsonResult();
jsonResult.setState(false);
jsonResult.setMessage(message);
return ObjectToJson(jsonResult);
}
/**
* 将对象转化为json字符串
* @param data
* @return
*/
public static String ObjectToJson(Object data){
try{
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
return "JsonResult [state=" + state + ", data=" + data + ", message=" + message + "]";
}
}

View File

@@ -0,0 +1,95 @@
package com.saye.hgddmz.commons;
import org.apache.commons.codec.CharEncoding;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
/**
* <p>
* token生成工具类
* </p>
*
* @author caoshiyan
* @version V1.0
* @date 2015年12月29日 上午10:16:14
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify by user: {修改人} 2015年12月29日
* @since
*/
public class TokenGenerateUtil {
/**
* <p>
* MD5加密工具类
* </p>
* @author caoshiyan
* @version V1.0
* @date 2015年11月30日 下午4:40:16
* @param s
* @return
*
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify by user: {修改人} 2015年11月30日
* @modify by reason:{方法名}:{原因}
* @since
*/
public final static String md5(String s) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
try {
// 最重要的是这句,需要加上编码类型
mdTemp.update(s.getBytes(CharEncoding.UTF_8));
} catch (UnsupportedEncodingException e) {
mdTemp.update(s.getBytes());
}
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str).toUpperCase();
} catch (Exception e) {
return null;
}
}
/**
* <p>
* 生成token
* </p>
* @author caoshiyan
* @version V1.0
* @date 2015年11月18日 下午6:28:01
* @param url GET请求URL带参数串POST请求URL不带参数串参数以JSON格式传入paramJson
* @param paramJson POST参数JSON格式
* @param secret 加密secret
* @return String 生成的token值
*
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify by user: {修改人} 2015年11月18日
* @modify by reason:{方法名}:{原因}
* @since
*/
public final static String buildToken(String url, String paramJson, String secret) {
String tempUrl = null;
tempUrl = url.substring("http://".length());
int index = tempUrl.indexOf("/");
String URI = tempUrl.substring(index);
String[] ss = URI.split("\\?");
if (ss.length > 1) {
return md5(ss[0] + ss[1] + secret);
} else {
return md5(ss[0] + paramJson + secret);
}
}
public static void main(String[] args) {
System.out.println(md5("/webapi/service/vss/getPlatEncodeDeviceResList{\"pageNo\":1,\"pageSize\":1000,\"appkey\":\"8a3018ac\",\"time\":1610594097178}69befa1eca0644af8000fd23d7b8c0f7"));
}
}
///webapi/service/vss/getPlatEncodeDeviceResList{"pageNo":1,"pageSize":1000,"appkey":"8a3018ac","time":1610594044620}69befa1eca0644af8000fd23d7b8c0f7

View File

@@ -0,0 +1,296 @@
package com.saye.hgddmz.commons.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateDUtil {
public static String yyyy_MM_dd = "yyyy-MM-dd";
public static String yyyyMMdd = "yyyyMMdd";
public static String yyyyMM = "yyyyMM";
public static String yyyy_MM = "yyyy-MM";
public static String yyyy_MM_dd_HH_00 = "yyyy-MM-dd HH:00";
public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
public static String yyyy_MM_dd_HH_mm_ss_SS = "yyyy-MM-dd HH:mm:ss.SS";
public static String yyyyMMddHHmm = "yyyyMMddHHmm";
public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
public static String yyyyMMddHHmmssSS = "yyyyMMddHHmmssSS";
public static String yyMMdd = "yyMMdd";
public static String yyyy_MM_dd_00_00 = "yyyy-MM-dd 00:00";
/**
* 将字符串时间改成Date类型
* @param format
* @param dateStr
* @return
*/
public static Date strToDate(String format,String dateStr) {
Date date = null;
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
date = simpleDateFormat.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 将Date时间转成字符串
* @param format
* @param date
* @return
*/
public static String DateToStr(String format,Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
/**
* 获取2个字符日期的天数差
* @param p_startDate
* @param p_endDate
* @return 天数差
*/
public static long getDaysOfTowDiffDate( String p_startDate, String p_endDate ){
Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, p_startDate);
Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, p_endDate);
long l_startTime = l_startDate.getTime();
long l_endTime = l_endDate.getTime();
long betweenDays = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60 * 60 * 24 ) );
return betweenDays;
}
/**
* 获取2个字符日期的天数差
* @param l_startDate
* @param l_endDate
* @return 天数差
*/
public static long getDaysOfTowDiffDate( Date l_startDate, Date l_endDate ){
long l_startTime = l_startDate.getTime();
long l_endTime = l_endDate.getTime();
long betweenDays = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60 * 60 * 24 ) );
return betweenDays;
}
/**
* 给出日期添加一段时间后的日期
* @param dateStr
* @param plus
* @return
*/
public static String getPlusDays(String format,String dateStr,long plus){
Date date = DateDUtil.strToDate(format, dateStr);
long time = date.getTime()+ plus*24*60*60*1000;
return DateDUtil.DateToStr(format,new Date(time));
}
/**
* 给出日期添加一段时间后的日期
* @param format
* @param date
* @param plus
* @return
*/
public static String getPlusDays(String format,Date date,long plus){
long time = date.getTime()+ plus*24*60*60*1000;
return DateDUtil.DateToStr(format,new Date(time));
}
/**
* 给出日期添加一段时间前的日期
* @param format
* @param date
* @param forth
* @return
*/
public static String getForthDays(String format,Date date,long forth){
long time = date.getTime()- forth*24*60*60*1000;
return DateDUtil.DateToStr(format,new Date(time));
}
/**
* 给出时间添加几个小时后的时间
* @param format
* @param dateStr
* @param plus
* @return
*/
public static String getPlusHours(String format,String dateStr,long plus){
Date date = DateDUtil.strToDate(format, dateStr);
long time = date.getTime()+ plus*60*60*1000;
return DateDUtil.DateToStr(format,new Date(time));
}
/**
* 给出时间添加几个分钟后的时间
* @param format
* @param dateStr
* @param plus
* @return
*/
public static String getPlusMinutes(String format,String dateStr,long plus){
Date date = DateDUtil.strToDate(format, dateStr);
long time = date.getTime()+ plus*60*1000;
return DateDUtil.DateToStr(format,new Date(time));
}
/**
* 得到当前时间格式如yyyy-MM-dd HH:mm:ss:SS
* @return
*/
public static String getCurrentTime(){
String nowTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, new Date());
return nowTime;
}
/**
* 得到当前时间格式如yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getTheCurrentTime(){
String nowTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd_HH_mm_ss, new Date());
return nowTime;
}
/**
* 得到当前日期格式如yyyyMMdd
* @return
*/
public static String getCurrentDate(){
String nowDate = DateDUtil.DateToStr(DateDUtil.yyyyMMdd, new Date());
return nowDate;
}
/**
* 得到当前日期格式如yyyyMMdd
* @return
*/
public static String getCurrentDate(String format){
String nowDate = DateDUtil.DateToStr(format, new Date());
return nowDate;
}
/**
* 获取2个字符日期的分钟数差
* @param p_startDate
* @param p_endDate
* @return 相差的分钟
*/
public static long getMinutesOfTowDiffDate(String p_startDate, String p_endDate ){
Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, p_startDate);
Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, p_endDate);
long l_startTime = l_startDate.getTime();
long l_endTime = l_endDate.getTime();
long betweenMinutes = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60) );
return betweenMinutes;
}
/**
* 获取2个字符日期的分钟数差
* @param p_startDate
* @param p_endDate
* @return 相差的分钟
*/
public static long getMinutesOfTowDiffDateMin(String format,String p_startDate, String p_endDate ){
Date l_startDate = DateDUtil.strToDate(format, p_startDate);
Date l_endDate = DateDUtil.strToDate(format, p_endDate);
long l_startTime = l_startDate.getTime();
long l_endTime = l_endDate.getTime();
long betweenMinutes = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60) );
return betweenMinutes;
}
public static long getMonthIntervalOfTowDiffDate(String p_startMonth, String p_endMonth){
Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM, p_startMonth);
Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM, p_endMonth);
Calendar calender = Calendar.getInstance();
calender.setTime(l_startDate);
long l_startMonth = calender.get(Calendar.MONTH)+1;
long l_startYear = calender.get(Calendar.YEAR);
calender.setTime(l_endDate);
long l_endMonth =calender.get(Calendar.MONTH)+1;
long l_endYear = calender.get(Calendar.YEAR);
long betweenYear = l_endYear - l_startYear;
long betweenMonth = (long) ( l_endMonth - l_startMonth ) ;
return betweenYear * 12 + betweenMonth;
}
/**
* 将字符串日期转为cron表达式
*/
public static String getCron(String execute_time) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
Date date = simpleDateFormat.parse(execute_time);
SimpleDateFormat dateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
return dateFormat.format(date);
}
/**
* 获取当前年月日日期
*/
public static String getCDate(){
Calendar now = Calendar.getInstance();
String year=now.get(Calendar.YEAR)+"";
String month=(now.get(Calendar.MONTH) + 1) + "";
String day=now.get(Calendar.DAY_OF_MONTH)+"";
if ((now.get(Calendar.MONTH) + 1) < 10) month = "0" + month;
if (now.get(Calendar.DAY_OF_MONTH) < 10) day= "0" + day;
String nowDate = year+""+month+""+day+"";
return nowDate;
}
}

View File

@@ -0,0 +1,31 @@
package com.saye.hgddmz.commons.encrypt;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptUtil {
// AES ecb模式解密 key:秘钥 initVector:偏移量 encrypted加密内容
public static String decrypt(String key,String initVector,String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
System.out.println(decrypt("a6xdabhysfescfbu","encryptionIntVec","oYzamqnnyJ8GG6646PDYBQ=="));
}
}

View File

@@ -0,0 +1,37 @@
package com.saye.hgddmz.commons.entity;
import java.util.List;
public class DepartEntity {
private String id;
private String title;
private boolean spread;
private List<DepartEntity> children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSpread() {
return spread;
}
public void setSpread(boolean spread) {
this.spread = spread;
}
public List<DepartEntity> getChildren() {
return children;
}
public void setChildren(List<DepartEntity> children) {
this.children = children;
}
}

View File

@@ -0,0 +1,37 @@
package com.saye.hgddmz.commons.entity;
import java.util.List;
public class RoleEntity {
private String id;
private String title;
private boolean spread;
private List<RoleEntity> children;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSpread() {
return spread;
}
public void setSpread(boolean spread) {
this.spread = spread;
}
public List<RoleEntity> getChildren() {
return children;
}
public void setChildren(List<RoleEntity> children) {
this.children = children;
}
}

View File

@@ -0,0 +1,785 @@
package com.saye.hgddmz.commons.excel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class ExportXLS implements IExport ,ISetExport{
Log logger = LogFactory.getLog(this.getClass());
public final static short ROWHEIGHT = 20; /*行高*/
public final static double PAGEMARGIN = 0.1;/*页边距*/
public final static short TITLESIZE = 16;/*标题文字大小*/
public final static short HEADERSIZE = 12;/*列头文字大小*/
public final static short DATA_CHARACTERSIZE = 9;/*汉字大小*/
public final static short DATA_NUMSIZE = 9;/*数字大小*/
public final static short DATA_DATESIZE = 9;/*日期大小*/
public final static String ALIGN_LEFT = "LEFT";/*水平居左*/
public final static String ALIGN_RIGHT = "RIGHT";/*水平居右*/
public final static String ALIGN_CENTER = "CENTER";/*水平居右*/
public final static String VERTICAL_TOP = "TOP";/*垂直居上*/
public final static String VERTICAL_CENTER = "MIDDLE";/*垂直居中*/
public final static String VERTICAL_BOTTOM = "BOTTOM";/*垂直居下*/
public final static short A4 = HSSFPrintSetup.A4_PAPERSIZE;
public final static short A5 = HSSFPrintSetup.A5_PAPERSIZE;
public final static short A3 = 8;
public final static String MERGEDEND = "_mergedend_";//表示合并次单元格
private List<Object> data;
private Object[] statisticData;
private String titleName;
private short titleHeight = 500;
private short headerHeight = 300;
private short cellHeight = 300;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFCellStyle headerCellStyle;
private HSSFCellStyle cellStyle;
private String[] header;
private String[] subHeader;
private String[] width;
private String[] sqlKey;
private IConversionByExport conversion;
private List<int[]> mergeRanges = new ArrayList<int[]>();
private Boolean isHeaderMergeTwo = Boolean.FALSE;
private int headerRowIndex = 1;
private int subHeaderRowIndex = 2;
private int titleMergeColumnIndex = 0;
private HashMap<Integer, Integer> modifiedDataHeightHM = new HashMap<Integer, Integer>(); //要修改的数据行的高度
private short subHeaderHeight = 300;
private Boolean isCreateFreezePane = true;
private HashMap<Integer, Integer> skipedDataIndexMap; //要空行的数据列序号:KEY为data的序号 VALUE默认为1
private List<HashMap<Integer, Integer>> resetCellStyleList = new ArrayList<HashMap<Integer,Integer>>(); //要重新设置样式的单元格:KEY为行号,VALUE为列号
private HSSFCellStyle resetCellStyle;
/**
*
*构造函数:得到一个纵向打印在A4纸上的xls
*
*/
public ExportXLS(String[] header){
this.wb = new HSSFWorkbook();
this.sheet = this.createSheet(false, ExportXLS.A4);
this.headerCellStyle = this.createCellStyle(ExportXLS.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new HSSFColor.GREY_25_PERCENT().getIndex());
this.cellStyle = this.createCellStyle(ExportXLS.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new HSSFColor.WHITE().getIndex());
this.setTitleName("export");
this.setHeader(header);
}
/**
* 设置打印纸大小,横向还是纵向
* @param header
* @param sqlKey
* @param pageSize
* @param landscapeFlag
*/
public ExportXLS(String[] header,String[] sqlKey,Short pageSize,boolean landscapeFlag){
this.wb = new HSSFWorkbook();
this.sheet = this.createSheet(landscapeFlag, pageSize);
this.headerCellStyle = this.createCellStyle(ExportXLS.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new HSSFColor.GREY_25_PERCENT().getIndex());
HSSFFont titlefont = this.wb.createFont(); //设置字体
titlefont.setFontHeightInPoints(ExportXLS.HEADERSIZE);
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerCellStyle.setFont(titlefont);
this.cellStyle = this.createCellStyle(ExportXLS.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new HSSFColor.WHITE().getIndex());
this.setTitleName("export");
this.setHeader(header);
this.setSqlKey(sqlKey);
}
private void setHeader(String[] header){
this.header = header;
//默认设置每个单元格的宽度为3500
int length = this.header.length;
this.width = new String[length];
for(int i=0;i<length;i++){
this.width[i] = "3500";
}
}
public void setSqlKey(String[] sqlKey) {
this.sqlKey = sqlKey;
}
/**
* 创建工作簿
* @param
* @param landscapeFlag 横向标记 // true横向 false纵向
* @param pageSize 纸张大小
* @return
*/
private HSSFSheet createSheet(boolean landscapeFlag,Short pageSize) {
HSSFPrintSetup ps = null;
HSSFSheet sheet = null;
sheet = this.wb.createSheet();
sheet.setHorizontallyCenter(true);
sheet.setMargin(HSSFSheet.LeftMargin,PAGEMARGIN); //设置页边距
sheet.setMargin(HSSFSheet.RightMargin,PAGEMARGIN);
sheet.setMargin(HSSFSheet.TopMargin,PAGEMARGIN*3);
sheet.setMargin(HSSFSheet.BottomMargin,PAGEMARGIN*3);
ps = sheet.getPrintSetup();
ps.setLandscape(landscapeFlag);
ps.setPaperSize(pageSize);
return sheet;
}
/**
* 创建单元格格式
*/
private HSSFCellStyle createCellStyle(String alignPosition,String verticalPosotion,Integer border,short fontSize,short color) {
HSSFCellStyle cellStyle = wb.createCellStyle();
/*
* 设置水平位置
*/
if(alignPosition.equals(ALIGN_LEFT)){
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
}
if(alignPosition.equals(ALIGN_RIGHT)){
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
}
if(alignPosition.equals(ALIGN_CENTER)){
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
}
/*
* 设置垂直位置
*/
if(alignPosition.equals(VERTICAL_TOP)){
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_TOP);
}
if(alignPosition.equals(VERTICAL_CENTER)){
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_CENTER);
}
if(alignPosition.equals(VERTICAL_BOTTOM)){
cellStyle.setAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
}
/*
* 设置边框
*/
if((border/1000)%10==1){
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
}
if((border/100)%10==1){
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
}
if((border/10)%10==1){
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
}
if(border%10==1){
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
}
/*
* 设置字体大小
*/
HSSFFont font = wb.createFont();
font.setFontHeightInPoints(fontSize);// 字体大小
cellStyle.setFont(font);
/*
* 设置背景色
*/
cellStyle.setFillForegroundColor(color);
cellStyle.setFillBackgroundColor(color);
cellStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS);
/*
* 自动换行
*/
cellStyle.setWrapText(true);
return cellStyle;
}
/**
* 根据不同的类型给cell赋值
*
* @param data 数据
* @param cell
* @throws SQLException
*/
private void obtainPropList(Object data, HSSFCell cell){
if(data == null){
data = "";
}
String type = data.getClass().getName();
if(type.equalsIgnoreCase("java.lang.String")) {
String value = (String)data;
cell.setCellValue(new HSSFRichTextString(value));
}else if(type.equalsIgnoreCase("java.lang.Integer") || type.equalsIgnoreCase("int")) {
int value = (Integer)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Long") || type.equalsIgnoreCase("long")) {
long value = (Long)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Float") || type.equalsIgnoreCase("float")){
float value = (Float)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Double") || type.equalsIgnoreCase("double")){
double value = (Double)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Byte") || type.equalsIgnoreCase("byte")) {
byte value = (Byte)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Character") || type.equalsIgnoreCase("char")) {
char value = (Character)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Boolean") || type.equalsIgnoreCase("boolean")) {
boolean value = (Boolean)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.math.BigDecimal")) {
BigDecimal value = (BigDecimal)data;
cell.setCellValue(value.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
}else if(type.equalsIgnoreCase("java.util.Date")) {
Date value = (Date)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.sql.Timestamp")) {
Timestamp timestamp = (Timestamp)data;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String value = dateFormat.format(timestamp.getTime());
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Short") || type.equalsIgnoreCase("short")) {
short value = (Short)data;
cell.setCellValue(value);
}
}
private void fillSheet(){
int addRowNum = 1;
if(!isHeaderMergeTwo){
addRowNum = 0;
}
if(isCreateFreezePane){
sheet.createFreezePane(0,this.headerRowIndex + addRowNum + 1);
}
//设置行宽
for(int i=0;i<this.width.length;i++){
this.sheet.setColumnWidth(i,Integer.parseInt(this.width[i]));
}
//填入抬头数据
HSSFRow titleRow = this.sheet.createRow(0);
titleRow.setHeight(this.titleHeight);
HSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue(new HSSFRichTextString(this.titleName));
HSSFCellStyle titleCellStyle = this.wb.createCellStyle(); //设置单元格style
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont titlefont = this.wb.createFont(); //设置字体
titlefont.setFontHeightInPoints(ExportXLS.TITLESIZE);
titlefont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleCellStyle.setFont(titlefont);
titleCellStyle.setWrapText(true);
titleCell.setCellStyle(titleCellStyle);
if(0 == this.titleMergeColumnIndex){
this.titleMergeColumnIndex = this.header.length-1;
}
this.sheet.addMergedRegion(new CellRangeAddress(0,addRowNum,0,this.titleMergeColumnIndex));
//填入标题数据
HSSFRow headerRow = this.sheet.createRow(this.headerRowIndex);
headerRow.setHeight(this.headerHeight);
for(int i=0;i<this.header.length;i++){
HSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
this.obtainPropList(this.header[i], headerCell);
}
Integer dataBeginIndex = this.headerRowIndex + 1;
if(null != subHeader && 0 != subHeader.length){
if(this.subHeader.length > this.header.length){
for (int i = this.header.length; i < this.subHeader.length; i++) {
HSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
headerCell.setCellValue("");
}
}
//填入副标题数据
HSSFRow subHeaderRow = this.sheet.createRow(this.subHeaderRowIndex);
subHeaderRow.setHeight(this.subHeaderHeight);
for(int i=0;i<this.subHeader.length;i++){
HSSFCell headerCell = subHeaderRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
this.obtainPropList(this.subHeader[i], headerCell);
}
if(isCreateFreezePane){
this.sheet.createFreezePane(0,this.subHeaderRowIndex+1);
}
dataBeginIndex++;
}
//填入数据
if(this.data != null){
for(int i=0;i<this.data.size();i++){
Boolean tf = true;
if(null != skipedDataIndexMap){
if(!skipedDataIndexMap.isEmpty() && null != skipedDataIndexMap.get(i)){
tf = false;
}
}
if(tf){
//将数据转为数组类型数据
Object[] array = null;
//如果不存在转换对象,则使用默认的转换方法
if(this.conversion != null){
array = this.conversion.conversion(this.data.get(i), this.sqlKey);
}else{
array = this.conversion(this.data.get(i));
}
HSSFRow dataRow = this.sheet.createRow(dataBeginIndex+i);
dataRow.setHeight(this.cellHeight);
if(null != this.modifiedDataHeightHM){
Integer rowHeight = this.modifiedDataHeightHM.get(i);
if(null != rowHeight){
dataRow.setHeight(Short.valueOf(rowHeight.toString()));
}
}
for(int j=0;j<array.length;j++){
HSSFCell dataCell = dataRow.createCell(j);
dataCell.setCellStyle(this.cellStyle);
this.obtainPropList(array[j],dataCell);
}
}else {
HSSFRow dataRow = this.sheet.createRow(dataBeginIndex+i);
}
}
}
if(null != resetCellStyleList && !resetCellStyleList.isEmpty()){
int len = resetCellStyleList.size();
for (int i = 0; i < len; i++) {
HashMap<Integer, Integer> resetCellStyleHM = resetCellStyleList.get(i);
Iterator<Map.Entry<Integer, Integer>> it = resetCellStyleHM.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
Integer rowIndex = entry.getKey();
Integer columnIndex = entry.getValue();
this.sheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(this.resetCellStyle);
}
}
}
//设置合并开始标识
boolean mergedStartFlag = false;
int[] startAndEnd = null;
List mergedIndex = new ArrayList();
if(null != this.statisticData){
//追加统计行
HSSFRow appendRow = this.sheet.createRow(this.sheet.getLastRowNum()+1);
for(int i=0;i<this.statisticData.length;i++){
//记录合并开始位置
if(!mergedStartFlag){
mergedStartFlag = true;
startAndEnd = new int[2];
startAndEnd[0] = i;
}
//记录合并结束位置
if(ExportXLS.MERGEDEND.equals(this.statisticData[i]) && mergedStartFlag){
mergedStartFlag = false;
startAndEnd[1] = i;
mergedIndex.add(startAndEnd);
}
//生成单元格,如果是合并单元格,内容置空
HSSFCell dataCell = appendRow.createCell(i);
dataCell.setCellStyle(this.cellStyle);
if(!ExportXLS.MERGEDEND.equals(this.statisticData[i])){
this.obtainPropList(this.statisticData[i],dataCell);
}
}
}
//根据合并位置记录 合并单元格
for(int i=0;i<mergedIndex.size();i++){
startAndEnd = (int[])mergedIndex.get(i);
this.sheet.addMergedRegion(new CellRangeAddress(this.sheet.getLastRowNum(),this.sheet.getLastRowNum(),startAndEnd[0],startAndEnd[1]));
}
//处理要合并的单元格区域
if(null != this.mergeRanges && 0 != this.mergeRanges.size()){
for (int i = 0; i < this.mergeRanges.size(); i++) {
int[] ranges = this.mergeRanges.get(i);
this.sheet.addMergedRegion(new CellRangeAddress(ranges[0], ranges[1], ranges[2], ranges[3]));
}
}
}
/**
* 默认数据转换方法(只转换数组类型对象)
* @return
*/
private Object[] conversion(Object obj){
try {
return (Object[])obj;
} catch (ClassCastException e) {
if(this.logger.isErrorEnabled()){
this.logger.error("导出工具转换数据对象错误!要转换的数据不是数组型的!");
}
}
return null;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setConversion(IConversionByExport conversion)
*/
public void setConversion(IConversionByExport conversion){
this.conversion = conversion;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setTitleName(java.lang.String)
*/
public void setTitleName(String titleName){
this.titleName = titleName;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifyHeader(java.lang.String, int)
*/
public void modifyHeader(String header, int index){
this.header[index] = header;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifyWidthOfHeader(java.lang.String, int)
*/
public void modifyWidthOfHeader(String width, int index){
this.width[index] = width;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setData(java.util.List)
*/
public void setData(List<Object> dataList){
this.data = dataList;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#appendStatisticRow(Object StatisticData)
*/
public void appendStatisticRow(Object statisticData) {
//如果不存在转换对象,则使用默认的转换方法
if(this.conversion != null){
this.statisticData = this.conversion.conversion(statisticData, this.sqlKey);
}else{
this.statisticData = this.conversion(statisticData);
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#returnClientDownload(javax.servlet.http.HttpServletResponse)
*/
public void returnClientDownload(HttpServletResponse response) throws IOException{
//将数据装填到sheet中
this.fillSheet();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + new String(this.titleName.getBytes("gb2312"),"ISO8859-1")+".xls");
OutputStream os = null;
try {
os = response.getOutputStream();
this.wb.write(os);
} catch (Exception e) {
} finally {
if(os != null) {
os.close();
os = null;
}
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#returnClientOpen(javax.servlet.http.HttpServletResponse)
*/
public void returnClientOpen(HttpServletResponse response) throws IOException{
//将数据装填到sheet中
this.fillSheet();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","online; filename=" + new String(this.titleName.getBytes("gb2312"),"ISO8859-1")+".xls");
try {
wb.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifTitleHeight(short height)
*/
public void modifTitleHeight(short height) {
this.titleHeight = height;
}
/**
* 设置副标题
* @param subHeader
*/
public void setSubHeader(String[] subHeader){
this.subHeader = subHeader;
//默认设置每个单元格的宽度为3500
int headerLength = this.header.length;
int subHeaderLength = 0;
if(null != subHeader && 0 != subHeader.length){
subHeaderLength = this.subHeader.length;
}
int length = headerLength > subHeaderLength ? headerLength : subHeaderLength;
this.width = new String[length];
for(int i=0;i<length;i++){
this.width[i] = "3500";
}
}
/**
* 设置要合并的单元格区域
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
*/
public void setMergeRange(int firstRow, int lastRow, int firstCol, int lastCol){
int[] range = new int[4];
range[0] = firstRow;
range[1] = lastRow;
range[2] = firstCol;
range[3] = lastCol;
this.mergeRanges.add(range);
}
/*
*
*/
public void execGenerateExcel(String exportFilePath) throws IOException{
//将数据装填到sheet中
this.fillSheet();
// 设置输入流
FileOutputStream fOut = new FileOutputStream(exportFilePath);
// 将模板的内容写到输出文件上
wb.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
}
/**
* 设置头部高度
* @param height
*/
public void modifyHeaderHeight(short height) {
this.headerHeight = height;
}
/**
* 设置头部标题所处行序号
* @param headerRowIndex
*/
public void modifyHeaderRowIndex(short headerRowIndex) {
this.headerRowIndex = headerRowIndex;
}
/**
* 设置头部副标题序号
* @param subHeaderRowIndex
*/
public void modifySubHeaderRowIndex(short subHeaderRowIndex) {
this.subHeaderRowIndex = subHeaderRowIndex;
}
/**
* 设置标题合并列号
* @param titleMergeColumnIndex
*/
public void setTitleMergeColumnIndex(int titleMergeColumnIndex) {
this.titleMergeColumnIndex = titleMergeColumnIndex;
}
/**
* 设置数据行高
* @param modifiedDataHeightHM数据序号和行高
*/
public void setModifiedDataHeightHM(HashMap<Integer, Integer> modifiedDataHeightHM) {
this.modifiedDataHeightHM = modifiedDataHeightHM;
}
/**
* 设置头部副标题高度
* @param subHeaderHeight
*/
public void modifySubHeaderHeight(short subHeaderHeight) {
this.subHeaderHeight = subHeaderHeight;
}
/**
* 设置是否创建冻结窗体
* @param isCreateFreezePane
*/
public void setIsCreateFreezePane(Boolean isCreateFreezePane) {
this.isCreateFreezePane = isCreateFreezePane;
}
/**
* 设置要空行的数据列序号
* @param skipedDataIndexMap
*/
public void setSkipedDataIndexMap(HashMap<Integer, Integer> skipedDataIndexMap) {
this.skipedDataIndexMap = skipedDataIndexMap;
}
/**
* 设置 重新设置样式的单元格序号及样式:样式为空时,默认为头部标题样式
* @param resetCellStyleList
* @param resetCellStyle
*/
public void resetCellForCellStyle(List<HashMap<Integer, Integer>> resetCellStyleList, HSSFCellStyle resetCellStyle) {
this.resetCellStyleList = resetCellStyleList;
if(null == resetCellStyle){
resetCellStyle = this.headerCellStyle;
}
this.resetCellStyle = resetCellStyle;
}
/**
* 设置 工作薄 密码保护
* @param pwd
*/
public void protectSheet(String pwd) {
this.sheet.protectSheet(pwd);
}
/**
* 设定抬头是否合并两行:在new ExportXLS之后就SET
* @param isHeaderMergeTwo
*/
public void setIsHeaderMergeTwo(Boolean isHeaderMergeTwo) {
this.isHeaderMergeTwo = isHeaderMergeTwo;
if(!this.isHeaderMergeTwo){
this.headerRowIndex = 1;
this.subHeaderRowIndex = 2;
}
}
}

View File

@@ -0,0 +1,790 @@
package com.saye.hgddmz.commons.excel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
public class ExportXLSX implements IExport ,ISetExport{
Log logger = LogFactory.getLog(this.getClass());
public final static short ROWHEIGHT = 20; /*行高*/
public final static double PAGEMARGIN = 0.1;/*页边距*/
public final static short TITLESIZE = 16;/*标题文字大小*/
public final static short HEADERSIZE = 12;/*列头文字大小*/
public final static short DATA_CHARACTERSIZE = 9;/*汉字大小*/
public final static short DATA_NUMSIZE = 9;/*数字大小*/
public final static short DATA_DATESIZE = 9;/*日期大小*/
public final static String ALIGN_LEFT = "LEFT";/*水平居左*/
public final static String ALIGN_RIGHT = "RIGHT";/*水平居右*/
public final static String ALIGN_CENTER = "CENTER";/*水平居右*/
public final static String VERTICAL_TOP = "TOP";/*垂直居上*/
public final static String VERTICAL_CENTER = "MIDDLE";/*垂直居中*/
public final static String VERTICAL_BOTTOM = "BOTTOM";/*垂直居下*/
public final static short A4 = XSSFPrintSetup.A4_PAPERSIZE;
public final static short A5 = XSSFPrintSetup.A5_PAPERSIZE;
public final static short A3 = 8;
public final static String MERGEDEND = "_mergedend_";//表示合并次单元格
private List<Object> data;
private Object[] statisticData;
private String titleName;
private short titleHeight = 500;
private short headerHeight = 300;
private short cellHeight = 300;
private XSSFWorkbook wb;
private XSSFSheet sheet;
private XSSFCellStyle headerCellStyle;
private XSSFCellStyle cellStyle;
private String[] header;
private String[] subHeader;
private String[] width;
private String[] sqlKey;
private IConversionByExport conversion;
private List<int[]> mergeRanges = new ArrayList<int[]>();
private Boolean isHeaderMergeTwo = Boolean.FALSE;
private int headerRowIndex = 1;
private int subHeaderRowIndex = 2;
private int titleMergeColumnIndex = 0;
private HashMap<Integer, Integer> modifiedDataHeightHM = new HashMap<Integer, Integer>(); //要修改的数据行的高度
private short subHeaderHeight = 300;
private Boolean isCreateFreezePane = true;
private HashMap<Integer, Integer> skipedDataIndexMap; //要空行的数据列序号:KEY为data的序号 VALUE默认为1
private List<HashMap<Integer, Integer>> resetCellStyleList = new ArrayList<HashMap<Integer,Integer>>(); //要重新设置样式的单元格:KEY为行号,VALUE为列号
private XSSFCellStyle resetCellStyle;
/**
*
*构造函数:得到一个纵向打印在A4纸上的xlsx
*
*/
public ExportXLSX(String[] header){
this.wb = new XSSFWorkbook();
this.sheet = this.createSheet(false, ExportXLSX.A4);
this.headerCellStyle = this.createCellStyle(ExportXLSX.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new XSSFColor(new Color(192, 192, 192)));
this.cellStyle = this.createCellStyle(ExportXLSX.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new XSSFColor(new Color(255, 255, 255)));
this.setTitleName("export");
this.setHeader(header);
}
/**
* 设置打印纸大小,横向还是纵向
* @param header
* @param sqlKey
* @param pageSize
* @param landscapeFlag
*/
public ExportXLSX(String[] header,String[] sqlKey,Short pageSize,boolean landscapeFlag){
XSSFWorkbook x= new XSSFWorkbook();
this.wb = x;
this.sheet = this.createSheet(landscapeFlag, pageSize);
this.headerCellStyle = this.createCellStyle(ExportXLSX.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new XSSFColor(new Color(192, 192, 192)));
XSSFFont titlefont = this.wb.createFont(); //设置字体
titlefont.setFontHeightInPoints(ExportXLSX.HEADERSIZE);
titlefont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
headerCellStyle.setFont(titlefont);
this.cellStyle = this.createCellStyle(ExportXLSX.ALIGN_CENTER, VERTICAL_CENTER, 1111, HEADERSIZE, new XSSFColor(new Color(255, 255, 255)));
this.setTitleName("export");
this.setHeader(header);
this.setSqlKey(sqlKey);
}
private void setHeader(String[] header){
this.header = header;
//默认设置每个单元格的宽度为3500
int length = this.header.length;
this.width = new String[length];
for(int i=0;i<length;i++){
this.width[i] = "3500";
}
}
public void setSqlKey(String[] sqlKey) {
this.sqlKey = sqlKey;
}
/**
* 创建工作簿
* @param landscapeFlag 横向标记 // true横向 false纵向
* @param pageSize 纸张大小
* @return
*/
private XSSFSheet createSheet(boolean landscapeFlag,Short pageSize) {
XSSFPrintSetup ps = null;
XSSFSheet sheet = null;
sheet = this.wb.createSheet();
sheet.setHorizontallyCenter(true);
sheet.setMargin(XSSFSheet.LeftMargin,PAGEMARGIN); //设置页边距
sheet.setMargin(XSSFSheet.RightMargin,PAGEMARGIN);
sheet.setMargin(XSSFSheet.TopMargin,PAGEMARGIN*3);
sheet.setMargin(XSSFSheet.BottomMargin,PAGEMARGIN*3);
ps = sheet.getPrintSetup();
ps.setLandscape(landscapeFlag);
ps.setPaperSize(pageSize);
return sheet;
}
/**
* 创建单元格格式
*/
private XSSFCellStyle createCellStyle(String alignPosition,String verticalPosotion,Integer border,short fontSize,XSSFColor color) {
XSSFCellStyle cellStyle = wb.createCellStyle();
/*
* 设置水平位置
*/
if(alignPosition.equals(ALIGN_LEFT)){
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
}
if(alignPosition.equals(ALIGN_RIGHT)){
cellStyle.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
}
if(alignPosition.equals(ALIGN_CENTER)){
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
}
/*
* 设置垂直位置
*/
if(alignPosition.equals(VERTICAL_TOP)){
cellStyle.setAlignment(XSSFCellStyle.VERTICAL_TOP);
}
if(alignPosition.equals(VERTICAL_CENTER)){
cellStyle.setAlignment(XSSFCellStyle.VERTICAL_CENTER);
}
if(alignPosition.equals(VERTICAL_BOTTOM)){
cellStyle.setAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
}
/*
* 设置边框
*/
if((border/1000)%10==1){
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
}
if((border/100)%10==1){
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
}
if((border/10)%10==1){
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);// 下边框
}
if(border%10==1){
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
}
/*
* 设置字体大小
*/
XSSFFont font = wb.createFont();
font.setFontHeightInPoints(fontSize);// 字体大小
cellStyle.setFont(font);
/*
* 设置背景色
*/
cellStyle.setFillForegroundColor(color);
cellStyle.setFillBackgroundColor(color);
cellStyle.setFillPattern(XSSFCellStyle.SPARSE_DOTS);
/*
* 自动换行
*/
cellStyle.setWrapText(true);
return cellStyle;
}
/**
* 根据不同的类型给cell赋值
*
* @param data 数据
* @param cell
* @throws SQLException
*/
private void obtainPropList(Object data, XSSFCell cell){
if(data == null){
data = "";
}
String type = data.getClass().getName();
if(type.equalsIgnoreCase("java.lang.String")) {
String value = (String)data;
cell.setCellValue(new XSSFRichTextString(value));
}else if(type.equalsIgnoreCase("java.lang.Integer") || type.equalsIgnoreCase("int")) {
int value = (Integer)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Long") || type.equalsIgnoreCase("long")) {
long value = (Long)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Float") || type.equalsIgnoreCase("float")){
float value = (Float)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Double") || type.equalsIgnoreCase("double")){
double value = (Double)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Byte") || type.equalsIgnoreCase("byte")) {
byte value = (Byte)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Character") || type.equalsIgnoreCase("char")) {
char value = (Character)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Boolean") || type.equalsIgnoreCase("boolean")) {
boolean value = (Boolean)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.math.BigDecimal")) {
BigDecimal value = (BigDecimal)data;
cell.setCellValue(value.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
}else if(type.equalsIgnoreCase("java.util.Date")) {
Date value = (Date)data;
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.sql.Timestamp")) {
Timestamp timestamp = (Timestamp)data;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String value = dateFormat.format(timestamp.getTime());
cell.setCellValue(value);
}else if(type.equalsIgnoreCase("java.lang.Short") || type.equalsIgnoreCase("short")) {
short value = (Short)data;
cell.setCellValue(value);
}
}
private void fillSheet(){
int addRowNum = 1;
if(!isHeaderMergeTwo){
addRowNum = 0;
}
if(isCreateFreezePane){
sheet.createFreezePane(0,this.headerRowIndex + addRowNum + 1);
}
//设置行宽
for(int i=0;i<this.width.length;i++){
this.sheet.setColumnWidth(i,Integer.parseInt(this.width[i]));
}
//填入抬头数据
XSSFRow titleRow = this.sheet.createRow(0);
titleRow.setHeight(this.titleHeight);
XSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue(new XSSFRichTextString(this.titleName));
XSSFCellStyle titleCellStyle = this.wb.createCellStyle(); //设置单元格style
titleCellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
titleCellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
XSSFFont titlefont = this.wb.createFont(); //设置字体
titlefont.setFontHeightInPoints(ExportXLSX.TITLESIZE);
titlefont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
titleCellStyle.setFont(titlefont);
titleCellStyle.setWrapText(true);
titleCell.setCellStyle(titleCellStyle);
if(0 == this.titleMergeColumnIndex){
this.titleMergeColumnIndex = this.header.length-1;
}
this.sheet.addMergedRegion(new CellRangeAddress(0,addRowNum,0,this.titleMergeColumnIndex));
//填入标题数据
XSSFRow headerRow = this.sheet.createRow(this.headerRowIndex);
headerRow.setHeight(this.headerHeight);
for(int i=0;i<this.header.length;i++){
XSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
this.obtainPropList(this.header[i], headerCell);
}
Integer dataBeginIndex = this.headerRowIndex + 1;
if(null != subHeader && 0 != subHeader.length){
if(this.subHeader.length > this.header.length){
for (int i = this.header.length; i < this.subHeader.length; i++) {
XSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
headerCell.setCellValue("");
}
}
//填入副标题数据
XSSFRow subHeaderRow = this.sheet.createRow(this.subHeaderRowIndex);
subHeaderRow.setHeight(this.subHeaderHeight);
for(int i=0;i<this.subHeader.length;i++){
XSSFCell headerCell = subHeaderRow.createCell(i);
headerCell.setCellStyle(this.headerCellStyle);
this.obtainPropList(this.subHeader[i], headerCell);
}
if(isCreateFreezePane){
this.sheet.createFreezePane(0,this.subHeaderRowIndex+1);
}
dataBeginIndex++;
}
//填入数据
if(this.data != null){
for(int i=0;i<this.data.size();i++){
Boolean tf = true;
if(null != skipedDataIndexMap){
if(!skipedDataIndexMap.isEmpty() && null != skipedDataIndexMap.get(i)){
tf = false;
}
}
if(tf){
//将数据转为数组类型数据
Object[] array = null;
//如果不存在转换对象,则使用默认的转换方法
if(this.conversion != null){
array = this.conversion.conversion(this.data.get(i), this.sqlKey);
}else{
array = this.conversion(this.data.get(i));
}
XSSFRow dataRow = this.sheet.createRow(dataBeginIndex+i);
dataRow.setHeight(this.cellHeight);
if(null != this.modifiedDataHeightHM){
Integer rowHeight = this.modifiedDataHeightHM.get(i);
if(null != rowHeight){
dataRow.setHeight(Short.valueOf(rowHeight.toString()));
}
}
for(int j=0;j<array.length;j++){
XSSFCell dataCell = dataRow.createCell(j);
dataCell.setCellStyle(this.cellStyle);
this.obtainPropList(array[j],dataCell);
}
}else {
XSSFRow dataRow = this.sheet.createRow(dataBeginIndex+i);
}
}
}
if(null != resetCellStyleList && !resetCellStyleList.isEmpty()){
int len = resetCellStyleList.size();
for (int i = 0; i < len; i++) {
HashMap<Integer, Integer> resetCellStyleHM = resetCellStyleList.get(i);
Iterator<Map.Entry<Integer, Integer>> it = resetCellStyleHM.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
Integer rowIndex = entry.getKey();
Integer columnIndex = entry.getValue();
this.sheet.getRow(rowIndex).getCell(columnIndex).setCellStyle(this.resetCellStyle);
}
}
}
//设置合并开始标识
boolean mergedStartFlag = false;
int[] startAndEnd = null;
List mergedIndex = new ArrayList();
if(null != this.statisticData){
//追加统计行
XSSFRow appendRow = this.sheet.createRow(this.sheet.getLastRowNum()+1);
for(int i=0;i<this.statisticData.length;i++){
//记录合并开始位置
if(!mergedStartFlag){
mergedStartFlag = true;
startAndEnd = new int[2];
startAndEnd[0] = i;
}
//记录合并结束位置
if(ExportXLSX.MERGEDEND.equals(this.statisticData[i]) && mergedStartFlag){
mergedStartFlag = false;
startAndEnd[1] = i;
mergedIndex.add(startAndEnd);
}
//生成单元格,如果是合并单元格,内容置空
XSSFCell dataCell = appendRow.createCell(i);
dataCell.setCellStyle(this.cellStyle);
if(!ExportXLSX.MERGEDEND.equals(this.statisticData[i])){
this.obtainPropList(this.statisticData[i],dataCell);
}
}
}
//根据合并位置记录 合并单元格
for(int i=0;i<mergedIndex.size();i++){
startAndEnd = (int[])mergedIndex.get(i);
this.sheet.addMergedRegion(new CellRangeAddress(this.sheet.getLastRowNum(),this.sheet.getLastRowNum(),startAndEnd[0],startAndEnd[1]));
}
//处理要合并的单元格区域
if(null != this.mergeRanges && 0 != this.mergeRanges.size()){
for (int i = 0; i < this.mergeRanges.size(); i++) {
int[] ranges = this.mergeRanges.get(i);
this.sheet.addMergedRegion(new CellRangeAddress(ranges[0], ranges[1], ranges[2], ranges[3]));
}
}
}
/**
* 默认数据转换方法(只转换数组类型对象)
* @return
*/
private Object[] conversion(Object obj){
try {
return (Object[])obj;
} catch (ClassCastException e) {
if(this.logger.isErrorEnabled()){
this.logger.error("导出工具转换数据对象错误!要转换的数据不是数组型的!");
}
}
return null;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setConversion(IConversionByExport conversion)
*/
public void setConversion(IConversionByExport conversion){
this.conversion = conversion;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setTitleName(java.lang.String)
*/
public void setTitleName(String titleName){
this.titleName = titleName;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifyHeader(java.lang.String, int)
*/
public void modifyHeader(String header, int index){
this.header[index] = header;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifyWidthOfHeader(java.lang.String, int)
*/
public void modifyWidthOfHeader(String width, int index){
this.width[index] = width;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#setData(java.util.List)
*/
public void setData(List<Object> dataList){
this.data = dataList;
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#appendStatisticRow(Object StatisticData)
*/
public void appendStatisticRow(Object statisticData) {
//如果不存在转换对象,则使用默认的转换方法
if(this.conversion != null){
this.statisticData = this.conversion.conversion(statisticData, this.sqlKey);
}else{
this.statisticData = this.conversion(statisticData);
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#returnClientDownload(javax.servlet.http.HttpServletResponse)
*/
public void returnClientDownload(HttpServletResponse response) throws IOException{
//将数据装填到sheet中
this.fillSheet();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + new String(this.titleName.getBytes("gb2312"),"ISO8859-1")+".xlsx");
OutputStream os = null;
try {
os = response.getOutputStream();
this.wb.write(os);
} catch (Exception e) {
} finally {
if(os != null) {
os.close();
os = null;
}
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#returnClientOpen(javax.servlet.http.HttpServletResponse)
*/
public void returnClientOpen(HttpServletResponse response) throws IOException{
//将数据装填到sheet中
this.fillSheet();
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","online; filename=" + new String(this.titleName.getBytes("gb2312"),"ISO8859-1")+".xlsx");
try {
wb.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/* (非 Javadoc
* @see com.saye.common.export.IEport#modifTitleHeight(short height)
*/
public void modifTitleHeight(short height) {
this.titleHeight = height;
}
/**
* 设置副标题
* @param subHeader
*/
public void setSubHeader(String[] subHeader){
this.subHeader = subHeader;
//默认设置每个单元格的宽度为3500
int headerLength = this.header.length;
int subHeaderLength = 0;
if(null != subHeader && 0 != subHeader.length){
subHeaderLength = this.subHeader.length;
}
int length = headerLength > subHeaderLength ? headerLength : subHeaderLength;
this.width = new String[length];
for(int i=0;i<length;i++){
this.width[i] = "3500";
}
}
/**
* 设置要合并的单元格区域
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
*/
public void setMergeRange(int firstRow, int lastRow, int firstCol, int lastCol){
int[] range = new int[4];
range[0] = firstRow;
range[1] = lastRow;
range[2] = firstCol;
range[3] = lastCol;
this.mergeRanges.add(range);
}
/*
*
*/
public void execGenerateExcel(String exportFilePath) throws IOException{
//将数据装填到sheet中
this.fillSheet();
// 设置输入流
FileOutputStream fOut = new FileOutputStream(exportFilePath);
// 将模板的内容写到输出文件上
wb.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
}
/**
* 设置头部高度
* @param height
*/
public void modifyHeaderHeight(short height) {
this.headerHeight = height;
}
/**
* 设置头部标题所处行序号
* @param headerRowIndex
*/
public void modifyHeaderRowIndex(short headerRowIndex) {
this.headerRowIndex = headerRowIndex;
}
/**
* 设置头部副标题序号
* @param subHeaderRowIndex
*/
public void modifySubHeaderRowIndex(short subHeaderRowIndex) {
this.subHeaderRowIndex = subHeaderRowIndex;
}
/**
* 设置标题合并列号
* @param titleMergeColumnIndex
*/
public void setTitleMergeColumnIndex(int titleMergeColumnIndex) {
this.titleMergeColumnIndex = titleMergeColumnIndex;
}
/**
* 设置数据行高
* @param modifiedDataHeightHM数据序号和行高
*/
public void setModifiedDataHeightHM(HashMap<Integer, Integer> modifiedDataHeightHM) {
this.modifiedDataHeightHM = modifiedDataHeightHM;
}
/**
* 设置头部副标题高度
* @param subHeaderHeight
*/
public void modifySubHeaderHeight(short subHeaderHeight) {
this.subHeaderHeight = subHeaderHeight;
}
/**
* 设置是否创建冻结窗体
* @param isCreateFreezePane
*/
public void setIsCreateFreezePane(Boolean isCreateFreezePane) {
this.isCreateFreezePane = isCreateFreezePane;
}
/**
* 设置要空行的数据列序号
* @param skipedDataIndexMap
*/
public void setSkipedDataIndexMap(HashMap<Integer, Integer> skipedDataIndexMap) {
this.skipedDataIndexMap = skipedDataIndexMap;
}
/**
* 设置 重新设置样式的单元格序号及样式:样式为空时,默认为头部标题样式
* @param resetCellStyleList
* @param resetCellStyle
*/
public void resetCellForCellStyle(List<HashMap<Integer, Integer>> resetCellStyleList, XSSFCellStyle resetCellStyle) {
this.resetCellStyleList = resetCellStyleList;
if(null == resetCellStyle){
resetCellStyle = this.headerCellStyle;
}
this.resetCellStyle = resetCellStyle;
}
/**
* 设置 工作薄 密码保护
* @param pwd
*/
public void protectSheet(String pwd) {
this.sheet.protectSheet(pwd);
}
/**
* 设定抬头是否合并两行:在new ExportXLS之后就SET
* @param isHeaderMergeTwo
*/
public void setIsHeaderMergeTwo(Boolean isHeaderMergeTwo) {
this.isHeaderMergeTwo = isHeaderMergeTwo;
if(!this.isHeaderMergeTwo){
this.headerRowIndex = 1;
this.subHeaderRowIndex = 2;
}
}
}

View File

@@ -0,0 +1,27 @@
package com.saye.hgddmz.commons.excel;
import java.util.HashMap;
public class HashMapConversionImpl implements IConversionByExport {
@Override
public Object[] conversion(Object obj) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] conversion(Object obj, String[] sqlArray) {
HashMap<Object, Object> hm = (HashMap<Object, Object>) obj;
int len = sqlArray.length;
Object[] result = new Object[len];
for (int i = 0; i < len; i++) {
result[i] = hm.get(sqlArray[i]);
}
return result;
}
}

View File

@@ -0,0 +1,6 @@
package com.saye.hgddmz.commons.excel;
public interface IConversionByExport {
public Object[] conversion(Object obj);
public Object[] conversion(Object obj, String[] sqlArray);
}

View File

@@ -0,0 +1,53 @@
package com.saye.hgddmz.commons.excel;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
public interface IExport {
/**
* 设置转换对象
* @param conversion
*/
public void setConversion(IConversionByExport conversion);
/**
* 设置数据数据
* @param dataList
* @throws SQLException
*/
public void setData(List<Object> dataList);
/**
* 在表格尾部追加数据,主要用于统计数据
*/
public void appendStatisticRow(Object statisticData);
/**
* 以下载形式导出
* @param response
* @throws IOException
*/
public void returnClientDownload(HttpServletResponse response)
throws IOException;
/**
* 以打开方式导出
* @param response
*/
public void returnClientOpen(HttpServletResponse response)
throws IOException;
/**
* 后台生成EXCEL文件
* @param exportFilePath
* @throws IOException
*/
public void execGenerateExcel(String exportFilePath) throws IOException;
}

View File

@@ -0,0 +1,58 @@
package com.saye.hgddmz.commons.excel;
public interface ISetExport {
/**
* 设置台头
* @param titleName
*/
public void setTitleName(String titleName);
/**
* 设置标题高
* @param height
*/
public void modifTitleHeight(short height);
/**
* 修改某个位置的标题栏名称
* @param header
* @param index
*/
public void modifyHeader(String header, int index);
/**
* 修改某个位置的标题栏宽度
* @param width
* @param index
*/
public void modifyWidthOfHeader(String width, int index);
/**
* 设置副标题
* @param subHeader
*/
public void setSubHeader(String[] subHeader);
/**
* 设置要合并的单元格区域
* @param firstRow 开始行
* @param lastRow 结束行
* @param firstCol 开始列
* @param lastCol 结束列
*/
public void setMergeRange(int firstRow, int lastRow, int firstCol, int lastCol);
/**
* 设置 工作薄 密码保护
* @param pwd
*/
public void protectSheet(String pwd);
/**
* 设定抬头是否合并两行:在new ExportXLS之后就SET
* @param isHeaderMergeTwo
*/
public void setIsHeaderMergeTwo(Boolean isHeaderMergeTwo);
}

View File

@@ -0,0 +1,27 @@
package com.saye.hgddmz.commons.exprot;
import java.util.HashMap;
public class HashMapConversionImpl implements IConversionByExport {
@Override
public Object[] conversion(Object obj) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] conversion(Object obj, String[] sqlArray) {
HashMap<Object, Object> hm = (HashMap<Object, Object>) obj;
int len = sqlArray.length;
Object[] result = new Object[len];
for (int i = 0; i < len; i++) {
result[i] = hm.get(sqlArray[i]);
}
return result;
}
}

View File

@@ -0,0 +1,6 @@
package com.saye.hgddmz.commons.exprot;
public interface IConversionByExport {
public Object[] conversion(Object obj);
public Object[] conversion(Object obj, String[] sqlArray);
}

View File

@@ -0,0 +1,36 @@
package com.saye.hgddmz.commons.file;
import java.io.File;
public class FileDUtil {
/**
* 得到文件的扩展名:大写字母的自动转换为小写字母
* @param f
* @return
*/
public static String getExtension(File f) {
return (f != null) ? getExtension(f.getName()) : "";
}
public static String getExtension(String filename) {
return getExtension(filename,"");
}
public static String getExtension(String filename, String defExt) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i >-1) && (i < (filename.length() - 1))) {
return filename.substring(i + 1).trim().toLowerCase();
}
}
return defExt;
}
public static void main(String[] args) {
File f = new File("d:\test.XLSX");
System.out.println(getExtension(f));
}
}

View File

@@ -0,0 +1,29 @@
package com.saye.hgddmz.commons.getBean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class GetBeanUtil implements ApplicationContextAware {
protected static ApplicationContext applicationContext ;
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static Object getBean(String name) {
//name表示其他要注入的注解name名
return applicationContext.getBean(name);
}
/**
* 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}

View File

@@ -0,0 +1,149 @@
//package com.saye.hgddmz.commons.image;
//
//
//
//import java.io.*;
//import java.net.HttpURLConnection;
//import java.net.URL;
//
//
///**
// * Description: 图片与base64的转换
// * @author dqzhang
// * @created 2018-10-9 下午4:07:55
// */
//public class ImageBase64DUtil {
//
//
//
// /**
// * 本地图片转换成base64字符串
// * @param imgFile 图片本地路径
// * @return
// * @dateTime 2018-02-23 14:40:46
// */
// public static String ImageToBase64ByLocal(String imgFile) throws Exception{// 将图片文件转化为字节数组字符串并对其进行Base64编码处理
// InputStream in = null;
// byte[] data = null;
//
// // 读取图片字节数组
// try {
// in = new FileInputStream(imgFile);
//
// data = new byte[in.available()];
// in.read(data);
// in.close();
// } catch (IOException e) {
//// e.printStackTrace();
// }
// // 对字节数组Base64编码
// BASE64Encoder encoder = new BASE64Encoder();
//
// return encoder.encode(data);// 返回Base64编码过的字节数组字符串
// }
//
//
//
// /**
// * 在线图片转换成base64字符串
// * @param imgURL 图片线上路径
// * @return
// * @dateTime 2018-02-23 14:43:18
// */
// public static String ImageToBase64ByOnline(String imgURL) {
// ByteArrayOutputStream data = new ByteArrayOutputStream();
// try {
// // 创建URL
// URL url = new URL(imgURL);
// byte[] by = new byte[1024];
// // 创建链接
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("GET");
// conn.setConnectTimeout(5000);
// InputStream is = conn.getInputStream();
// // 将内容读取内存中
// int len = -1;
// while ((len = is.read(by)) != -1) {
// data.write(by, 0, len);
// }
// // 关闭流
// is.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// // 对字节数组Base64编码
// BASE64Encoder encoder = new BASE64Encoder();
// return encoder.encode(data.toByteArray());
// }
//
//
// /**
// * base64字符串转换成图片
// * @param imgStr base64字符串
// * @param imgFilePath 图片存放路径
// * @return
// * @dateTime 2018-02-23 14:42:17
// */
// public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
//
//
//
// BASE64Decoder decoder = new BASE64Decoder();
// try {
// // Base64解码
// byte[] b = decoder.decodeBuffer(imgStr);
// for (int i = 0; i < b.length; ++i) {
// if (b[i] < 0) {// 调整异常数据
// b[i] += 256;
// }
// }
// OutputStream out = new FileOutputStream(imgFilePath);
// out.write(b);
// out.flush();
// out.close();
//
// return true;
// } catch (Exception e) {
// return false;
// }
//
// }
//
// /**
// * @description 下载在线图片到本地
// */
// public static void readInputStream(URL url,File file) throws Exception {
// // 打开链接
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// // 设置请求方式为"GET"
// conn.setRequestMethod("GET");
// // 超时响应时间为5秒
// conn.setConnectTimeout(5 * 1000);
// // 通过输入流获取图片数据
// InputStream inStream = conn.getInputStream();
// ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// // 创建一个Buffer字符串
// byte[] buffer = new byte[1024];
// // 每次读取的字符串长度,如果为-1代表全部读取完毕
// int len = 0;
// // 使用一个输入流从buffer里把数据读取出来
// while ((len = inStream.read(buffer)) != -1) {
// // 用输出流往buffer里写入数据中间参数代表从哪个位置开始读len代表读取的长度
// outStream.write(buffer, 0, len);
// }
// // 关闭输入流
// inStream.close();
// // 创建输出流
// FileOutputStream outStream2 = new FileOutputStream(file);
// // 写入数据
// outStream2.write(outStream.toByteArray());
// // 关闭输出流
// outStream2.close();
// }
//
// public static void main(String[] args) throws Exception{
// }
//
//
//
//}

View File

@@ -0,0 +1,27 @@
package com.saye.hgddmz.commons.log;
public class ExceptionDUtil {
/**
* 返回异常的详细信息
* @param e
* @return
*/
public static String getDetailExceptionMsg(Exception e){
StringBuffer exceptionMessage = new StringBuffer();
StackTraceElement[] stackTraceElementes = e.getStackTrace();
int length = stackTraceElementes.length;
StackTraceElement ste;
//只要最顶上的错误栈
for(int i=0;i<length;i++){
ste = stackTraceElementes[i];
exceptionMessage.append(ste.getClassName()+":"+ste.getLineNumber()+"\r\n");
//break;
}
String result = e.toString()+"\r\n"+exceptionMessage.toString();
return result;
}
}

View File

@@ -0,0 +1,415 @@
package com.saye.hgddmz.commons.string;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;
public class StringDUtil {
/**
* 去掉给定字符串前和后的空格,返回干净的字符串
* @param str
* @return String
*/
public static String removeSpaces(Object args) {
String argsStr = changeNullToEmpty(args);
if(args!=null){
args = argsStr.trim();
while (argsStr.startsWith(" ")) {
argsStr = argsStr.substring(1, argsStr.length()).trim();
}
while (argsStr.endsWith(" ")) {
argsStr = argsStr.substring(0, argsStr.length() - 1).trim();
}
}else{
argsStr = "";
}
return argsStr;
}
/**
* 转全角的函数
* @param str
* @return String
*/
public static String toSBC(String input) {
//半角转全角:
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 32) {
c[i] = (char) 12288;
continue;
}
if (c[i] < 127) {
c[i] = (char) (c[i] + 65248);
}
}
return new String(c);
}
/**
* 转半角的函数
* @param str
* @return String
*/
public static String toDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375){
c[i] = (char) (c[i] - 65248);
}
}
return new String(c);
}
/**
* 为每添加一个元素前面增加指定的分隔 除第一个元素之外
* @param str
* @param appStr
* @param compart
* @return
*/
public static StringBuffer appendElement(StringBuffer strB,String appStr,String compart){
//当出入参数为NULL时
if(strB == null){
return new StringBuffer(appStr);
}
//当没有元素时直接添加追加元素 否则先添加分隔符
if(strB.length() == 0){
strB.append(appStr);
}else{
strB.append(compart);
strB.append(appStr);
}
return strB;
}
/**
* 移除元素
* @param str
* @param moveStr
* @param compart
* @return
*/
public static StringBuffer moveElement(StringBuffer strB,String moveStr,String compart){
//当出入参数为NULL时
if(strB == null){
return strB;
}
StringBuffer newStrB = new StringBuffer();
String[] strArray = strB.toString().split(compart);
for(int i=0;i<strArray.length;i++){
if(moveStr.equals(strArray[i])){
continue;
}
if(i == 0){
newStrB.append(strArray[i]);
}else{
newStrB.append(compart);
newStrB.append(strArray[i]);
}
}
return newStrB;
}
/**
* 移除第一个匹配的元素
* @param str
* @param moveStr
* @param compart
* @return
*/
public static StringBuffer moveFirstElement(StringBuffer strB,String moveStr,String compart){
//当出入参数为NULL时
if(strB == null){
return strB;
}
StringBuffer newStrB = new StringBuffer();
String[] strArray = strB.toString().split(compart);
boolean tag = false;
for(int i=0;i<strArray.length;i++){
if(moveStr.equals(strArray[i]) == true && tag == false){
tag = true;
continue;
}
if(i == 0){
newStrB.append(strArray[i]);
}else{
newStrB.append(compart);
newStrB.append(strArray[i]);
}
}
return newStrB;
}
/**
* 从给定字符中 返回所含的中文字符 并按每组以以字符串数组的形式返回
* @param src
* @return
*/
public static String[] getChinese(String src){
List list = new ArrayList();
byte[] srcByte = src.getBytes();
int srcLength = srcByte.length;
int begin = -1;
int end = -1;
for(int i=0;i<srcLength;i++){
//设置中文的开始位
if(srcByte[i] < 0 && begin == -1){
begin = i;
}
//设置中文的结束位
if(srcByte[i] > 0 && begin != -1 && end == -1){
end = i;
}
//如果已经找到中文的开始 但直到最后也没找到中文的结束,则将字符的结束位当成中文的截止位
if(begin != -1 && i == srcLength - 1){
end = i;
}
//将中文提取出来
if(begin != -1 && end != -1){
int tempLength = end-begin+1;
if(tempLength % 2 != 0){
tempLength = tempLength - 1;
}
byte[] tempByte = new byte[tempLength];
System.arraycopy(srcByte, begin, tempByte, 0, tempLength);
list.add(new String(tempByte));
begin = -1;
end = -1;
}
}
//将中文以数组输出
int size = list.size();
String[] chineseArray = new String[size];
for(int i=0;i<size;i++){
chineseArray[i] = list.get(i).toString();
}
return chineseArray;
}
/**
* 产生指定长度的随机码(由字母和数字组成)
* @param len
* @return
*/
public static String generateRandomCodeForLength(int len){
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sRand = new StringBuffer();
Random random=new Random();
for (int i = 0; i < len; i++) {
String ch = String.valueOf(s.charAt(random.nextInt(s.length())));
sRand.append(ch);
}
return sRand.toString();
}
/**
* 将字符串NULL转换为空:""
* @param args
* @return
*/
public static String changeNullToEmpty(Object args) {
String result = null == args ? "" : String.valueOf(args);
if("\"null\"".equals(result) || "null".equals(result)){
result = "";
}
return result;
}
/**
* 将字符串NULL转换为空:"",并且去字符串中的转义字符
* @param args
* @return
*/
public static String changeNullToEmptyReplaceEscape(Object args) {
String result = null == args ? "" : String.valueOf(args);
result = result.replaceAll("'", "");
result = result.replaceAll("", "{");
result = result.replaceAll("", "}");
return result;
}
/**
* 验证字符串是否为空NULL或""
* @param str
* @return 返回值为true说明是为空。
*/
public static Boolean isEmpty(Object args) {
String result = "";
String str = changeNullToEmpty(args);
if("".equals(removeSpaces(str)) || "\"null\"".equals(removeSpaces(str)) || "null".equals(removeSpaces(str))){
return true;
}
return false;
}
/**
* 验证MAP中要验证的字段是否为空NULL或""
* @param hm 要验证的MAP
* @param fieldString 传入方式(以,将字段进行分割)personid,sysid
* @return 返回值为空,则验证成功,说明都不为空;否则返回为空的该字段名。
*/
public static String isExistEmpty(Map<Object, Object> hm, String fieldString) {
String[] strArr = fieldString.split(",");
for (int i = 0; i < strArr.length; i++) {
String key = strArr[i];
Object obj = hm.get(key);
if(isEmpty(obj)){
return key;
}
}
return "";
}
/**
* 判断字符串是否为正整数
* @param str
* @return
*/
public static boolean isPositiveInteger (String str){
Pattern pattern = Pattern.compile("^[0-9]*[1-9][0-9]*$");
return pattern.matcher(str).matches();
}
/**
* 判断字符串是否为正整数或正浮点数
* @param str
* @return
*/
public static boolean isPositiveIntegerOrPositiveFloat (String str){
Pattern pattern = Pattern.compile("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
return pattern.matcher(str).matches();
}
/**
*
* @description 截取字符串
* @author mczheng
* @created 2016-6-25 下午6:13:29
* @param str 要截取的字符串
* @param maxLength 字符长度英文字符算0.5个长度当截取剩下最后一位是0.5个长度而最后一个字符是中文时,则舍弃掉。
* @return
*/
public static String substring(String str, int maxLength) {
if (!hasLength(str))
return str;
int subSLength = maxLength * 2;
int tempSubLength = subSLength;//截取字符数
String subStr = "";
try {
int strLen = str.getBytes("GBK").length;
if (strLen <= subSLength) return str;
subStr = str.substring(0, str.length() < subSLength ? str.length() : subSLength); //截取的子串
int subStrByetsL = subStr.getBytes("GBK").length; //截取子串的字节长度
//说明截取的字符串中包含有汉字
while (subStrByetsL > tempSubLength) {
int subSLengthTemp = --subSLength;
subStr = str.substring(0, subSLengthTemp > str.length() ? str.length() : subSLengthTemp);
subStrByetsL = subStr.getBytes("GBK").length;
}
subStr += "...";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return subStr;
}
public static boolean hasLength(String str) {
return (str != null) && (str.length() > 0);
}
public static void main(String[] args) {
//
// StringBuffer str = new StringBuffer("a,c,d,c");
// str = StringDUtil.moveFirstElement(str, "c", ",");
// System.out.println(str.toString());
//
// System.out.println(isEmpty("\"null\""));
System.out.println(StringDUtil.substring("一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十", 15));
}
/**
* @description 判断字符串是否有值且值不为空格
* @author thuang
* @created 2020年1月7日 下午1:09:49
* @param str
* @return
*/
public static boolean isNotBlank(String str) {
if (str != null && str.length() > 0 && str.trim().length() > 0) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,24 @@
package com.saye.hgddmz.commons.uuid;
import java.util.UUID;
public class UUIDGenerator {
public UUIDGenerator() {
}
/**
* 获得一个UUID
* @return String UUID
*/
public static String getUUID(){
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
}
public static void main(String[] args){
String ss = UUIDGenerator.getUUID();
System.out.println(ss);
System.out.println(ss.length());
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,143 @@
package com.saye.hgddmz.entity;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* (BankbillHistory)实体类
*
* @author makejava
* @since 2021-09-09 16:03:37
*/
@Getter
@Setter
public class BankbillHistory implements Serializable {
private static final long serialVersionUID = -33254602055133483L;
/**
* 清算日期
*/
private String cQsrq;
/**
* 交易日期
*/
private String cJyrq;
/**
* 交易时间
*/
private String cJysj;
/**
* 终端号
*/
private String cZdh;
/**
* 卡号(部分加密 应该没用
*/
private String cCard;
/**
* 交易类型
*/
private String cJylx;
/**
* 交易金额
*/
private String cJyje;
/**
* 清算金额
*/
private String cQsje;
/**
* 手续费
*/
private String cSxf;
/**
* 实际支付金额
*/
private String cSjzfje;
/**
* 参考号
*/
private String cCkh;
/**
* 流水号
*/
private String cLsh;
/**
* 卡类型
*/
private String cKlx;
/**
* 发卡行
*/
private String cFkh;
/**
* 支付方式
*/
private String cZffs;
/**
* 银商订单号
*/
private String cYsddh;
/**
* 商户订单号
*/
private String cShddh;
/**
* 备注字段
*/
private String cBzzd;
/**
* 钱包优惠金额
*/
private String cQbyhje;
/**
* 商户优惠金额
*/
private String cShyhje;
/**
* 原交易流水号
*/
private String cYjylsh;
/**
* 分期期数
*/
private String cFqqs;
/**
* 分期手续费
*/
private String cFqsxf;
/**
* 分期服务方
*/
private String cFqfwf;
/**
* 分期付息方
*/
private String cFqfxf;
/**
* 其他优惠金额
*/
private String cQtyhje;
/**
* 退货订单号
*/
private String cThddh;
/**
* 付款附言
*/
private String cFkfy;
/**
* 分店简称
*/
private String cFdjc;
/**
* 子订单号
*/
private String cZddh;
/**
* 对账表名
*/
private String billTableName;
}

View File

@@ -0,0 +1,25 @@
package com.saye.hgddmz;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
/**
* @author thuang
* @version 1.0
* @description: TODO
* @date 2021/9/22 13:12
*/
@Controller
public class testController {
@RequestMapping("/index")
public String toIndex(){
return "index";
}
}

View File

@@ -0,0 +1,301 @@
package com.saye.hgddmz.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.net.SocketException;
/**
* @author thuang
* @version 1.0
* @description:
* 下载ftp文件
* @date 2021/8/6 14:17
*/
@Slf4j
public class DownloadFtpUtil {
/**
* 获取FTPClient对象
*
* @param ftpHost
* FTP主机服务器
* @param ftpPassword
* FTP 登录密码
* @param ftpUserName
* FTP登录用户名
* @param ftpPort
* FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
// 设置更长的连接超时时间
ftpClient.setConnectTimeout(60000); // 60秒连接超时
ftpClient.setDataTimeout(120000); // 120秒数据传输超时
ftpClient.setControlKeepAliveTimeout(300); // 保持连接活跃
ftpClient.setControlKeepAliveReplyTimeout(10000); // 保持连接回复超时
log.info("=== FTP连接详细信息 ===");
log.info("FTP服务器地址: {}", ftpHost);
log.info("FTP服务器端口: {}", ftpPort);
log.info("FTP用户名: {}", ftpUserName);
log.info("连接超时时间: {}ms", 60000);
log.info("数据传输超时时间: {}ms", 120000);
log.info("开始连接FTP服务器: {}:{}", ftpHost, ftpPort);
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
// 检查连接是否成功
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.error("FTP连接失败服务器响应码: {}", ftpClient.getReplyCode());
log.error("FTP服务器响应信息: {}", ftpClient.getReplyString());
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
return null;
}
log.info("FTP服务器连接成功响应码: {}", ftpClient.getReplyCode());
log.info("FTP服务器响应信息: {}", ftpClient.getReplyString());
log.info("正在登录FTP服务器用户名: {}", ftpUserName);
boolean loginSuccess = ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!loginSuccess) {
log.error("FTP登录失败请检查用户名和密码");
log.error("登录失败响应码: {}", ftpClient.getReplyCode());
log.error("登录失败响应信息: {}", ftpClient.getReplyString());
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
return null;
}
log.info("FTP登录成功响应码: {}", ftpClient.getReplyCode());
log.info("FTP登录响应信息: {}", ftpClient.getReplyString());
// 设置FTP传输模式
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 二进制传输模式
ftpClient.enterLocalPassiveMode(); // 被动模式
ftpClient.setBufferSize(1024 * 1024); // 设置缓冲区大小
log.info("=== FTP连接和登录成功 ===");
return ftpClient;
} catch (SocketException e) {
log.error("FTP连接Socket异常: {}", e.getMessage());
log.error("异常堆栈信息:", e);
// 发生异常时断开连接
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ie) {
log.error("断开FTP连接失败: {}", ie.getMessage());
}
}
} catch (IOException e) {
log.error("FTP连接IO异常: {}", e.getMessage());
log.error("异常堆栈信息:", e);
// 发生异常时断开连接
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ie) {
log.error("断开FTP连接失败: {}", ie.getMessage());
}
}
} catch (Exception e) {
log.error("FTP连接未知异常: {}", e.getMessage());
log.error("异常堆栈信息:", e);
// 发生异常时断开连接
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ie) {
log.error("断开FTP连接失败: {}", ie.getMessage());
}
}
}
return null;
}
/*
* 从FTP服务器下载文件
*
* @param ftpHost FTP IP地址
*
* @param ftpUserName FTP 用户名
*
* @param ftpPassword FTP用户名密码
*
* @param ftpPort FTP端口
*
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
*
* @param localPath 下载到本地的位置 格式H:/download
*
* @param fileName 文件名称
*/
public static boolean downloadFtpFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
int maxRetries = 3; // 最大重试次数
for (int retryCount = 0; retryCount < maxRetries; retryCount++) {
FTPClient ftpClient = null;
OutputStream os = null;
boolean shouldRetry = false;
try {
log.info("尝试第 {} 次下载文件: {}", retryCount + 1, fileName);
//先判断下载的路径是否存在 不存在创建
File file = new File(localPath);
if (!file.exists()) {
file.mkdirs();
}
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
// 检查连接是否成功
if (ftpClient == null) {
log.error("FTP客户端为null连接失败");
shouldRetry = true;
} else if (!ftpClient.isConnected()) {
log.error("FTP客户端未连接连接状态: {}", ftpClient.isConnected());
shouldRetry = true;
} else {
log.info("FTP连接状态检查通过开始下载文件");
log.info("FTP连接状态: {}", ftpClient.isConnected());
// 切换到指定目录
if (ftpPath != null && !ftpPath.trim().isEmpty()) {
if (!ftpClient.isConnected()) {
log.error("FTP连接已断开无法切换目录");
shouldRetry = true;
} else {
boolean changed = ftpClient.changeWorkingDirectory(ftpPath);
if (!changed) {
log.warn("切换FTP目录失败: " + ftpPath);
}
}
}
if (!shouldRetry) {
// 跳过文件存在性检查,直接尝试下载
log.info("跳过文件存在性检查,直接尝试下载文件: {}", fileName);
// 下载文件
File localFile = new File(localPath + File.separatorChar + fileName);
log.info("准备下载文件到本地路径: {}", localFile.getAbsolutePath());
os = new FileOutputStream(localFile);
if (!ftpClient.isConnected()) {
log.error("FTP连接已断开无法下载文件");
shouldRetry = true;
} else {
log.info("开始下载文件: {}", fileName);
boolean downloadSuccess = ftpClient.retrieveFile(fileName, os);
// 立即关闭输出流
if (os != null) {
try {
os.close();
os = null;
} catch (IOException e) {
log.error("关闭输出流失败: {}", e.getMessage());
}
}
if (downloadSuccess) {
log.info("文件下载成功: {}", fileName);
// 检查下载的文件大小
if (localFile.exists()) {
long fileSize = localFile.length();
log.info("下载的文件大小: {} 字节", fileSize);
if (fileSize == 0) {
log.warn("警告下载的文件大小为0字节可能下载失败");
shouldRetry = true;
} else {
return true; // 下载成功
}
} else {
log.error("下载的文件不存在: {}", localFile.getAbsolutePath());
shouldRetry = true;
}
} else {
log.error("文件下载失败: {}", fileName);
// 获取FTP服务器的回复码
int replyCode = ftpClient.getReplyCode();
log.error("FTP服务器回复码: {}", replyCode);
shouldRetry = true;
}
}
}
}
} catch (FileNotFoundException e) {
log.error("没有找到文件: " + ftpPath + "/" + fileName + ", 错误: " + e.getMessage());
e.printStackTrace();
shouldRetry = true;
} catch (SocketException e) {
log.error("连接FTP失败: " + e.getMessage());
e.printStackTrace();
shouldRetry = true;
} catch (IOException e) {
log.error("文件读取错误: " + e.getMessage());
e.printStackTrace();
shouldRetry = true;
} catch (Exception e) {
log.error("FTP下载过程中发生未知错误: " + e.getMessage());
e.printStackTrace();
shouldRetry = true;
} finally {
// 确保资源正确关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
log.error("关闭输出流失败: " + e.getMessage());
}
}
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
log.error("关闭FTP连接失败: " + e.getMessage());
}
}
}
// 如果需要重试且还有重试次数
if (shouldRetry && retryCount < maxRetries - 1) {
log.info("等待2秒后重试...");
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
}
}
}
log.error("所有重试都失败了");
return false;
}
public static void main(String[] args) {
boolean b = downloadFtpFile("uisftp.chinaums.com", "101200-898650180620093", "Aa112233.", 21, "build", "d:\\1", "898650180620093_20250709_68.xls");
}
}

View File

@@ -0,0 +1,11 @@
server:
port: 8081
servlet:
context-path: /
#日志
logging:
level:
com.saye: debug
pattern:
console: "%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<div>1</div>
</body>
</html>

View File

@@ -0,0 +1,312 @@
package com.saye.hgddmz;
import com.saye.hgddmz.util.DownloadFtpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* FTP Connection Test Class
* @author thuang
* @version 1.0
* @description: Test FTP connection functionality
* @date 2024/1/1
*/
@Slf4j
public class FtpConnectionTest {
private static final String FTP_HOST = "uisftp.chinaums.com";
private static final int FTP_PORT = 21;
private static final String FTP_USERNAME = "101200-898650180620093";
private static final String FTP_PASSWORD = "Aa112233.";
/**
* Test network connectivity
*/
@Test
public void testNetworkConnectivity() {
log.info("=== Start testing network connectivity ===");
// Test DNS resolution
try {
log.info("Testing DNS resolution: {}", FTP_HOST);
InetAddress address = InetAddress.getByName(FTP_HOST);
log.info("DNS resolution successful, IP address: {}", address.getHostAddress());
log.info("Hostname: {}", address.getHostName());
log.info("Is reachable: {}", address.isReachable(5000));
} catch (UnknownHostException e) {
log.error("DNS resolution failed: {}", e.getMessage());
} catch (IOException e) {
log.error("Network reachability test failed: {}", e.getMessage());
}
// Test port connectivity
try {
log.info("Testing port connectivity: {}:{}", FTP_HOST, FTP_PORT);
Socket socket = new Socket(FTP_HOST, FTP_PORT);
log.info("Port connectivity test successful, local port: {}", socket.getLocalPort());
socket.close();
} catch (IOException e) {
log.error("Port connectivity test failed: {}", e.getMessage());
}
log.info("=== Network connectivity test completed ===");
}
/**
* Test FTP connection
*/
@Test
public void testFtpConnection() {
log.info("=== Start testing FTP connection ===");
FTPClient ftpClient = null;
try {
ftpClient = DownloadFtpUtil.getFTPClient(FTP_HOST, FTP_USERNAME, FTP_PASSWORD, FTP_PORT);
if (ftpClient != null && ftpClient.isConnected()) {
log.info("FTP connection test successful");
log.info("FTP server information:");
log.info(" - Server address: {}", FTP_HOST);
log.info(" - Server port: {}", FTP_PORT);
log.info(" - Connection status: {}", ftpClient.isConnected());
log.info(" - Current directory: {}", ftpClient.printWorkingDirectory());
// Test listing files in root directory
try {
String[] files = ftpClient.listNames();
if (files != null) {
log.info("Root directory file list ({} files):", files.length);
for (String file : files) {
log.info(" - {}", file);
}
} else {
log.info("Root directory is empty");
}
} catch (IOException e) {
log.warn("Failed to list files in root directory: {}", e.getMessage());
}
// Test listing files in build directory
try {
log.info("Switching to build directory...");
boolean changed = ftpClient.changeWorkingDirectory("build");
if (changed) {
log.info("Successfully switched to build directory");
String[] files = ftpClient.listNames();
if (files != null) {
log.info("Build directory file list ({} files):", files.length);
for (String file : files) {
log.info(" - {}", file);
}
} else {
log.info("Build directory is empty");
}
} else {
log.error("Failed to switch to build directory");
}
} catch (IOException e) {
log.warn("Failed to list files in build directory: {}", e.getMessage());
}
} else {
log.error("FTP connection test failed");
}
} catch (Exception e) {
log.error("FTP connection test exception: {}", e.getMessage());
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
log.info("FTP connection closed");
} catch (IOException e) {
log.error("Failed to close FTP connection: {}", e.getMessage());
}
}
}
log.info("=== FTP connection test completed ===");
}
/**
* Test FTP file download
*/
@Test
public void testFtpFileDownload() {
log.info("=== Start testing FTP file download ===");
String ftpPath = "build";
String localPath = "d:\\1";
String fileName = "898650180620093_20250723_1.xls";
log.info("Download parameters:");
log.info(" - FTP path: {}", ftpPath);
log.info(" - Local path: {}", localPath);
log.info(" - File name: {}", fileName);
log.info(" - Full local path: {}\\{}", localPath, fileName);
try {
boolean result = DownloadFtpUtil.downloadFtpFile(
FTP_HOST, FTP_USERNAME, FTP_PASSWORD, FTP_PORT,
ftpPath, localPath, fileName
);
if (result) {
log.info("FTP file download test successful");
log.info("Download file information:");
log.info(" - Server path: {}/{}", ftpPath, fileName);
log.info(" - Local path: {}/{}", localPath, fileName);
// Check if file actually exists
java.io.File downloadedFile = new java.io.File(localPath + "\\" + fileName);
if (downloadedFile.exists()) {
log.info("File exists on local system");
log.info(" - File size: {} bytes", downloadedFile.length());
log.info(" - Last modified: {}", new java.util.Date(downloadedFile.lastModified()));
} else {
log.error("File does not exist on local system");
}
} else {
log.error("FTP file download test failed");
}
} catch (Exception e) {
log.error("FTP file download test exception: {}", e.getMessage());
e.printStackTrace();
}
log.info("=== FTP file download test completed ===");
}
/**
* Test FTP connection parameters
*/
@Test
public void testFtpConnectionParameters() {
log.info("=== Start testing FTP connection parameters ===");
log.info("FTP connection parameters:");
log.info(" - Host address: {}", FTP_HOST);
log.info(" - Port: {}", FTP_PORT);
log.info(" - Username: {}", FTP_USERNAME);
log.info(" - Password: {}", FTP_PASSWORD.replaceAll(".", "*"));
// Test different timeout settings
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
// Set shorter timeout for testing
ftpClient.setConnectTimeout(10000); // 10 seconds
ftpClient.setDataTimeout(30000); // 30 seconds
log.info("Connection timeout setting: {}ms", 10000);
log.info("Data transfer timeout setting: {}ms", 30000);
ftpClient.connect(FTP_HOST, FTP_PORT);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
log.info("Connection successful, response code: {}", ftpClient.getReplyCode());
boolean loginSuccess = ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
if (loginSuccess) {
log.info("Login successful");
} else {
log.error("Login failed, response code: {}", ftpClient.getReplyCode());
}
} else {
log.error("Connection failed, response code: {}", ftpClient.getReplyCode());
}
} catch (Exception e) {
log.error("FTP connection parameters test exception: {}", e.getMessage());
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
log.error("Failed to close FTP connection: {}", e.getMessage());
}
}
}
log.info("=== FTP connection parameters test completed ===");
}
/**
* Simple FTP connection test
*/
@Test
public void testSimpleFtpConnection() {
log.info("=== Start simple FTP connection test ===");
FTPClient ftpClient = null;
try {
ftpClient = DownloadFtpUtil.getFTPClient(FTP_HOST, FTP_USERNAME, FTP_PASSWORD, FTP_PORT);
if (ftpClient != null) {
log.info("FTP client created successfully");
log.info("Connection status: {}", ftpClient.isConnected());
if (ftpClient.isConnected()) {
log.info("FTP connection is active");
// Test a simple command
String currentDir = ftpClient.printWorkingDirectory();
log.info("Current directory: {}", currentDir);
} else {
log.error("FTP connection is not active");
}
} else {
log.error("FTP client is null");
}
} catch (Exception e) {
log.error("Simple FTP connection test exception: {}", e.getMessage());
e.printStackTrace();
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
log.info("FTP connection closed");
} catch (IOException e) {
log.error("Failed to close FTP connection: {}", e.getMessage());
}
}
}
log.info("=== Simple FTP connection test completed ===");
}
/**
* Main method, can run tests directly
*/
public static void main(String[] args) {
FtpConnectionTest test = new FtpConnectionTest();
log.info("Starting FTP connection test...");
// Test network connectivity
test.testNetworkConnectivity();
// Test FTP connection
test.testFtpConnection();
// Test FTP connection parameters
test.testFtpConnectionParameters();
// Test file download (optional)
// test.testFtpFileDownload();
log.info("FTP connection test completed");
}
}

View File

@@ -0,0 +1,13 @@
package com.saye.hgddmz;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HgdDmzApplicationTests {
@Test
void contextLoads() {
}
}