1 代码
1.1 依赖文件:
<!--fdfs-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.4</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
这里的<exclusions>标签后面错误部分解释
1.2 配置文件:application.properties:
fdfs.so-timeout=1501
fdfs.connect-timeout=601
##缩略图生成参数
fdfs.thumb-image.width=150
fdfs.thumb-image.height=150
##TrackerList参数,支持多个
fdfs.tracker-list=10.19.151.142:22122
fdfs.tracker-list=10.19.151.143:22122
fdfs.tracker-list=10.19.151.144:22122
fdfs.return-url = http://10.19.151.142:9999/
fdfs.pool.max-total=200
fdfs.pool.max-wait-millis=5000`
1.3 配置类文件 FdfsConfiguration.java
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* @description
* @Author cxc
* @Date 2020/4/2 11:34
* @Version 1.0
*/
@Configuration
@Import(FdfsClientConfig.class)//注解,就可以拥有带有连接池的FastDFS Java客户端了
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfiguration {
}
1.4 工具类文件 FastDFSUploadUtils.java
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* @description
* @Author cxc
* @Date 2020/4/2 11:36
* @Version 1.0
*/
@Component
public class FastDFSUploadUtils {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
@Value("${fdfs.return-url}")
private String returnUrl;
/**
* 上传并返回全访问路径
* @param file
* @return
* @throws IOException
*
*/
public String uploadFile(MultipartFile file) throws IOException {
//StorePath storePath = storageClient.uploadFile((InputStream)file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
StorePath storePath = storageClient.uploadImageAndCrtThumbImage((InputStream) file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
System.out.println(storePath.getFullPath());
System.out.println(getResAccessUrl(storePath));
return getResAccessUrl(storePath);
}
/**
* 删除
*
* @param fileUrl
*/
public void deleteFile(String fileUrl) {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
/**
* 下载
*
* @param fileUrl
* @return
* @throws IOException
*/
public byte[] downloadFile(String fileUrl) throws IOException {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
String group = storePath.getGroup();
String path = storePath.getPath();
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
/**
* 根据半截路径获取可访问的全路径
*
* @param filePath
* @return
*/
private String getResAccessUrl(String filePath) {
return returnUrl + "/" + filePath;
}
/**
* 封装文件完整URL地址
*
* @param storePath
* @return
*/
private String getResAccessUrl(StorePath storePath) {
return returnUrl + "/" + storePath.getFullPath();
}
}
1.5 controller文件: FdfsController.java
import com.oss.messageService.util.FastDFSUploadUtils;
import com.oss.messageService.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
/**
* @description 文件上传和下载
* @Author cxc
* @Date 2020/4/2 15:28
* @Version 1.0
*/
@Api(tags = "文件上传")
@RestController
@Slf4j
@RequestMapping("/FdfsController")
public class FdfsController {
@Autowired
private FastDFSUploadUtils fastDFSClientUtils;
@ApiOperation(value = "文件上传", notes = "文件上传")
@PostMapping("/fdfs_upload")
public ResponseResult fdfsUpload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseResult.fail("请选择文件");
}
try {
String fileUrl = fastDFSClientUtils.uploadFile(file);
return ResponseResult.success(fileUrl);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseResult.fail("上传失败");
}
@ApiOperation(value = "删除文件", notes = "删除文件")
@PostMapping("/fdfs_delete")
public ResponseResult delete(@RequestParam("fileUrl") String fileUrl) {
try {
fastDFSClientUtils.deleteFile(fileUrl);
} catch (Exception e) {
e.printStackTrace();
return ResponseResult.fail(e.toString());
}
return ResponseResult.success("删除成功");
}
@ApiOperation(value = "下载文件", notes = "下载文件")
@GetMapping("/fdfs_download")
public void download(@RequestParam("fileUrl") String fileUrl, HttpServletResponse response) throws IOException {
byte[] bytes = fastDFSClientUtils.downloadFile(fileUrl);
// 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.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 (IOException e) {
e.printStackTrace();
}
}
}
}
2 错误
过程中遇到个错误
2.1 错误:
IDEA 报错 LoggerFactory is not a Logback LoggerContext but Logback is on the classpath,如何排除依赖冲突
2.2 分析原因:引入的jar包和项目本来的jar包发生了冲突
2.3 解决:见下连接
https://blog.csdn.net/zhanggonglalala/article/details/88953345?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
3 参考
搭建过程参考了csdn里面一篇博客,附上csdn连接
https://blog.csdn.net/hanxing20112011/article/details/84439486