赞
踩
格式: f d f s _ u p l o a d _ f i l e < c o n f i g _ f i l e > < l o c a l _ f i l e n a m e > [ s t o r a g e _ i p : p o r t ] [ s t o r e _ p a t h _ i n d e x ] \textcolor{Cyan}{fdfs\_upload\_file\ <config\_file>\ <local\_filename>\ [storage\_ip:port] [store\_path\_index]} fdfs_upload_file <config_file> <local_filename> [storage_ip:port][store_path_index]
参数含义:
- <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
- <local_filename> :本地文件路径(./文件名)
- [storage_ip:port] :(可选参数)
- [store_path_index] :(可选参数)
格式: f d f s _ d o w n l o a d _ f i l e < c o n f i g _ f i l e > < f i l e _ i d > [ l o c a l _ f i l e n a m e ] [ < d o w n l o a d _ o f f s e t > < d o w n l o a d _ b y t e s > ] \textcolor{Cyan}{fdfs\_download\_file\ <config\_file>\ <file\_id>\ [local\_filename] [<download\_offset>\ <download\_bytes>]} fdfs_download_file <config_file> <file_id> [local_filename][<download_offset> <download_bytes>]
参数含义:
- <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
- <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)
- [local_filename] :(可选参数)文件下载地址
- <download_offset> :(可选参数)文件下载开始时间
- <download_bytes> :(可选参数)文件下载的字节数
格式: f d f s _ f i l e _ i n f o < c o n f i g _ f i l e > < f i l e _ i d > \textcolor{Cyan}{fdfs\_file\_info\ <config\_file>\ <file\_id>} fdfs_file_info <config_file> <file_id>
参数含义:
- <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
- <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)
格式: f d f s _ d e l e t e _ f i l e < c o n f i g _ f i l e > < f i l e _ i d > \textcolor{Cyan}{fdfs\_delete\_file\ <config\_file>\ <file\_id>} fdfs_delete_file <config_file> <file_id>
参数含义:
- <config_file> :配置文件路径(/etc/fdfs/client.conf:上面配置的路径)
- <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名(文件上传返回的id)
<dependencies> <!-- 起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Junit 起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- springmvc 起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- thymeleaf起步依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- MyBatisPlus 起步依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- fastdfs 客户端 --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.5</version> </dependency> </dependencies> <!-- 阿里云仓库配置 --> <repositories> <repository> <id>maven-ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public//</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> </repositories>
在applacation.yml配置fastdfs地址
# 日志格式 logging: pattern: console: '[%yellow(%date{yyyy-MM-dd HH:mm:ss.SSS}) %highlight(%-5level)] [%cyan(%X{traceid}) %magenta(%-15thread)] [%green(%-50logger){50}] : %.4000m%n' ################################ 分布式文件系统的配置 ################################ fdfs: # 超时时间 connect-timeout: 600 # 连接时间 so-timeout: 1500 #缩略图生成参数 thumb-image: width: 150 height: 150 # 服务器地址 tracker-list: - 192.168.xx.xx:22122
controller编写、上传、下载
- fastFileStorageClient对象
uploadFile:上传
- 参数一:传输文件内容的输入流;
- 参数二:文件的size;
- 参数三:文件扩展名;
- 参数四:描述文件的元数据;
注意 \textcolor{red}{注意} 注意:返回值:上传文件在存储节点的唯一标识(卷名 + 文件名)
@Controller @RequestMapping("file") public class FileController { //fastdfs存储节点的客户端对象 @Autowired private FastFileStorageClient fastFileStorageClient; @Autowired private IFiletestService fileService; /** * 文件上传 * @param files * @return * @throws Exception */ @RequestMapping("/fileUpload") public String upload(MultipartFile files[]) throws Exception { for (MultipartFile file : files) { /** * 文件上传: * 参数一:传输文件内容的输入流; * 参数二:文件的size; * 参数三:文件扩展名; * 参数四:描述文件的元数据; * 返回值:上传文件在存储节点的唯一标识(卷名 + 文件名) */ StorePath storePath = fastFileStorageClient.uploadFile( file.getInputStream(), file.getSize(), file.getOriginalFilename() .substring(file .getOriginalFilename() .lastIndexOf(".")+1), null); // 保存文件信息 fileService.uploadMassage(Filetest.builder() .name(file.getOriginalFilename()) .tag(storePath.getGroup()) .url(storePath.getPath()) .build()); } return "redirect:/file/showFiles"; } }
downloadFile:下载
- 参数一:文件处于存储节点的卷名;
- 参数二:文件在存储节点的文件名;
- 参数三:下载的回调函数;
注意 1 \textcolor{red}{注意1} 注意1:返回值:文件内容的字节数组;
注意 2 \textcolor{red}{注意2} 注意2:乱码问题:
// 设置响应头 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
- 1
- 2
@Controller @RequestMapping("file") public class FileController { //fastdfs存储节点的客户端对象 @Autowired private FastFileStorageClient fastFileStorageClient; @Autowired private IFiletestService fileService; // 文件下载 @RequestMapping("/download") public void fileDown(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException { // 设置响应头 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"UTF-8")); Filetest file = fileService.getFileByName(fileName); /** * 下载文件: * 参数一:文件处于存储节点的卷名; * 参数二:文件在存储节点的文件名; * 参数三:下载的回调函数; * 返回值:文件内容的字节数组 */ byte[] bytes = fastFileStorageClient.downloadFile( file.getTag(), file.getUrl(), new DownloadByteArray()); // 获取字节输出流 ServletOutputStream os = response.getOutputStream(); // 使用输出流写出文件 os.write(bytes); os.flush(); os.close(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。