• pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
  • 配置类

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {

}

  • 包装类
@Component
public class FastDFSClientWrapper { private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class); @Autowired
private FastFileStorageClient fastFileStorageClient; /**
* 文件上传
* 最后返回fastDFS中的文件名称;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc
*
* @param bytes 文件字节
* @param fileSize 文件大小
* @param extension 文件扩展名
* @return fastDfs路径
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
logger.info(storePath.getGroup() + "==" + storePath.getPath() + "======" + storePath.getFullPath());
return storePath.getFullPath();
} /**
* 下载文件
* 返回文件字节流大小
* @param fileUrl 文件URL
* @return 文件字节
* @throws IOException
*/
public byte[] downloadFile(String group,String path) throws IOException {
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
} }
  • html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 th:inlines="text">文件上传</h1>
<form action="upload" method="post" enctype="multipart/form-data">
<p>选择文件: <input type="file" name="file"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
  • application.yml
#fastdfs 配置
fdfs:
so-timeout: 1500
connect-timeout: 600
thumb-image:
width: 150
height: 150
tracker-list:
- 192.168.180.104:22122
spring:
thymeleaf:
prefix: classpath:/templates/
  • web层
@RestController
public class FdfsController { @Autowired
private FastDFSClientWrapper fastDFSClientWrapper; private final Logger logger = LoggerFactory.getLogger(FdfsController.class); @RequestMapping("file")
public String file(){
return "/fileUploade";
} @PostMapping("/upload")
public void upload(MultipartFile file) throws Exception {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
} catch (IOException e) {
logger.error("获取文件错误");
e.printStackTrace();
}
//获取源文件名称
String originalFileName = file.getOriginalFilename();
//获取文件后缀--.doc
String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
String fileName = file.getName();
//获取文件大小
long fileSize = file.getSize();
System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
String string = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
//group1/M00/00/00/wKi0aF3I4dOAIhAXAAAG7e1-evI891.txt
System.out.println(string);
} @GetMapping("/downloadFile")
public void downloadFile(String group,String path,String fileName,HttpServletResponse response) throws Exception {
byte[] bytes = fastDFSClientWrapper.downloadFile(group, path);
//设置相应类型application/octet-stream (注:applicatoin/octet-stream 为通用,一些其它的类型苹果浏览器下载内容可能为空)
response.reset();
response.setContentType("applicatoin/octet-stream");
//设置头信息 Content-Disposition为属性名 附件形式打开下载文件 指定名称 为 设定的fileName
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 写入到流
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close(); } }

最新文章

  1. php Your system does not support any of these drivers: gmagick,imagick,gd2
  2. 东哥读书小记 之 《MacTalk人生元编程》
  3. Java基础之打印万年历
  4. Java注解的使用
  5. TCP/UDP端口列表
  6. IRunningObjectTable接口
  7. Windows PowerShell ISE
  8. SGU131--NYOJ435
  9. LESS使用方法简介(装逼神器)
  10. Equal 和==比较
  11. unity插件开发——AssetDatabase
  12. hdu 2825 aC自动机+状压dp
  13. cinder-volume报错vmdk2 is reporting problems, not sending heartbeat. Service will appear &quot;down&quot;.
  14. 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删
  15. C++怎么实现线程安全
  16. Navicat工具怎么连接oracle数据库
  17. python---cookie模拟登陆和模拟session原理
  18. 解决JBoss只能通过localhost访问不能通过IP的问题
  19. 使用c语言调用python小结
  20. 【JQuery】jQuery中的常用方法小结

热门文章

  1. Delphi XE2 之 FireMonkey 入门(4) - 控件天生可做容器
  2. list数据的存储
  3. sqoop应用
  4. 【AndroidFramework】ATV9遥控器红外模式下,机顶盒在假待机阶段会响应遥控器语音键
  5. Python笔记(二十八)_魔法方法_迭代器
  6. 应用安全_WTS-WAF绕过
  7. ToolProvider.getSystemJavaCompiler()方法空指针的排坑
  8. 10、应用机器学习的建议(Advice for Applying Machine Learning)
  9. React.memo
  10. 08-js流程控制、循环、元素操作