链接:https://pan.baidu.com/s/1t4uAHgmDFi0W6fs8_1ZxeA 提取码:lakw安装步骤 环境安装
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y安装libfastcommon
将libfastcommon、fastdfs和fastdfs-nginx解压文件放到 home/fdfs下 cd libfastcommon-master/编译安装
./make.sh && ./make.sh install
若提示权限不够可使用chmod u+x *.sh设置权限
控制台出现以下信息表示编译并且安装成功mkdir -p /usr/lib64 mkdir -p /usr/lib mkdir -p /usr/include/fastcommon安装fastdfs
cd fastdfs-master/编译安装
./make.sh && ./make.sh install
若提示权限不够可使用chmod u+x *.sh设置权限
控制台出现以下信息表示编译并且安装成功mkdir -p /usr/bin mkdir -p /etc/fdfs mkdir -p /usr/lib64 mkdir -p /usr/lib配置tracker服务 将fastdfs配置文件全部复制到etc/fdfs/目录下
cp -r /home/fdfs/fastdfs-master/conf
@Override
public String uploadFile(MultipartFile file) {
//获取文件后缀位置
int index = file.getOriginalFilename().lastIndexOf(".");
//文件后缀名
String fileSuffix = file.getOriginalFilename().substring(index + 1);
try {
//将文件上传到fastDFS
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileSuffix, null);
String filePath = storePath.getFullPath(); //获取上传文件路径
logger.info("文件路径为:" + filePath);
return filePath;
}catch (Exception e){
e.printStackTrace();
logger.info("上传文件失败");
}
return null;
}
文件下载
@RequestMapping("/download")
public Map downloadFile(String fileName, HttpServletResponse response) {
fileService.downloadFile(fileName, response);
return null;
}
@Override
public void downloadFile(String filePath, HttpServletResponse response) {
String group = filePath.substring(0, filePath.indexOf("/"));
String path = filePath.substring(filePath.indexOf("/") + 1);
String fileSuffix = filePath.substring(filePath.lastIndexOf(".") + 1);
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
ServletOutputStream outputStream = null;
try{
//下载文件
byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName.concat(fileSuffix), "UTF-8"));
response.setCharacterEncoding("UTF-8");
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
logger.info("下载文件失败");
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件删除
@Override public MapdeleteFile(Map context) { Map requestBody = ContextUtil.getRequestBody(context); try { String filePath = StringUtil.getAsString(requestBody.get("filePath")); if(filePath == null || "".equals(filePath)){ throw new Exception("文件路径不能为空"); } fastFileStorageClient.deleteFile(filePath); }catch (Exception e){ e.printStackTrace(); logger.info("删除文件失败"); } return null; }



