赞
踩
wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz"
wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz"
wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz"
Nginx
解压后放到/usr/local
./make.sh && ./make.sh install
解压后放到/usr/local
./make.sh && ./make.sh install
cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker
mkdir -p /fastdfs/storage
vi /etc/fdfs/tracker.conf
base_path = /fastdfs/tracker
启动tracker:/etc/init.d/fdfs_trackerd start
停止tracker:/etc/init.d/fdfs_trackerd stop
vi /etc/fdfs/storage.conf
base_path = /fastdfs/storage
store_path0 = /fastdfs/storage
tracker_server = tracker的IP:22122
http.server_port = 80
启动storage:/etc/init.d/fdfs_storaged start
vi /etc/fdfs/client.conf
base_path = /fastdfs/tracker
tracker_server = tracker的IP:22122
#设置开机启动:vi /etc/rc.d/rc.local
/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
fastdfs-nginx-module解压后放到/usr/local
nginx添加模块(解压目录,不是nginx主目录)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
fastdfs-nginx-module配置文件修改并将config文件中的/usr/local替换成/usr:cd /usr/local/fastdfs-nginx-module-1.22/src/
vi config
nginx添加模块:./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src/
nginx编译安装:make && make install
fastdfs-nginx-module配置文件复制
cp /usr/local/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
并修改配置文件
vi /etc/fdfs/mod_fastdfs.conf
connect_timeout=10
tracker_server=IP:22122
url_have_group_name=true
store_path0=/fastdfs/storage
进入fastdfd的conf目录, 将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下
cp http.conf mime.types /etc/fdfs/
创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
修改nginx.conf
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name IP;
location ~/group([0-9])/M00 {
root /fastdfs/storage/data;
ngx_fastdfs_module;
}
}
启动nginx
根据路径访问文件
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
fdfs.so-timeout = 1501
fdfs.connect-timeout = 601
fdfs.thumb-image.width= 150
fdfs.thumb-image.height= 150
fdfs.tracker-list=IP:22122
fdfs.web-server-url=IP:23001/
更改文件大小上限
spring.servlet.multipart.max-file-size=30MB
spring.servlet.multipart.max-request-size=30MB
@Component public class FastDFSClient { @Autowired private FastFileStorageClient storageClient; @Autowired private FdfsWebServer fdfsWebServer; /** * 上传文件 * @param file 文件对象 * @return 文件访问地址 * @throws IOException */ public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); return fdfsWebServer.getWebServerUrl() + storePath.getFullPath();//完整url } /** * 下载文件 * @param fileUrl 文件url * @return */ public byte[] download(String fileUrl) { String group = fileUrl.substring(0, fileUrl.indexOf("/")); String path = fileUrl.substring(fileUrl.indexOf("/") + 1); byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray()); return bytes; } /** * 删除文件 * @param fileUrl 文件访问地址 * @return */ public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { return; } try { StorePath storePath = StorePath.parseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (FdfsUnsupportStorePathException e) { logger.warn(e.getMessage()); } } }
@RestController public class FastDFSController { @Autowired private FastDFSClient fastDFSClient; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { byte[] bytes = file.getBytes(); String originalFileName = file.getOriginalFilename(); String extension = FilenameUtils.getExtension(file.getOriginalFilename()); String fileName = file.getName(); long fileSize = file.getSize(); System.out.println(originalFileName + "==========" + fileName + "===========" + fileSize + "===============" + extension + "===========" + bytes.length); return fastDFSClient.uploadFile(file); } @PostMapping("/download") public void downloadFile(@RequestParam("fileUrl")String fileUrl, HttpServletResponse response) throws IOException { byte[] bytes = fastDFSClient.download(fileUrl); // 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式 response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("1.jpg", "UTF-8")); response.setCharacterEncoding("UTF-8"); ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } @GetMapping("/deleteFile") public String deleteFile(@RequestParam("fileUrl")String fileUrl) { fastDFSClient.deleteFile(fileUrl); return "success"; } }
文件不能擅自修改名称 或 自己直接上传
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。