Browse Source

增加基于mongodb的文件服务实现,无安全机制

airhead 8 years ago
parent
commit
2d17a696ba

+ 24 - 0
hos-dfs/.gitignore

@ -0,0 +1,24 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

+ 233 - 0
hos-dfs/mvnw

@ -0,0 +1,233 @@
#!/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
#
#    http://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.
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Maven2 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
           #
           # Look for the Apple JDKs first to preserve the existing behaviour, and then look
           # for the new JDKs provided by Oracle.
           #
           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then
             #
             # Oracle JDKs
             #
             export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi
           if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=`/usr/libexec/java_home`
           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 Migwn, 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)`"
  # TODO classpath?
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
# 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"`
fi
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
  local basedir=$(pwd)
  local wdir=$(pwd)
  while [ "$wdir" != '/' ] ; do
    if [ -d "$wdir"/.mvn ] ; then
      basedir=$wdir
      break
    fi
    wdir=$(cd "$wdir/.."; pwd)
  done
  echo "${basedir}"
}
# concatenates all lines of a file
concat_lines() {
  if [ -f "$1" ]; then
    echo "$(tr -s '\n' ' ' < "$1")"
  fi
}
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)}
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
# 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} "$@"

+ 145 - 0
hos-dfs/mvnw.cmd

@ -0,0 +1,145 @@
@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    http://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 Maven2 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 key stroke 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 enable echoing my 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
set MAVEN_CMD_LINE_ARGS=%*
@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="".\.mvn\wrapper\maven-wrapper.jar""
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS%
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%

+ 55 - 0
hos-dfs/pom.xml

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.yihu.hos</groupId>
        <artifactId>hos-parent</artifactId>
        <version>1.3.0</version>
        <relativePath>../hos-parent</relativePath>
    </parent>
    <artifactId>hos-dfs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>hos-dfs</name>
    <description>hos file and mq moddule</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.zbus</groupId>
            <artifactId>zbus</artifactId>
        </dependency>
    </dependencies>
</project>

+ 31 - 0
hos-dfs/src/main/java/com/yihu/com/hos/HosDfsApplication.java

@ -0,0 +1,31 @@
package com.yihu.com.hos;
import com.yihu.com.hos.configuration.ZbusConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.zbus.mq.server.MqServer;
import org.zbus.mq.server.MqServerConfig;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class HosDfsApplication implements CommandLineRunner {
    @Autowired
    private ZbusConfiguration zbusConfiguration;
    public static void main(String[] args) {
        SpringApplication.run(HosDfsApplication.class, args);
    }
    @Override
    public void run(String... strings) throws Exception {
        MqServerConfig config = new MqServerConfig();
        config.serverPort = zbusConfiguration.getZbusPort();
        config.storePath = zbusConfiguration.getZbusStore();
        final MqServer server = new MqServer(config);
        server.start();
        System.out.println("zbus 启动成功!");
    }
}

+ 0 - 57
hos-dfs/src/main/java/com/yihu/com/hos/configuration/MongoConfig.java

@ -1,57 +0,0 @@
package com.yihu.com.hos.configuration;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static java.util.Collections.singletonList;
/**
 * @created Airhead 2016/7/27.
 */
@Configuration
public class MongoConfig {
    @Value("${spring.data.mongodb.host}")
    private String host;
    @Value("${spring.data.mongodb.port}")
    private int port;
    @Value("${spring.data.mongodb.username}")
    private String username;
    @Value("${spring.data.mongodb.password}")
    private String password;
    @Value("${spring.data.mongodb.authenticationDatabase}")
    private String authenticationDatabase;
    @Bean
    public MongoClient mongoClient() throws Exception {
        return new MongoClient(singletonList(new ServerAddress(host, port)),
                singletonList(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray())));
    }
    @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient(singletonList(new ServerAddress(host, port)),
                singletonList(MongoCredential.createCredential(username, authenticationDatabase, password.toCharArray())));
    }
    public String getHost() {
        return host;
    }
    public int getPort() {
        return port;
    }
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
}

+ 28 - 50
hos-dfs/src/main/java/com/yihu/com/hos/controller/DFSController.java

@ -1,73 +1,51 @@
package com.yihu.com.hos.controller;
import com.yihu.com.hos.service.DFSService;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
/**
 * 基于Mongo的文件系统
 * <p>
 * Created by l4qiang on 2017-04-10.
 */
@RestController
@RequestMapping(value = "/dfs")
public class DFSController {
    @Autowired
    private DFSService dfsService;
    /**
     *  //TODO 后 独立到对应的rest接口中
     * 上传文件
     * @param response
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/upload/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(
            HttpServletResponse response, @PathVariable("fileName")  String fileName,
            @ApiParam(name = "pack", value = "文件", allowMultiple = true)
            @RequestPart() MultipartFile pack
    ) {
        try {
            return dfsService.uploadFile(pack.getInputStream(), fileName);
        } catch (Exception e) {
            return Result.error("上传文件出错!");
        }
    @RequestMapping(value = "file/{fileId}/info", method = RequestMethod.GET)
    public ResponseEntity info(@PathVariable("fileId") String fileId) {
        return dfsService.info(fileId);
    }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.GET)
    public ResponseEntity retrieve(@PathVariable("fileId") String fileId, HttpServletResponse httpServletResponse) {
        return dfsService.retrieve(fileId, httpServletResponse);
    }
    @RequestMapping(value = "file/{fileId}/download", method = RequestMethod.GET)
    public ResponseEntity download(@PathVariable("fileId") String fileId) {
        return dfsService.download(fileId);
    }
    /**
     *  //TODO 后 独立到对应的rest接口中
     * 文件下载
     * @return
     */
    @RequestMapping(value = "/down/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Object down(HttpServletResponse response, @PathVariable("fileName")  String fileName) {
        try {
            OutputStream os = response.getOutputStream();
            return dfsService.dowFile(os, fileName);
        } catch (Exception e) {
            return Result.error("下载文件出错!");
        }
    @RequestMapping(value = "file", method = RequestMethod.POST)
    public ResponseEntity store(@RequestPart MultipartFile file) {
        return dfsService.store(file);
    }
    /**
     *  //TODO 后 独立到对应的rest接口中
     * 删除文件
     * @return
     */
    @RequestMapping(value = "/delFile/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object delFile(HttpServletResponse response, @PathVariable("fileName")  String fileName) {
        try {
            return dfsService.delFile(fileName);
        } catch (Exception e) {
            return Result.error("下载文件失败!");
        }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.POST)
    public ResponseEntity update(@PathVariable("fileId") String fileId, @RequestParam("file") MultipartFile file) {
        return dfsService.update(fileId, file);
    }
    @RequestMapping(value = "file/{fileId}", method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable("fileId") String fileId) {
        return dfsService.delete(fileId);
    }
}

+ 91 - 31
hos-dfs/src/main/java/com/yihu/com/hos/service/DFSService.java

@ -1,68 +1,128 @@
package com.yihu.com.hos.service;
import com.yihu.com.hos.configuration.MongoConfig;
import com.yihu.hos.core.encrypt.DES;
import com.yihu.hos.web.framework.model.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
/**
 * Mongodb file system
 * Created by l4qiang on 2017-04-10.
 */
@Service
public class DFSService {
    @Autowired
    private GridFsOperations operations;
    private GridFsOperations gridFsOperations;
    @Autowired
    private MongoConfig mongoConfig;
    private ObjectMapper objectMapper;
    public Result dowFile(OutputStream os, String fileName) {
    public ResponseEntity info(String fileId) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase("dfs"), os, fileName);
            if (succ) {
                return Result.success("读取文件成功");
            } else {
                return Result.success("读取文件失败");
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
            if (gridFSDBFile == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
            }
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("id", gridFSDBFile.getId().toString());
            objectNode.put("filename", gridFSDBFile.getFilename());
            objectNode.put("contentType", gridFSDBFile.getContentType());
            objectNode.put("length", gridFSDBFile.getLength());
            objectNode.put("uploadDate", gridFSDBFile.getUploadDate().toString());
//            objectNode.put("aliases", gridFSDBFile.getAliases().toString());
            objectNode.put("md5", gridFSDBFile.getMD5());
            String info = objectMapper.writeValueAsString(objectNode);
            return ResponseEntity.ok()
                    .body(info);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("读取文件异常");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public Result uploadFile(InputStream inputStream, String fileName) {
    public ResponseEntity retrieve(String fileId, HttpServletResponse httpServletResponse) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            String saveFileName = GridFSUtil.uploadFile(inputStream, fileName, null);
            if (saveFileName != null) {
                return Result.success("上传文件成功");
            } else {
                return Result.error("上传文件失败");
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
            if (gridFSDBFile == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
            }
            ServletOutputStream outputStream = httpServletResponse.getOutputStream();
            org.apache.commons.io.IOUtils.copy(gridFSDBFile.getInputStream(), outputStream);
            outputStream.close();
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传文件异常");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public Result delFile(String fileName) {
    public ResponseEntity download(String fileId) {
        Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
        GridFSDBFile gridFSDBFile = gridFsOperations.findOne(query);
        if (gridFSDBFile == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file was not found");
        }
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + gridFSDBFile.getFilename() + "\"")
                .body(Optional.of(gridFSDBFile.getInputStream()));
    }
    public ResponseEntity store(MultipartFile file) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            boolean succ = GridFSUtil.deleteFile(fileName);
            if (succ) {
                return Result.success("删除文件成功");
            } else {
                return Result.success("删除文件失败");
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            GridFSFile gridFSFile = gridFsOperations.store(inputStream, fileName);
            if (gridFSFile != null) {
                return ResponseEntity.status(HttpStatus.OK).body(gridFSFile.getId().toString());
            }
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("file was fail to store");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    public ResponseEntity update(String fileId, MultipartFile file) {
        ResponseEntity delete = delete(fileId);
        if (delete.getStatusCode() != HttpStatus.OK) {
            return delete;
        }
        return store(file);
    }
    public ResponseEntity delete(String fileId) {
        try {
            Query query = Query.query(Criteria.where("_id").is(new ObjectId(fileId)));
            gridFsOperations.delete(query);
            return ResponseEntity.status(HttpStatus.OK).body("file was deleted");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除文件异常");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
}

+ 0 - 385
hos-dfs/src/main/java/com/yihu/com/hos/service/GridFSUtil.java

@ -1,385 +0,0 @@
package com.yihu.com.hos.service;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.mongodb.gridfs.GridFSDBFile;
import com.yihu.hos.core.file.FileUtil;
import eu.medsea.mimeutil.MimeUtil;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsCriteria;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.stereotype.Component;
import java.io.*;
import java.sql.Blob;
import java.util.*;
/**
 * MongoDB GridFS 操作类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/7/21.
 */
@Component
public class GridFSUtil {
    public static final int defaultChunkSize = 1024 * 1024 * 4;
    public static GridFsOperations gridFsOperations;
    public static ObjectId uploadFile( Blob blob, String fileType, Map<String, Object> params) {
//        String fileName = UUID.randomUUID().toString() + "." + fileType;
        //自定义字段
        Document metaDocument = new Document();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                metaDocument.append(key, entry.getValue());
            }
        }
        // Create some custom options
        GridFSUploadOptions gridFSUploadOptions = new GridFSUploadOptions()
                .chunkSizeBytes(defaultChunkSize).metadata(metaDocument);
        try {
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(blob.getBinaryStream(), gridFSUploadOptions);
            if (gridFSFile != null) {
                return (ObjectId) gridFSFile.getId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 上传文件至Mongodb by GridFS
     *
     * @param filePath        上传的文件路径
     * @param saveFileName   保存到mongo的文件名
     * @param params          自定义保存字段
     */
    public static String uploadFile(String filePath,String saveFileName, Map<String, Object> params) {
        //自定义字段
        Document metaDocument = new Document();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                metaDocument.append(key, entry.getValue());
            }
        }
        // Create some custom options
        FileInputStream fileInputStream = null;
        try {
            File readFile = new File(filePath);
            fileInputStream = new FileInputStream(readFile);
            gridFsOperations.delete(Query.query(GridFsCriteria.where("filename").is(saveFileName)));//删除原来的文件,保证唯一
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(fileInputStream,saveFileName,"",metaDocument);
            if (gridFSFile != null) {
                return saveFileName;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 从 mongodb GridFS 下载文件
     *
     * @param dbName   数据库名
     * @param savePath 文件保存路径
     * @param objectId GridFS文件保存ObjectId
     * @return
     */
    public static String downFile(String dbName, String savePath, ObjectId objectId) {
        FileOutputStream fileOutputStream = null;
        try {
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
            fileOutputStream = new FileOutputStream(savePath);
            gridFSDBFile.writeTo(fileOutputStream);
            return savePath;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 批量下载文件保存(根据 fs.files集合)
     *
     * @param dbName   数据库名
     * @param savePath 文件保存的路径
     * @param fsFiles  fs.files
     * @return 以“,”分割的文件名
     */
    public static Map<String, StringBuffer> downFileList(String dbName, String savePath, List<GridFSDBFile> fsFiles) {
        StringBuffer stringBuffer = new StringBuffer();
        Map<String, String> fileNames = new HashMap<>();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (fsFiles != null && fsFiles.size() > 0) {
            for (GridFSDBFile fsFile : fsFiles) {
                Object objectId = fsFile.getId();
                String fileType = fsFile.getFilename().substring(fsFile.getFilename().lastIndexOf("."));
                String newName = UUID.randomUUID().toString() + "." + fileType;
                GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
                try {
                    gridFSDBFile.writeTo(out);
                    boolean success = FileUtil.writeFile(savePath + "/" + newName, out.toByteArray(), "utf-8");
                    if (!success) {
                        break;
                    }
                    String type = getMimeType(out.toByteArray());
                    fileNames.put(newName, type);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return groupDataMap(fileNames);
        } else {
            return null;
        }
    }
    /**
     * 删除 mongodb-GridFS文件
     *
     * @param dbName
     * @param objectId
     */
    public static void deleteFile(String dbName, ObjectId objectId) {
        gridFsOperations.delete(Query.query(GridFsCriteria.where("_id").is(objectId)));
    }
    /**
     * 查询fs.files 数据 in GridFS
     *
     * @param filters 查询条件
     * @return files集合
     */
    public static List<GridFSDBFile> findFiles( Map<String, Object> filters) {
        Query query = new Query();
        if (filters != null) {
            filters.forEach((key, value) -> query.addCriteria(GridFsCriteria.where(key).is(value)));
        }
        return gridFsOperations.find(query);
    }
    /**
     * 根据ObjectID集合查询GridFS 文件列表
     *
     * @param dbName 数据库名
     * @param ids    objectId集合
     * @return
     */
    public static List<GridFSDBFile> findFsFiles(String dbName, List<ObjectId> ids) {
        List<GridFSDBFile> list = new ArrayList<>();
        ids.forEach(objectId -> {
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("_id").is(objectId)));
            list.add(gridFSDBFile);
        });
        return list;
    }
    /*************************************** MineType 工具类 *********************************/
    /**
     * 获取文件Mine-Type
     *
     * @param file
     * @return
     */
    public static String getMimeType(File file) {
        MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
        Collection<?> collection = MimeUtil.getMimeTypes(file);
        return collection.toString();
    }
    public static String getMimeType(byte[] bytes) {
        MimeUtil.registerMimeDetector("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
        Collection<?> collection = MimeUtil.getMimeTypes(bytes);
        return collection.toString();
    }
    /**
     * 非结构化档案--文件类型map生成
     *
     * @param map
     * @return
     */
    public static Map<String, StringBuffer> groupDataMap(Map<String, String> map) {
        Map<String, StringBuffer> result = new HashMap<String, StringBuffer>();
        Iterator<String> rs = map.keySet().iterator();
        while (rs.hasNext()) {
            String key = rs.next();
            String value = map.get(key);
            if (result.containsKey(value)) {
                result.get(value).append(",").append(key);
            } else {
                result.put(value, new StringBuffer(key));
            }
        }
        return result;
    }
    /**
     *  上传文件至Mongodb by GridFS
     * @param saveFileName  保存到mongo的文件名
     * @param inputStream   文件流
     * @param params         metaData数据
     * @return
     */
    public static String uploadFile( InputStream inputStream,String saveFileName,Map<String ,Object> params) {
        //metaData参数
        Document metaDocument = new Document();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                String key = entry.getKey();
                metaDocument.append(key, entry.getValue());
            }
        }
        try {
            gridFsOperations.delete(Query.query(GridFsCriteria.where("filename").is(saveFileName)));//删除原来的文件,保证唯一
            com.mongodb.gridfs.GridFSFile gridFSFile = gridFsOperations.store(inputStream, saveFileName, "", metaDocument);
            if (gridFSFile != null) {
                return saveFileName;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 据文件名返回文件,只返回第一个
     *
     * @param fileName
     * @return
     */
    public static boolean readFile(MongoDatabase db, OutputStream os, String fileName) {
        try {
            GridFSBucket gridFS = GridFSBuckets.create(db);
            gridFS.downloadToStreamByName(fileName, os);
            os.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 读取文件内容
     *
     * @param fileName 文件名
     * @return
     */
    public static String readFileContent( String fileName) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            GridFSDBFile gridFSDBFile = gridFsOperations.findOne(Query.query(GridFsCriteria.where("filename").is(fileName)));
            gridFSDBFile.writeTo(byteArrayOutputStream);
            return byteArrayOutputStream.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 从 mongodb GridFS 下载文件
     *
     * @param savePath 文件保存路径
     * @param fileName GridFS文件名
     * @return
     */
    public static String downFile( String savePath, String fileName) {
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(savePath);
            fileOutputStream = new FileOutputStream(file);
            List<GridFSDBFile> gridFSDBFiles = gridFsOperations.find(Query.query(GridFsCriteria.where("filename").is(fileName)));
            if (gridFSDBFiles==null || gridFSDBFiles.isEmpty()){
                return null;
            }else {
                GridFSDBFile gridFSDBFile = gridFSDBFiles.get(0);
                gridFSDBFile.writeTo(fileOutputStream);
                return savePath;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    public static boolean deleteFile(String fileName){
        try {
            gridFsOperations.delete(Query.query(GridFsCriteria.where("filename").is(fileName)));//删除原来的文件,保证唯一
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

+ 21 - 0
hos-dfs/src/main/resources/application.yml

@ -0,0 +1,21 @@
server:
  port: 9010
security:
  basic:
    enabled: false
---
spring:
  profiles: dev
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      gridFsDatabase: dfs
      database: runtime
hos:
  zbus:
    port: 9020
    store: ./store

+ 16 - 0
hos-dfs/src/test/java/com/yihu/com/hos/HosFdsApplicationTests.java

@ -0,0 +1,16 @@
package com.yihu.com.hos;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HosFdsApplicationTests {
	@Test
	public void contextLoads() {
	}
}