<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- taobao fastdfs依賴 --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <!-- Swagger2 核心依賴 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
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; @Configuration @Import(FdfsClientConfig.class) // 導入FastDFS-Client組件 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解決jmx重複註冊bean的問題 public class FdfsConfiguration { }
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xuecheng.test.fastdfs")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringBoot利用Swagger構建API文檔") .description("使用RestFul風格, 建立人:知了一笑") .termsOfServiceUrl("https://i-beta.cnblogs.com/javaju") .version("version 1.0") .build(); } }
server:
port: 8011
# 分佈式文件系統FDFS配置
fdfs:
soTimeout: 1500 #socket鏈接超時時長
connectTimeout: 600 #鏈接tracker服務器超時時長
reqHost: 192.168.133.131 #nginx訪問地址
reqPort: 80 #nginx訪問端口
thumbImage: #縮略圖生成參數,可選
width: 150
height: 150
trackerList: #TrackerList參數,支持多個,我這裏只有一個,若是有多個在下方加- x.x.x.x:port
- 192.168.133.131:22122
- 192.168.133.131:22122
spring:
application:
name: ware-fast-dfs
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 20MB
import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @Component public class FastDFSClientUtil { private static final Logger LOGGER = LoggerFactory.getLogger(FastDFSClientUtil.class); @Resource private FastFileStorageClient storageClient ; /** * 上傳文件 */ public String upload(MultipartFile multipartFile) throws Exception{ String originalFilename = multipartFile.getOriginalFilename(). substring(multipartFile.getOriginalFilename(). lastIndexOf(".") + 1); StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( multipartFile.getInputStream(), multipartFile.getSize(),originalFilename , null); return storePath.getFullPath() ; } /** * 刪除文件 */ public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { LOGGER.info("fileUrl == >>文件路徑爲空..."); return; } try { StorePath storePath = StorePath.parseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (Exception e) { LOGGER.info(e.getMessage()); } } /** * 下載文件 */ public byte[] downloadFile(String fileUrl){ String group = fileUrl.substring(0, fileUrl.indexOf("/")); String path = fileUrl.substring(fileUrl.indexOf("/") + 1); DownloadByteArray downloadByteArray = new DownloadByteArray(); byte[] bytes = this.storageClient.downloadFile(group, path, downloadByteArray); try { //將文件保存到d盤 FileOutputStream fileOutputStream = new FileOutputStream("d:\\911565.png"); fileOutputStream.write(bytes); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bytes; } }
import com.juju.test.fastdfs.FastDFSClientUtil; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; @RestController public class FileController { @Resource private FastDFSClientUtil fileDfsUtil ; /** * 文件上傳 */ @ApiOperation(value="上傳文件", notes="測試FastDFS文件上傳") @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST) public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){ String result ; try{ String path = fileDfsUtil.upload(file) ; if (!StringUtils.isEmpty(path)){ result = path ; } else { result = "上傳失敗" ; } } catch (Exception e){ e.printStackTrace() ; result = "服務異常" ; } return ResponseEntity.ok(result); } /** * 文件刪除 */ @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET) public ResponseEntity<String> deleteByPath (){ String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ; fileDfsUtil.deleteFile(filePathName); return ResponseEntity.ok("SUCCESS") ; } @ApiOperation(value="下載文件", notes="測試FastDFS文件下載") @GetMapping("/downloadFile") public ResponseEntity<String> downloadFile(){ String g = "group1/M00/00/00/wKiFg13Plc6AVz_hAB6IzmlNPeY944.png"; byte[] bytes = fileDfsUtil.downloadFile(g); System.out.println(bytes); return ResponseEntity.ok("SUCCESS"); } }
@SpringBootApplication
//Swaggerui註解 @EnableSwagger2 public class TestFastDFSApplication { public static void main(String[] args) { SpringApplication.run(TestFastDFSApplication.class,args); } }
返回文件存儲在 FastDFS服務器上的 url 地址html
2019-11-16java