项目预览网址 : http://trans.waibaobao.cn/file/pics

安装:前提安装mongodb 作为文件储存库

1)nginx-gridfs安装

a、安装所用依赖包 yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++

b、下载nginx-gridfs 源代码 git clone https://github.com/mdirolf/nginx-gridfs.git(前提安装git)

进入nginx-gridfs 后

git checkout v0.8

git branch

git submodule init

git submodule update

2)a、nginx 下载安装 
wget http://nginx.org/download/nginx-1.14.2.tar.gz      解压 tar -zxvf nginx-1.14.2.tar.gz

b、进入nginx-1.14.2后
./configure --prefix=/usr/local/nginx   --with-openssl=/usr/include/openssl --add-module=../nginx-mongodb/nginx-gridf

c、make -j8 && make install -j8 (此时会多个nginx目录)

我的报错了

d、vi objs/Makefile  把第3行的-Werror错误去后  make -j8 && make install -j8

e、进入nginx的配置文件更改

    server {
listen 10010;
server_name localhost; location /pics/ {
gridfs zrdb
field=filename
type=string;
mongo 127.0.0.1:27017;
} }

test 代表mongodb 数据库名

pics 为配置java 访问mongodb的访问路径

mongodb.url=http://47.106.32.125/pics/
ivs.mongodb.host=47.106.32.125
ivs.mongodb.port=27017
ivs.mongodb.database=test

ok!!!

下面说下java 怎么实现连接mongodb 数据库

话不多说上代码:

1)配置类 加载mongodb dev 的配置文件

package com.fyun.tewebcore.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mongodb.MongoClientOptions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import java.io.IOException; @Component
@PropertySource("classpath:/config/mongodb-${spring.profiles.active:dev}.properties")
@Configuration
public class MongoConfig extends JsonSerializer<String> {
public static String mongodbUrl; public static String urlKey = "mongodbUrl"; @Override
public void serialize(String String, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
if (StringUtils.isNotEmpty(String))
jsonGenerator.writeString(mongodbUrl + String);
else
jsonGenerator.writeString(" ");
} @Value("${mongodb.url}")
public void setMongodbUrl(String mongodbUrl) {
MongoConfig.mongodbUrl = mongodbUrl;
} @Bean
public MongoClientOptions mongoOptions(){
return MongoClientOptions.builder().maxConnectionIdleTime(3000).build();
}
}

2)写一个文件上传controller

package com.fyun.tewebcore.Controller.file;
import com.fyun.common.model.base.CommonResponse;
import com.fyun.common.model.base.SystemCode;
import com.fyun.common.utils.file.MongoGridfsServiceImpl;
import com.fyun.common.utils.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* @author zhourui
* @create 2019/12/31
*/
@Api("文件上传处理器")
@RestController
@RequestMapping("/file")
public class FileController {
private static final Logger logger= LoggerFactory.getLogger(FileController.class);
@Autowired
private MongoGridfsServiceImpl mongoGridfsService;
@ApiOperation(value = "文件上传")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResponse upload(@RequestParam(value = "file",required = false) MultipartFile commonsMultipartFile, HttpServletResponse responses) {
responses.addHeader("Access-Control-Allow-Origin", "*");
CommonResponse response = new CommonResponse();
logger.info("#commonsMultipartFile==getOriginalFilename入参" +commonsMultipartFile.getOriginalFilename());
logger.info("#commonsMultipartFile==入参" +commonsMultipartFile);
logger.info("#commonsMultipartFile入参" +commonsMultipartFile.getContentType());
try {
String fileName = StringUtils.getFileName(commonsMultipartFile.getOriginalFilename().substring(commonsMultipartFile.getOriginalFilename().lastIndexOf(".")));
logger.info("#文件名称=="+fileName);
mongoGridfsService.save(commonsMultipartFile.getInputStream(),fileName);
response.setCode(SystemCode.SYSTEM_SUCCESS.getCode());
response.setMessage(SystemCode.SYSTEM_SUCCESS.getMessage());
response.setData(fileName);
return response;
} catch (Exception e) {
logger.error("未知异常");
response.setCode(SystemCode.FILE_UPLOAD_EXCEPTION.getCode());
response.setMessage(SystemCode.FILE_UPLOAD_EXCEPTION.getMessage()+e.getMessage());
return response;
}
}
}

3)调服务接口对gridfs实现增删改查

package com.fyun.common.utils.file;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct;
import java.io.*;
import java.util.List; /**
* Created by 78729 on 2017/7/7.
*/
@Service
@PropertySource("classpath:/config/mongodb-${spring.profiles.active:dev}.properties")
public class MongoGridfsServiceImpl { private static Logger logger = Logger.getLogger(MongoGridfsServiceImpl.class);
private GridFS gridFS;
private MongoClient mongoClient;
@Value("${ivs.mongodb.host}")
private String mongoDBServerUrl = "localhost";
@Value("${ivs.mongodb.port}")
private String portStr;
private int port = 27017;
@Value("${ivs.mongodb.database}")
private String dataBase; public void setMongoDBServerUrl(String mongoDBServerUrl) {
this.mongoDBServerUrl = mongoDBServerUrl;
} public void setPort(int port) {
this.port = port;
} public void setDataBase(String dataBase) {
this.dataBase = dataBase;
} @PostConstruct
public void init() {
try {
mongoClient = new MongoClient(mongoDBServerUrl, port);
DB db = mongoClient.getDB(dataBase);
gridFS = new GridFS(db);
} catch (Exception e) {
logger.error("mongoClient:UnknownHostException", e);
}
} public void saveById(InputStream in, String id) {
GridFSDBFile gridFSDBFile = getById(id);
if (gridFSDBFile != null) {
logger.error(String.format("%s,文件已经存在", gridFSDBFile.getFilename()));
return;
} GridFSInputFile gridFSInputFile = gridFS.createFile(in);
gridFSInputFile.setId(id);
gridFSInputFile.save();
} public void save(String filePath, String fileName) {
GridFSDBFile gridFSDBFile = getByFileName(fileName);
if (gridFSDBFile != null) {
logger.error(String.format("%s,文件已经存在", gridFSDBFile.getFilename()));
return;
}
try {
FileInputStream in = new FileInputStream(new File(filePath));
byte[] buffer = new byte[in.available()];
InputStream input = new ByteArrayInputStream(buffer); GridFSInputFile gridFSInputFile = gridFS.createFile(input, fileName);
gridFSInputFile.setFilename(fileName);
gridFSInputFile.save();
} catch (Exception e) {
logger.error(String.format("文件:%s,未找到", filePath), e);
}
} public void save(InputStream in, String fileName) {
GridFSDBFile gridFSDBFile = getByFileName(fileName);
if (gridFSDBFile != null) {
logger.error(String.format("%s,文件已经存在", gridFSDBFile.getFilename()));
return;
} GridFSInputFile gridFSInputFile = gridFS.createFile(in, fileName);
gridFSInputFile.setFilename(fileName);
gridFSInputFile.save();
} public void save(byte[] in, String saveFileName) {
GridFSDBFile gridFSDBFile = getByFileName(saveFileName);
if (gridFSDBFile != null) {
logger.error(String.format("%s,文件已经存在", gridFSDBFile.getFilename()));
return;
}
try {
InputStream input = new ByteArrayInputStream(in);
GridFSInputFile gridFSInputFile = gridFS.createFile(input, saveFileName);
gridFSInputFile.setFilename(saveFileName);
gridFSInputFile.save();
} catch (Exception e) {
logger.error(String.format("文件:%s,未找到", saveFileName), e);
}
} public void save(InputStream in, String fileName, boolean replace) {
GridFSDBFile gridFSDBFile = getByFileName(fileName);
if ((gridFSDBFile != null) && (replace)) {
this.gridFS.remove(fileName);
}
GridFSInputFile gridFSInputFile = gridFS.createFile(in, fileName);
gridFSInputFile.setFilename(fileName);
gridFSInputFile.save();
} public GridFSDBFile getById(String id, String outFilePath) {
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = gridFS.findOne(query);
if ((gridFSDBFile != null) && (StringUtils.isNotEmpty(outFilePath))) {
writeToFile(gridFSDBFile, outFilePath);
}
return gridFSDBFile;
} public GridFSDBFile getById(String id) {
DBObject query = new BasicDBObject("_id", id);
return gridFS.findOne(query);
} /**
* 根据id查询文件并通过流的方式返回,
*
* @param id 文件ID
* @return
*/ public InputStream getStreamById(String id) {
DBObject query = new BasicDBObject("_id", id);
GridFSDBFile gridFSDBFile = gridFS.findOne(query);
return gridFSDBFile.getInputStream();
} public GridFSDBFile getByFileName(String fileName) {
DBObject query = new BasicDBObject("filename", fileName);
return gridFS.findOne(query);
} public GridFSDBFile getByFileName(String fileName, String outFilePath) {
DBObject query = new BasicDBObject("filename", fileName);
GridFSDBFile gridFSDBFile = gridFS.findOne(query);
if ((gridFSDBFile != null) && (StringUtils.isNotEmpty(outFilePath))) {
writeToFile(gridFSDBFile, outFilePath);
}
return gridFSDBFile;
} public List<GridFSDBFile> getListByFileName(String fileName) {
DBObject query = new BasicDBObject("filename", fileName);
List gridFSDBFileList = gridFS.find(query);
return gridFSDBFileList;
} public void removeById(String id) {
DBObject remove = new BasicDBObject("_id", id);
gridFS.remove(remove);
} public void removeByFileName(String fileName) {
gridFS.remove(fileName);
} private void writeToFile(GridFSDBFile gridFSDBFile, String outFilePath) {
try {
gridFSDBFile.writeTo(outFilePath);
} catch (IOException e) {
logger.error(String.format("%s,文件输出异常", gridFSDBFile.getFilename()), e);
}
} }

wget http://nginx.org/download/nginx-1.14.2.tar.gz

最新文章

  1. 优化Android Studio/Gradle构建
  2. 返回顶部demo
  3. 曲线提取数据Engauge Digitizer
  4. 删除linux系统服务
  5. 转最简便安装python+selenium-webdriver环境方法
  6. ios 中的UI控件学习总结(1)
  7. 原生应用native、Web应用、混合应用hybrid:3者的优缺点解析
  8. Win32环境下的程序崩溃异常定位
  9. 1657: [Usaco2006 Mar]Mooo 奶牛的歌声
  10. Oracle 11g RAC 自动应用PSU补丁简明版
  11. [面试算法题]有序列表删除节点-leetcode学习之旅(4)
  12. 前端开发IDE VSCode + live preview
  13. C语言 &#183; 勾股数
  14. ELK--filebeat nginx模块
  15. org.apache.phoenix.exception.PhoenixIOException: SYSTEM:CATALOG
  16. JDK和CGLIB生成动态代理类的区别
  17. bzoj4358 premu
  18. NDK samples以及部分博客
  19. 安装mysql出现no compatible servers were found
  20. vue--动态路由和get传值

热门文章

  1. 使用插件式开发称重仪表驱动,RS232串口对接各类地磅秤数据实现ERP管理
  2. Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level
  3. App几个可能造成内存泄漏的情况:
  4. ArcGIS实现打点、线路图、色块、自定义弹窗
  5. 使用linux命令直接在网上下载文件,解压,改名
  6. ng-alain创建组件添加路由导航菜单项基础步骤详解
  7. 【随笔记】XR872 Codec 驱动移植和应用程序实例(附芯片调试方法)
  8. 【学习笔记】开源库之 - sigslot (提供该库存在对象拷贝崩溃问题的解决方案)
  9. Oracle ADG环境下的RMAN备份策略
  10. .net core + vue + elementui 删除指定日期段、指定路径下的所有文件