|
吾爱游客
发表于 2019-5-13 16:03
1、申 请 I D:lovelyhedong
2、个人邮箱:755964539@qq.com
3、原创技术文章:linux安装fastdfs,并且java上传下载对象文件
|
Linux------FASTDFS安装
cd /usr/local/
mkdir fdfs
cd fdfs
wget https://github.com/happyfish100/fastdfs/archive/master.zip -O ./fastdfs_server.zip
wget https://github.com/happyfish100/libfastcommon/archive/master.zip -O ./libfastcommon.zip
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip -O./fastdfs-nginx-module.zip
unzip fastdfs-nginx-module.zip
unzip fastdfs_server.zip
unzip libfastcommon.zip
cd libfastcommon-master
./make.sh
./make.sh install
cd ..
cd fastdfs-master
./make.sh
./ make.sh install
cd /etc/fdfs/
cp tracker.conf.sample tracker.conf
cp storage.conf sample storage.conf
cp client.conf.sample client.conf
vim tracker.conf
修改22行:
base_path=/home/yuqing/fastdfs/
修改22行后:
base_path=/home/fastdfs/
修改261行:
http.server_port=8888
修改261行后:
http.server_port=80
cd /home/
mkdir fastdfs
cd fastdfs
mkdir storage
cd /etc/fdfs/
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
vim storage.conf
修改41行:
base_path=/home/yuqing/fastdfs/
修改41行后:
base_path=/home/fastdfs/storage
修改110行:
store_path1=/home/yuqing/fastdfs2
修改110行后:
store_path0=/home/fastdfs/storage
修改119行:
tracker_server=192.168.1.121:22122
修改119行后:
tracker_server=本机服务器ip(或者虚拟机ip):22122
修改285行:
http.server_port=8888
修改285行后:
http.server_port=80
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
cd /usr/local/fdfs/fastdfs-nginx-module-master/src/
vim mod_fastdfs.conf
修改53行:
url_have_group_name = false
修改53行后:
url_have_group_name = true
修改62行:
store_path1=/home/yuqing/fastdfs1
修改62行后:
store_path0=/home/fastdfs/storage
cp ./mod_fastdfs.conf /etc/fdfs/
cd /usr/local/fdfs/fastdfs-master/conf/
cp ./{anti-steal.jpg,http.conf,mime.types} /etc/fdfs
netstat -tunlp|grep fdfs
kill -9 xxxx -----(22122端口)
kill -9 xxxx -----(23000端口)
cd /usr/
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
wget http://nginx.org/download/nginx-1.15.12.tar.gz -O ./nginx.tar.gz
tar -zxvf nginx.tar.gz
cd nginx-1.15.12
./configure
make
make install
cd /usr/local/nginx/conf/
vim nginx.conf
在server添加:
location /group1/M00/ {
root /home/fastdfs/storage/data;
}
location ~/group([0-9])/M00 {
ngx_fastdfs_module;
}
cd /usr/nginx-1.15.12/
./configure --add-module=/usr/local/fdfs/fastdfs-nginx-module-master/src (添加fastdfs-nginx模块)
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf (启动tracker)
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf (启动storage)
/usr/local/nginx/sbin/nginx (启动nginx)
以下是java测试上传下载删除代码
工具类代码
package com.test;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import java.io.BufferedOutputStream;
import java.io.IOException;
/**
* FastDFS工具类【实现文件上传、下载、删除、查询】
* @author lovelyhedong
*/
public class FastDFSUtil{
private static TrackerClient trackerClient = null;
private static TrackerServer trackerServer = null;
private static StorageServer storageServer = null;
private static StorageClient1 storageClient = null;
static {
try {
ClientGlobal.init("fdfs_client.conf");
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public static String uploadFile(String fileName, String extName, NameValuePair[] metas) {
String result=null;
try {
result = storageClient.upload_file1(fileName, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/**
* 上传文件,传fileName
* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
* @return null为失败
*/
public static String uploadFile(String fileName) {
return uploadFile(fileName, null, null);
}
/**
*
* @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg
* @param extName 文件的扩展名 如 txt jpg等
* @return null为失败
*/
public static String uploadFile(String fileName, String extName) {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public static String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {
String result=null;
try {
result = storageClient.upload_file1(fileContent, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/**
* 上传文件
* @param fileContent 文件的字节数组
* @return null为失败
* @throws Exception
*/
public static String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
/**
* 上传文件
* @param fileContent 文件的字节数组
* @param extName 文件的扩展名 如 txt jpg png 等
* @return null为失败
*/
public static String uploadFile(byte[] fileContent, String extName) {
return uploadFile(fileContent, extName, null);
}
/**
* 文件下载到磁盘
* @param path 图片路径
* @param output 输出流 中包含要输出到磁盘的路径
* @return -1失败,0成功
*/
public static int download_file(String path,BufferedOutputStream output) {
int result=-1;
try {
byte[] b = storageClient.download_file1(path);
try{
if(b != null){
output.write(b);
result=0;
}
}catch (Exception e){} //用户可能取消了下载
finally {
if (output != null){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 获取文件数组
* @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return
*/
public static byte[] download_bytes(String path) {
byte[] b=null;
try {
b = storageClient.download_file1(path);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return b;
}
/**
* 删除文件
* @param group 组名 如:group1
* @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return -1失败,0成功
*/
public static Integer delete_file(String group ,String storagePath){
int result=-1;
try {
result = storageClient.delete_file(group, storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param storagePath 文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return -1失败,0成功
* @throws IOException
* @throws Exception
*/
public static Integer delete_file(String storagePath){
int result=-1;
try {
result = storageClient.delete_file1(storagePath);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/**
* 获取远程服务器文件资源信息
* @param groupName 文件组名 如:group1
* @param remoteFileName M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
* @return
*/
public static FileInfo getFile(String groupName,String remoteFileName){
try {
return storageClient.get_file_info(groupName, remoteFileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
测试代码
package com.test.test;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerGroup;
import org.csource.fastdfs.TrackerServer;
import com.test.FastDFSUtil;
import com.test.FastDFSUtils;
import cn.hutool.core.date.DateUtil;
public class MainTest {
public static void main(String[] args) throws IOException, MyException {
ClientGlobal.init("fdfs_client.conf");
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
TrackerGroup group= ClientGlobal.g_tracker_group;
TrackerClient trackerClient=new TrackerClient(group);
TrackerServer trackerServer=trackerClient.getConnection();
StorageServer storageServer=trackerClient.getStoreStorage(trackerServer);
// StorageClient storageClient=new StorageClient(trackerServer, storageServer);
StorageClient1 storageClient1=new StorageClient1(trackerServer, storageServer);
// Integer delete_file = FastDFSUtil.delete_file("group1/M00/00/00/rBsAEFzAL0iASL-eAASpeEkOW6c968.jpg");
// System.out.println("删除:"+delete_file);
// String uploadFile = FastDFSUtil.uploadFile("C:\\Users\\Administrator\\Desktop\\110.png");
// //group1/M00/00/00/rBsAEFy-wYiAV32HAAfbmiK0IQo766.png
// System.out.println("文件路径地址:"+uploadFile);
// System.out.println("ac929c28562b9253a0726d86f28dc393".length());
}
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|