最後の記事では、dockerを使用してfastDFSファイルサーバーをインストールする方法を紹介しました。この記事では、SpringBootの統合を紹介して、fastDFSファイルサーバーへのファイルアップロードを実現します。
1.pom.xmlファイルで依存関係を追加する
<! - 连接fastdfs文件系统- > < 依存> < groupIdを> net.oschina.zcx7878 </ groupIdを> < たartifactId > fastdfs-クライアントのJava </ たartifactId > < バージョン> 1.27.0.0 </ バージョン> </ 依存関係>
2.リソースパッケージの下に構成ファイルfdfs_client.confを作成します
tracker_serverの値ipは、ファイルサーバーのIPです。
connect_timeout = 30
network_timeout = 60
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key =
tracker_server = ip:22122
3. FastDFSConfig.javaを作成してfdfs_client.conf構成ファイルをロードします
import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.TrackerClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; / ** * fastDFS文上传的配置 * / @Configuration public class FastDFSConfig { private final Logger log = LoggerFactory.getLogger(this .getClass()); @Value( "classpath:fdfs_client.conf" ) private Resource ccs; @Bean public TrackerClient initClient(){ try { ClientGlobal.init(ccs.getFilename()); return new TrackerClient(); } catch (Exception e){ log .info( "FastDFSがクライアントの作成に失敗しました" ); return null ; } } }
4.ファイルアップロード用のCotrollerを作成します。返されたアクセスパスのIPはファイルサーバーのIPです
import org.csource.common.NameValuePair; import org.csource.fastdfs。* ; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping( "/ファイル" ) パブリック クラスFileController { プライベートロガーログ= LoggerFactory.getLogger(FileController。クラス)。 @Autowired プライベートTrackerClient trackerClient; @PostMapping( "/ upload" ) public String uploadFile(@RequestParam( "file")MultipartFile file)throws Exception { if(file == null ){ throw new RuntimeException( "文件不可是空" ); } // 1.获取文件的完整名称 String filename =file.getOriginalFilename(); if (StringUtils.isEmpty(filename)){ throw new RuntimeException( "File does not exist" ); } // 2.ファイルの拡張名を取得します String extName = filename.substring(filename.lastIndexOf( "。 ")+ 1 ); log.info( "完全なファイル名: "+ファイル名+"ファイル拡張子: "+ extName); NameValuePair [] metaList = new NameValuePair [1 ]; metaList [ 0] = new NameValuePair(" fileName " 、filename); // 3. trackerServer TrackerServer trackerServer =を作成しますtrackerClient.getConnection(); // 4. StorageServer参照を作成し、値はnull StorageServer storageServer = null ; // 5. StorageClientオブジェクトを作成します。2つのパラメータが必要ですTrackerServerオブジェクト、StorageServer参照 StorageClient storageClient = new StorageClient(trackerServer 、storageServer); // 6. StorageClientオブジェクトを使用して画像をアップロードします。 String [] strings = storageClient.upload_file(file.getBytes()、extName、metaList); return "http:// ip:8888 /" + strings [0] + "/" + strings [1 ]; }
5.この時点で、postmanを使用してファイルアップロードインターフェイスを呼び出し、返されたパスに従ってブラウザでアクセスすると、アップロードしたファイルに正常にアクセスできます。