FastDFS使用教程
FastDFS下载必要安装包libfastcommonfastdfstracker配置storage配置client配置(开发不需要)fastdfs-nginx-module 和
SEO靠我 Nginx使用pomapplicationservicecontroller
FastDFS
下载必要安装包
wget -c "https://github.com/happyfish100/fastdfSEO靠我s/archive/V6.06.tar.gz"
wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gzSEO靠我"
wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz"
Nginx
libfastcommSEO靠我on
解压后放到/usr/local ./make.sh && ./make.sh install
fastdfs
解压后放到/usr/local ./make.sh && ./make.sh instalSEO靠我l
cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
SEO靠我
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker
mkdir -p /fas
SEO靠我tdfs/storage
tracker配置
vi /etc/fdfs/tracker.conf
base_path = /fastdfs/tracker
启动tracker:/etc/iSEO靠我nit.d/fdfs_trackerd start
停止tracker:/etc/init.d/fdfs_trackerd stop
storage配置
vi /etc/fdfs/storage.conf
b
SEO靠我ase_path = /fastdfs/storage
store_path0 = /fastdfs/storage
tracker_server = tracker的
SEO靠我IP:22122
http.server_port = 80
启动storage:/etc/init.d/fdfs_storaged start
client配置(开发不需要)
vi /etSEO靠我c/fdfs/client.conf
base_path = /fastdfs/tracker
tracker_server = tracker的IP:22122
#设置开机启动:vi /SEO靠我etc/rc.d/rc.local
/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
fastdfs-nginSEO靠我x-module 和 Nginx
fastdfs-nginx-module解压后放到/usr/local
nginx添加模块(解压目录,不是nginx主目录)
./configure --prefix=/u
SEO靠我sr/local/nginx --with-http_stub_status_module
fastdfs-nginx-module配置文件修改并将config文件中的/usr/local替换成/usrSEO靠我:cd /usr/local/fastdfs-nginx-module-1.22/src/ vi config
nginx添加模块:./configure --add-module=/usr/localSEO靠我/fastdfs-nginx-module-1.22/src/
nginx编译安装:make && make install
fastdfs-nginx-module配置文件复制
cp /usr/localSEO靠我/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
并修改配置文件
vi /etc/fdfs/mod_fastdfs.confconnec
SEO靠我t_timeout=10
tracker_server=IP:22122
url_have_group_name=true
store_path0=/f
SEO靠我astdfs/storage
进入fastdfd的conf目录, 将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下
cp http.conf mime.types /et
SEO靠我c/fdfs/
创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /fastdfs/storage/data/ /fastdfs/sto
SEO靠我rage/data/M00
修改nginx.conf
vi /usr/local/nginx/conf/nginx.confserver {listen 80;server_name IP;locatio
SEO靠我n ~/group([0-9])/M00 {root /fastdfs/storage/data;ngx_fastdfs_module;}
}
启动nginx
根据路径访问文件
使用
pom
SEO靠我 <dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><vers
SEO靠我ion>1.26.7</version>
</dependency>
application
fdfs.so-timeout = 1501
SEO靠我 fdfs.connect-timeout = 601
fdfs.thumb-image.width= 150
fdfs.thumb-image.height= 1
SEO靠我50
fdfs.tracker-list=IP:22122
fdfs.web-server-url=IP:23001/
service
SEO靠我 @Component
public class FastDFSClient {@Autowiredprivate FastFileStorageClient storageClien
SEO靠我t;@Autowiredprivate FdfsWebServer fdfsWebServer;/*** 上传文件* @param file 文件对象* @return 文件访问地址* @throws
SEO靠我 IOException*/public String uploadFile(MultipartFile file) throws IOException {StorePath storePath =
SEO靠我 storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.get
SEO靠我OriginalFilename()),null);return fdfsWebServer.getWebServerUrl() + storePath.getFullPath();//完整url}/
SEO靠我*** 下载文件* @param fileUrl 文件url* @return*/public byte[] download(String fileUrl) {String group = file
SEO靠我Url.substring(0, fileUrl.indexOf("/"));String path = fileUrl.substring(fileUrl.indexOf("/") + 1);byt
SEO靠我e[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());return bytes;}/*** 删除文件
SEO靠我* @param fileUrl 文件访问地址* @return*/public void deleteFile(String fileUrl) {if (StringUtils.isEmpty(fi
SEO靠我leUrl)) {return;}try {StorePath storePath = StorePath.parseFromUrl(fileUrl);storageClient.deleteFile
SEO靠我(storePath.getGroup(), storePath.getPath());} catch (FdfsUnsupportStorePathException e) {logger.warn
SEO靠我(e.getMessage());}}
}
controller
@RestController
public class FastD
SEO靠我FSController {@Autowiredprivate FastDFSClient fastDFSClient;@PostMapping("/upload")public String upl
SEO靠我oadFile(@RequestParam("file") MultipartFile file) throws IOException {byte[] bytes = file.getBytes()
SEO靠我;String originalFileName = file.getOriginalFilename();String extension = FilenameUtils.getExtension(
SEO靠我file.getOriginalFilename());String fileName = file.getName();long fileSize = file.getSize();System.o
SEO靠我ut.println(originalFileName + "==========" + fileName + "===========" + fileSize + "==============="
SEO靠我 + extension + "===========" + bytes.length);return fastDFSClient.uploadFile(file);}@PostMapping("/d
SEO靠我ownload")public void downloadFile(@RequestParam("fileUrl")String fileUrl, HttpServletResponse respon
SEO靠我se) throws IOException {byte[] bytes = fastDFSClient.download(fileUrl);// 这里只是为了整合fastdfs,所以写死了文件格式。
SEO靠我需要在上传的时候保存文件名。下载的时候使用对应的格式response.setHeader("Content-disposition", "attachment;filename=" + URLEnco
SEO靠我der.encode("1.jpg", "UTF-8"));response.setCharacterEncoding("UTF-8");ServletOutputStream outputStrea
SEO靠我m = null;try {outputStream = response.getOutputStream();outputStream.write(bytes);} catch (IOExcepti
SEO靠我on e) {e.printStackTrace();} finally {try {outputStream.flush();outputStream.close();} catch (Except
SEO靠我ion e) {e.printStackTrace();}}}@GetMapping("/deleteFile")public String deleteFile(@RequestParam("fil
SEO靠我eUrl")String fileUrl) {fastDFSClient.deleteFile(fileUrl);return "success";}
}