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

Commit db3cfb32 authored by morningbear's avatar morningbear

图片接口

parents
Pipeline #177 failed with stages
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
### VS Code ###
.vscode/
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
#!/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 "$@"
@REM ----------------------------------------------------------------------------
@REM Licensed to the Apache Software Foundation (ASF) under one
@REM or more contributor license agreements. See the NOTICE file
@REM distributed with this work for additional information
@REM regarding copyright ownership. The ASF licenses this file
@REM to you under the Apache License, Version 2.0 (the
@REM "License"); you may not use this file except in compliance
@REM with the License. You may obtain a copy of the License at
@REM
@REM https://www.apache.org/licenses/LICENSE-2.0
@REM
@REM Unless required by applicable law or agreed to in writing,
@REM software distributed under the License is distributed on an
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@REM KIND, either express or implied. See the License for the
@REM specific language governing permissions and limitations
@REM under the License.
@REM ----------------------------------------------------------------------------
@REM ----------------------------------------------------------------------------
@REM Maven Start Up Batch script
@REM
@REM Required ENV vars:
@REM JAVA_HOME - location of a JDK home dir
@REM
@REM Optional ENV vars
@REM M2_HOME - location of maven2's installed home dir
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
@REM e.g. to debug Maven itself, use
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
@REM ----------------------------------------------------------------------------
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
@echo off
@REM set title of command window
title %0
@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
@REM set %HOME% to equivalent of $HOME
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
:skipRcPre
@setlocal
set ERROR_CODE=0
@REM To isolate internal variables from possible post scripts, we use another setlocal
@setlocal
@REM ==== START VALIDATION ====
if not "%JAVA_HOME%" == "" goto OkJHome
echo.
echo Error: JAVA_HOME not found in your environment. >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
:OkJHome
if exist "%JAVA_HOME%\bin\java.exe" goto init
echo.
echo Error: JAVA_HOME is set to an invalid directory. >&2
echo JAVA_HOME = "%JAVA_HOME%" >&2
echo Please set the JAVA_HOME variable in your environment to match the >&2
echo location of your Java installation. >&2
echo.
goto error
@REM ==== END VALIDATION ====
:init
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set EXEC_DIR=%CD%
set WDIR=%EXEC_DIR%
:findBaseDir
IF EXIST "%WDIR%"\.mvn goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set WDIR=%CD%
goto findBaseDir
:baseDirFound
set MAVEN_PROJECTBASEDIR=%WDIR%
cd "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
cd "%EXEC_DIR%"
:endDetectBaseDir
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
@setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
:endReadAdditionalConfig
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
)
@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
if exist %WRAPPER_JAR% (
if "%MVNW_VERBOSE%" == "true" (
echo Found %WRAPPER_JAR%
)
) else (
if not "%MVNW_REPOURL%" == "" (
SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar"
)
if "%MVNW_VERBOSE%" == "true" (
echo Couldn't find %WRAPPER_JAR%, downloading it ...
echo Downloading from: %DOWNLOAD_URL%
)
powershell -Command "&{"^
"$webclient = new-object System.Net.WebClient;"^
"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
"}"^
"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
"}"
if "%MVNW_VERBOSE%" == "true" (
echo Finished downloading %WRAPPER_JAR%
)
)
@REM End of extension
@REM Provide a "standardized" way to retrieve the CLI args that will
@REM work with both Windows and non-Windows executions.
set MAVEN_CMD_LINE_ARGS=%*
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
if ERRORLEVEL 1 goto error
goto end
:error
set ERROR_CODE=1
:end
@endlocal & set ERROR_CODE=%ERROR_CODE%
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
@REM check for post script, once with legacy .bat ending and once with .cmd ending
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
:skipRcPost
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
if "%MAVEN_BATCH_PAUSE%" == "on" pause
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
exit /B %ERROR_CODE%
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_image</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_image</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加JPA支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加数据库连接支持 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<!-- 添加swagger依赖,用于接口测试和文档生成 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.demo_image;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoImageApplication {
public static void main(String[] args) {
SpringApplication.run(DemoImageApplication.class, args);
}
}
package com.example.demo_image.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author morningbear
* @date 20.7.7
* 测试用
*/
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
/**上传地址*/
@Value("${file.upload.path}")
private String filePath;
/**显示相对地址*/
@Value("${file.upload.path.relative}")
private String fileRelativePath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(fileRelativePath).
addResourceLocations("file:/" + filePath);
}
}
package com.example.demo_image.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author morningbear
* @date 20.7.14
*/
@EnableSwagger2
@SpringBootConfiguration
public class SwaggerUiConfig {
private Boolean enableSwaggerUi = Boolean.FALSE;
private Environment environment;
@Autowired
public void setEnvironment(Environment environment){
this.environment = environment;
Profiles profiles = Profiles.of("dev", "test");
enableSwaggerUi = environment.acceptsProfiles(profiles);
}
@Bean("clientAPI")
public Docket clientApi(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("客户端")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo_image.controller.api"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
@Bean("adminAPI")
public Docket adminApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("管理后台")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller.admin"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
/**
* 功能描述:SwaggerUI文档的基本信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("骨架")
.description("骨架 。。。施工中")
.termsOfServiceUrl("http://127.0.0.1")
.contact(new Contact("", "", ""))
.version("v0.0.1")
.build();
}
}
package com.example.demo_image.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author morningbear
* @date 20.7.13
*/
@Slf4j
@RestController
public class BaseController {
private static String getRequestRemoteAddr() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
return request.getRemoteAddr();
}
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}
}
\ No newline at end of file
package com.example.demo_image.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* @author morningbear
* @date 20.7.7
*
*/
//@Controller
public class TestController {
/**上传地址*/
/**
@Value("${file.upload.path}")
private String filePath;
/**跳转上传页面*/
/**@GetMapping("/")
public String test() {
return "Page";
}
/**执行上传*/
/**
@PostMapping("upload")
public String upload(@RequestParam("file") MultipartFile file, Model model) {
// 获取上传文件名
String filename = file.getOriginalFilename();
// 定义上传文件保存路径
String path = filePath+"rotPhoto/";
// 新建文件
File filepath = new File(path, filename);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
try {
// 写入文件
file.transferTo(new File(path + File.separator + filename));
} catch (IOException e) {
e.printStackTrace();
}
// 将src路径发送至html页面
model.addAttribute("filename", "/images/rotPhoto/"+filename);
return "Page";
}
*/
}
package com.example.demo_image.controller.api;
import com.example.demo_image.controller.BaseController;
import com.example.demo_image.dto.PageResult;
import com.example.demo_image.dto.Result;
import com.example.demo_image.service.FileStoreService;
import com.example.demo_image.vo.sys.FileVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.InputStreamSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @author morningbear
* @date 20.7.13
*/
@Slf4j
@RestController
@ApiModel("处理图片上传的controller层接口")
public class FileController extends BaseController {
@Resource
private FileStoreService fileStoreService;
/**
* 图片文件上传
* @param file 图片文件
* @return HttpStatus
*/
@ApiOperation("图片文件上传")
@ApiImplicitParam(name = "file", value = "图片文件", required = true, dataType = "__file")
@PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
public ResponseEntity<Result> addFile(@RequestParam(value = "file") MultipartFile file){
if (file.isEmpty()){
return new ResponseEntity<>(new Result().serverError("文件不存在,请重新上传"), HttpStatus.NO_CONTENT);
}
fileStoreService.addFileAndStore(file);
return new ResponseEntity<>(new Result().ok("add success"), HttpStatus.OK);
}
/**
* 按文件名查询图片
* @param fileName 文件名
* @return 图片
*/
@ApiOperation("按文件名查询图片")
@ApiImplicitParam(name = "fileName", value = "文件名")
@GetMapping(value = "/file/fileName/{fileName}", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<InputStreamSource> findFileByFileName(@PathVariable String fileName){
FileVo fileVo = null;
try {
fileVo = fileStoreService.findFileByFileName(fileName);
}catch (IOException e){
e.printStackTrace();
}
if (fileVo == null){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(new InputStreamResource(fileVo.getContent()), HttpStatus.OK);
}
@ApiOperation("按id查询图片")
@ApiImplicitParam(name = "id", value = "编号")
@GetMapping(value = "/file/ID/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<InputStreamResource> findFileById(@PathVariable String id){
FileVo fileVo = null;
try {
fileVo = fileStoreService.findFileById(id);
}catch (IOException e){
e.printStackTrace();
}
if (fileVo == null){
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(new InputStreamResource(fileVo.getContent()), HttpStatus.OK);
}
/**
* 查询所有图片文件
*/
@ApiOperation("查询所有图片文件并返回详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNumber", value = "页码"),
@ApiImplicitParam(name = "pageSize", value = "页大小")
})
@GetMapping(value = "/files")
public ResponseEntity<PageResult<FileVo>> getAllFiles(@RequestParam(value = "pageNumber", defaultValue = "0") Integer pageNumber,
@RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize) throws IOException{
return new ResponseEntity(new Result().ok("success", fileStoreService.findAllFiles(pageNumber, pageSize)), HttpStatus.OK);
}
}
package com.example.demo_image.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.List;
/**
* @author morningbear
* @date 20.7.7
* @param <T>
*/
@ApiModel("统一返回分页对象")
@Slf4j
@Data
@Accessors(chain = true)
public class PageResult<T> implements Serializable {
@ApiModelProperty("当前页码")
private Integer pageNumber;
@ApiModelProperty("分页大小")
private Integer pageSize;
@ApiModelProperty("总数")
private Integer total;
@ApiModelProperty("数据")
private List<T> data;
public PageResult() {
}
public PageResult(Integer pageNumber, Integer pageSize, Integer total, List<T> data) {
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.total = total;
this.data = data;
}
}
package com.example.demo_image.dto;
import com.example.demo_image.util.FastJsonEnumSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import java.io.Serializable;
/**
* @author morningbear
* @date 20.7.11
* @param <T>
*/
@Data
@Slf4j
@Accessors(chain = true)
@NoArgsConstructor
public class Result<T> implements Serializable {
@JsonSerialize(using = FastJsonEnumSerializer.class)
@ApiModelProperty("响应码")
private HttpStatus code;
@ApiModelProperty("描述")
private String message;
@ApiModelProperty("数据")
private T data;
public Result(HttpStatus code, String message, T data){
this.code = code;
this.message = message;
this.data = data;
}
public Result result(HttpStatus code, String message, T data) {
return new Result(code, message, data);
}
public Result ok() {
return new Result(HttpStatus.OK, "操作成功", null);
}
public Result ok(String message) {
return new Result(HttpStatus.OK, message, null);
}
public Result ok(T data) {
return new Result(HttpStatus.OK, "", data);
}
public Result ok(String message, T data) {
return new Result(HttpStatus.OK, message, data);
}
public Result serverError() {
return new Result(HttpStatus.INTERNAL_SERVER_ERROR, "服务器异常", null);
}
public Result serverError(String message) {
return new Result(HttpStatus.INTERNAL_SERVER_ERROR, message, null);
}
public Result serverError(T data) {
return new Result(HttpStatus.INTERNAL_SERVER_ERROR, "", data);
}
public Result serverError(String message, T data) {
return new Result(HttpStatus.INTERNAL_SERVER_ERROR, message, data);
}
public Result requestError(String message) {
return new Result(HttpStatus.BAD_REQUEST, message, null);
}
}
package com.example.demo_image.entity;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Date;
/**
* @author morningbear
* @date 20.7.11
*/
@Slf4j
@Data
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
/**
* 记录标识
*/
@Id
@Column(name = "id")
private String id;
/**
* 记录创建者
*/
@Column(name = "create_by")
private Long createBy;
/**
* 记录修改者
*/
@Column(name = "update_by")
private Long updateBy;
/**
* 记录创建时间
*/
@Column(name = "create_at")
private Date createAt;
/**
* 记录修改时间
*/
@Column(name = "update_at")
private Date updateAt;
/**
* 记录是否被删除
*/
@Column(name = "is_del")
private Boolean isDel = Boolean.FALSE;
}
package com.example.demo_image.entity.sys;
import com.alibaba.fastjson.annotation.JSONField;
import com.example.demo_image.entity.BaseEntity;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.InputStream;
/**
* @author morningbear
* @date 20.7.11
*/
@Slf4j
@Data
@Accessors(chain = true)
@Entity
@Table(name = "img_file")
public class FileEntity extends BaseEntity {
/**
* 上传后的文件名
*/
@Column(name = "file_name")
private String fileName;
/**
* 原文件名
*/
@Column(name = "file_origin_name")
private String fileOriginName;
/**
* 文件路径
*/
@Column(name = "file_path")
private String filePath;
/**
* 文件大小
*/
@Column(name = "file_size")
private Long size;
@Column(name = "file_type")
private String fileType;
/**
* 文件内容
*/
@JSONField(serialize = false)
@Transient
private InputStream content;
}
config.stopbubbling=true
lombok.equalsandhashcode.callsuper=call
\ No newline at end of file
package com.example.demo_image.repository;
import com.example.demo_image.entity.sys.FileEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
/**
* @author morningbear
* @date 20.7.11
*/
public interface FileRepository extends JpaRepository<FileEntity, Long> {
/**
* 根据图片文件名查询图片
* @param filename 文件名
* @return 与图片有关的记录
*/
//@Query("select f from FileEntity f where f.fileName=:filename and f.isDel=false ")
FileEntity findByFileName(@Param("filename") String filename);
/**
* 根据ID查找
* @param id 图片id
* @return 对应的图片
*/
@Query("select f from FileEntity f where f.id=:id and f.isDel=false ")
FileEntity findFileEntityById(@Param("id") String id);
/**
* 查询所有未删除的图片
* @param of 分页请求
* @return 所有的图片记录
*/
@Query("select f from FileEntity f where f.isDel=false ")
Page<FileEntity> findAll(PageRequest of);
}
package com.example.demo_image.service;
import com.example.demo_image.dto.PageResult;
import com.example.demo_image.entity.sys.FileEntity;
import com.example.demo_image.repository.FileRepository;
import com.example.demo_image.util.FileUtils;
import com.example.demo_image.util.SnowflakeIdWorker;
import com.example.demo_image.vo.sys.FileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author morningbear
* @date 20.7.13
*/
@Slf4j
@Service
public class FileStoreService {
@Resource
private FileRepository fileRepository;
@Resource
private SnowflakeIdWorker snowflakeIdWorker;
private PageResult<FileVo> getFileVoPageResult(Integer pageSize, Integer pageNumber, Page<FileEntity> page) throws IOException{
List<FileVo> fileVoList = new ArrayList<>();
for(FileEntity fileEntity : page.getContent()){
log.info("fileId->{}", fileEntity.getId());
FileVo fileVo = new FileVo();
BeanUtils.copyProperties(fileEntity, fileVo);
fileVo.setId(fileEntity.getId());
//Path path = Paths.get(fileEntity.getFilePath(), fileEntity.getFileName());
//log.info("load image from " + path.toString());
//fileVo.setContent(Files.newInputStream(path));
fileVoList.add(fileVo);
}
return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), fileVoList);
}
/**
* 一些基本文件基本属性
* @param fileEntity 文件实体
*/
private void fileInitial(FileEntity fileEntity) {
String id = String.valueOf(snowflakeIdWorker.nextId());
fileEntity.setId(id);
fileEntity.setCreateAt(new Date());
fileEntity.setCreateBy(1L);
fileEntity.setUpdateAt(new Date());
fileEntity.setUpdateBy(1L);
}
/**
* 返回所有图片的信息
* @return PageResult<FileVo>
*/
public PageResult<FileVo> findAllFiles(Integer pageNumber, Integer pageSize) throws IOException{
Page<FileEntity> page = fileRepository.findAll(PageRequest.of(pageNumber, pageSize));
return getFileVoPageResult(pageSize, pageNumber, page);
}
/**
* 按ID查找图片
* @return FileVo
* 应该加一个检测找到的文件是不是空文件
*/
public FileVo findFileById(String id) throws IOException{
if (id == null){
return null;
}else {
log.info("id->{}", id);
FileEntity fileEntity = fileRepository.findFileEntityById(id);
FileVo fileVo = new FileVo();
BeanUtils.copyProperties(fileEntity, fileVo);
Path path = Paths.get(fileEntity.getFilePath(), fileEntity.getFileName());
fileVo.setContent(Files.newInputStream(path));
return fileVo;
}
}
/**
* 按文件名查找
* @param fileName 文件名
* @return 图片
*/
public FileVo findFileByFileName(String fileName) throws IOException{
if(fileName == null){
return null;
}else {
log.info("fileName->{}", fileName);
FileEntity fileEntity = fileRepository.findByFileName(fileName);
FileVo fileVo = new FileVo();
BeanUtils.copyProperties(fileEntity, fileVo);
Path path = Paths.get(fileEntity.getFilePath(), fileEntity.getFileName());
log.info("path->{}", path);
fileVo.setContent(Files.newInputStream(path));
return fileVo;
}
}
/**
* 添加图片。这个只保存图片信息,仅做测试
* @return 文件id
*/
@Transactional(rollbackOn = Exception.class)
public String addFile(FileVo fileVo){
FileEntity fileEntity = new FileEntity();
BeanUtils.copyProperties(fileVo, fileEntity);
fileInitial(fileEntity);
fileRepository.save(fileEntity);
return fileEntity.getId();
}
/**
* 上传后存盘
* @param file 文件
* @return 文件id
*/
@Transactional(rollbackOn = Exception.class)
public String addFileAndStore(MultipartFile file){
//基础路径
String basePath = "E:/xi_an";
//文件保存路径
String folder = FileUtils.getFolder();
String fileName = FileUtils.getFileName() + FileUtils.getFileNameSub(file.getOriginalFilename());
FileEntity fileEntity = new FileEntity();
fileInitial(fileEntity);
try {
Path filePath = Files.createDirectories(Paths.get(basePath, folder));
log.info("path->{}", filePath);
Path fullPath = Paths.get(basePath, folder, fileName);
log.info("fullPath->{}", fullPath);
Files.write(fullPath, file.getBytes(), StandardOpenOption.CREATE);
//文件信息
fileEntity.setFileOriginName(file.getOriginalFilename());
fileEntity.setFileType(file.getContentType());
fileEntity.setSize(file.getSize());
fileEntity.setFileName(fileName);
fileEntity.setFilePath(filePath.toString());
fileRepository.save(fileEntity);
}catch (IOException e){
e.printStackTrace();
}
return fileEntity.getId();
}
/**
@Value("${file.upload.path}")
private String file;
private final Path filePath = Paths.get(file);
public Stream<Path> loadAll(){
try {
return Files.walk(filePath, 1)
.filter(path -> !path.equals(filePath))
.map(filePath::relativize);
}
catch (IOException e){
throw new RuntimeException(e);
}
}
*/
}
package com.example.demo_image.util;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.http.HttpStatus;
import java.io.IOException;
/**
* @author morningbear
* @date 20.7.7
*/
public class FastJsonEnumSerializer extends JsonSerializer {
/**
* 序列化重写
*/
@Override
public void serialize(Object object, JsonGenerator jsonGeneratorn, SerializerProvider serializerProvider) throws IOException{
if (object instanceof HttpStatus){
HttpStatus httpStatus = (HttpStatus) object;
jsonGeneratorn.writeNumber(httpStatus.value());
}
}
}
package com.example.demo_image.util;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author morningbear
* @date 20.7.7
*/
@Slf4j
public class FileUtils {
/**
* 获取分隔符
* @return fileSeparator
*/
private static String getFileSeparator(){
return System.getProperty("file.separator");
}
/**
* 上传后的文件夹名
* @return folderName
*/
public static String getFolder(){
return LocalDateTime.now().format(DateTimeFormatter
.ofPattern("yyyy/MM/dd")) + getFileSeparator();
}
/**
* 获取随机文件名
* @return
*/
public static String getFileName(){
return UUIDUtil.getUid("FL_" , 17) ;
}
/**
* 获取文件后缀
* @param fileName 文件名
* @return jpg/png/...
*/
public static String getFileNameSub(String fileName){
return fileName.substring(fileName.indexOf("."));
}
}
package com.example.demo_image.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 雪花ID
*
* @author morningbear
* @date 20.7.7
*/
@Slf4j
@Component
public class SnowflakeIdWorker {
/**
* 开始时间截 (2015-01-01)
*/
private final long twepoch = 1420041600000L;
/**
* 机器id所占的位数
*/
private final long workerIdBits = 5L;
/**
* 数据标识id所占的位数
*/
private final long datacenterIdBits = 5L;
/**
* 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数)
*/
private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
/**
* 支持的最大数据标识id,结果是31
*/
private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
/**
* 序列在id中占的位数
*/
private final long sequenceBits = 12L;
/**
* 机器ID向左移12位
*/
private final long workerIdShift = sequenceBits;
/**
* 数据标识id向左移17位(12+5)
*/
private final long datacenterIdShift = sequenceBits + workerIdBits;
/**
* 时间截向左移22位(5+5+12)
*/
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
/**
* 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095)
*/
private final long sequenceMask = -1L ^ (-1L << sequenceBits);
/**
* 工作机器ID(0~31)
* 通过应用配置文件获取
*/
@Value("${snowflake.workerId}")
private long workerId;
/**
* 数据中心ID(0~31)
* 通过应用配置文件获取
*/
@Value("${snowflake.datacenterId}")
private long datacenterId;
/**
* 毫秒内序列(0~4095)
*/
private long sequence = 0L;
/**
* 上次生成ID的时间截
*/
private long lastTimestamp = -1L;
/**
* 获得下一个ID (该方法是线程安全的)
*
* @return SnowflakeId
*/
public synchronized long nextId() {
long timestamp = timeGen();
/**
* 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
*/
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
/**
* 如果是同一时间生成的,则进行毫秒内序列
*/
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
/**
* 上次生成ID的时间截
*/
lastTimestamp = timestamp;
/**
* 移位并通过或运算拼到一起组成64位的ID
*/
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
}
/**
* 阻塞到下一个毫秒,直到获得新的时间戳
*
* @param lastTimestamp 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* 返回以毫秒为单位的当前时间
*
* @return 当前时间(毫秒)
*/
protected long timeGen() {
return System.currentTimeMillis();
}
}
package com.example.demo_image.util;
import org.apache.commons.lang3.StringUtils;
import java.math.BigInteger;
import java.util.UUID;
/**
*
* @author morningbear
* @date 20.7.13
* UUID工具类
*/
public class UUIDUtil {
private static final String[] chars = new String[]{"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
/**
* 返回22位无重复UUID串
*
* @return uid
*/
public static String getUid() {
return getUUIDStr();
}
/**
* 返回前缀+22位无重复UUID串
*
* @param prefix 前缀
* @return pre + uid
*/
public static String getUid(String prefix) {
return prefix + getUUIDStr();
}
/**
* 返回前缀+指定长度无重复UUID串
* @param prefix
* @param Length
* @return
*/
public static String getUid(String prefix , int Length){
return prefix+getUUIDByLength(Length);
}
/**
* 返回8位短UUID串
*
* @return uid
*/
public static String getShortUid() {
StringBuilder shortBuffer = new StringBuilder();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
/**
* 返回一个指定长度的 UUId字符串
* @param length
* @return
*/
public static String getUUIDByLength(int length) {
if (length < 1) {
return null;
}
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i <= (length / 22); i++) {
sBuffer.append(getUid());
}
//截取 length 长度
return sBuffer.toString().substring(0,length);
}
private static BigInteger unsigned2BigInt(long value) {
if (value >= 0) {
return BigInteger.valueOf(value);
}
// 将value 转 无符号数
long lowValue = value & 0x7fffffffffffffffL;
return BigInteger.valueOf(lowValue).
add(BigInteger.valueOf(Long.MAX_VALUE)).
add(BigInteger.valueOf(1));
}
/**
* 获取UUID字符串
* @return
*/
private static String getUUIDStr() {
UUID uuid = UUID.randomUUID();
BigInteger pt1 = unsigned2BigInt(uuid.getMostSignificantBits());
BigInteger pt2 = unsigned2BigInt(uuid.getLeastSignificantBits());
StringBuilder sb = new StringBuilder();
while (pt1.longValue() != 0) {
BigInteger[] s = pt1.divideAndRemainder(BigInteger.valueOf(chars.length));
sb.append(chars[s[1].intValue()]);
pt1 = s[0];
}
sb.append(StringUtils.repeat(chars[0], 11 - sb.length()));
while (pt2.longValue() != 0) {
BigInteger[] s = pt2.divideAndRemainder(BigInteger.valueOf(chars.length));
sb.append(chars[s[1].intValue()]);
pt2 = s[0];
}
sb.append(StringUtils.repeat(chars[0], 22 - sb.length()));
return StringUtils.reverse(sb.toString());
}
}
package com.example.demo_image.vo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
/**
* @author morningbear
* @date 20.7.7
*/
@ApiModel(value = "基本实体")
@Slf4j
@Data
@Accessors(chain = true)
public class BaseVo implements Serializable {
@ApiModelProperty(value = "编号")
@Resource
private String id;
@JsonIgnore
private Long createBy;
@JsonIgnore
private Long updateBy;
@JsonIgnore
private Date createAt;
@JsonIgnore
private Date updateAt;
}
package com.example.demo_image.vo.sys;
import com.alibaba.fastjson.annotation.JSONField;
import com.example.demo_image.vo.BaseVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;
/**
* @author morningbear
* @date 20.7.13
*/
@Data
@ApiModel("图片文件实体")
@Slf4j
@Accessors(chain = true)
public class FileVo extends BaseVo {
@ApiModelProperty("上传后的文件名")
private String fileName;
@ApiModelProperty("原文件名")
private String fileOriginName;
@ApiModelProperty("文件大小")
private Long size;
@ApiModelProperty("文件类型")
private String fileType;
@ApiModelProperty("图片")
@JSONField(serialize = false)
private InputStream content;
}
server.port=8899
file.upload.path=E://images/
file.upload.path.relative=/images/**
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/image_db?characterEncoding=UTF-8&useUnicode=true&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=liu
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.open-in-view=true
snowflake.workerId=0
snowflake.datacenterId=0
spring.profiles.active=dev
\ No newline at end of file
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../css/bootstrap.css">
<title>ImageTest</title>
</head>
<body>
<form action="../upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*">
<br>
<input type="submit" value="上传" class="btn btn-success">
</form>
[[${filename}]]
<br>
<img th:src="@{${filename}}" alt="图片">
</body>
</html>
\ No newline at end of file
package com.example.demo_image;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoImageApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment