記事のリンク:
はじめに:
私たちは、最初の2 FastDFSは、基本モジュールとFastDFS + nginxのた画像サーバまあ以来建て構造は、実際にアップロード写真の友人を操作するJavaコードと組み合わせてプロジェクトを開始することの時間を完了しています。この記事では、ファイルのアップロード機能を完了するために、springboot統合FastDFS_Clientを説明します。
本体:
オンライン今基本的にFastDFS_Clientへの3つの方法がプロジェクトに統合しました:
最初のプロジェクトに導入されたダウンロードfastdfs-クライアントのjava-マスターソースパッケージの包装にあります
第二は、直接POMファイルをMavenの依存座標のオンラインシェアと結合されています
まず、ここで説明するのは、依存関係を追加し、オンラインMavenの直接依存座標であります
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
第二に、プロファイルapplication.propertiesを追加
server.port=8989
# fastDFS 配置
fdfs.so-timeout=1501
fdfs.connect-timeout=601
fdfs.thumb-image.width=150
fdfs.thumb-image.height=150
fdfs.web-server-url=192.168.xxx.x:xxxxx/ //这个会追加在返回地址的前面 ip+nginx的端口
fdfs.tracker-list[0]=192.168.xxx.x:22122
三、FastDFSツール
import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
@Slf4j
@Component
public class FastDFSUtils {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
/**
* 上传文件
*
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
return getResAccessUrl(storePath);
}
/**
* 上传文件
*
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return getResAccessUrl(storePath);
}
/**
* 将一段字符串生成一个文件上传
*
* @param content 文件内容
* @param fileExtension
* @return
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return getResAccessUrl(storePath);
}
// 封装图片完整URL地址
private String getResAccessUrl(StorePath storePath) {
String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
return fileUrl;
}
/**
* 下载文件
*
* @param fileUrl 文件url
* @return
*/
public byte[] download(String fileUrl) {
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());
return bytes;
}
/**
* 删除文件
*
* @param fileUrl 文件访问地址
* @return
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
log.warn(e.getMessage());
}
}
}
ロード構成クラスFastDFS
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)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
}
コントローラクラス
@RestController
@RequestMapping("/opinion")
public class FastDFSController {
@Autowired
private FastDFSClient fdfsClient;
/**
* 文件上传
* @param file
* @return
* @throws Exception
*/
@RequestMapping("/upload1")
public Map<String,Object> upload(@RequestParam("pic")MultipartFile file) throws Exception{
String url = fdfsClient.uploadFile(file);
Map<String,Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "上传成功");
result.put("url", url);
return result;
}
}
第四に、郵便配達テスト
ここで、コントローラでフォームを返し、コードは、私は実際、戻り値がマッピングされていないため、スタイル値は、具体的には、クラスデータのパッケージの先端に応答して、同じではありません。しかし、ここであなただけの、このオペレーションのコードは何の効果もありませんコピーします。
参考記事:https://blog.csdn.net/lgw96000/article/details/83268724
要約:
ここでは、springbootの簡単な統合FastDFSは基本的に完成されてきたが、そのようなローカルなどいくつかの問題、あなたはリモートサーバトラッカー192.168.xxx.xに接続することはできませんがあります:あなたは192.168.xxx.xをマッピングした場合でも、22122時間:22122の外部ネットワークアドレスは、しかし、 fastdfs_client_javaクライアントが最初の訪問トラッカーであるので、トラッカーはデバッグが不便の多くを持つことになりますので、こと、IPストレージが利用可能に戻ったが、ネットワーク内のIPアドレスとポートストレージ。この特定の場合、当分の間、何の良い解決策は、デバッグのみにイントラネットサーバアプリケーションを投げるために、ありません。
私はすぐに興味を持つようにあなたの友人の小さなパートナーとして76、があったエイダ、またチャット、映画、テレビ番組、音楽、漫画を飾った時点からの時間の共有知識にのようなプログラマ、午前私が持っている懸念にクリックし、理解していないビューやウェルカムメッセージの異なる点がある場所があります。