亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot整合FastDFS方法過程詳解

瀏覽:35日期:2023-05-19 08:33:42

一.pom.xml

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wj</groupId> <artifactId>fastdsf-boot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>fastdsf-boot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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> <build> <plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

二.application.yml

#fastdfs 配置fdfs:so-timeout: 150000connect-timeout: 150000 #超時時間thumb-image:width: 150height: 150tracker-list:- 111.111.111.111:22122 #ip:端口號spring:thymeleaf:prefix: classpath:/templates/servlet:multipart:max-file-size: 50MB #單次單個文件最大大小max-request-size: 50MB #單次上傳所有文件的總大小<br>#注意,這里springboot默認配置的大小是1MB和10MB,可能不夠用,具體參考MultipartProperties.java

SpringBoot整合FastDFS方法過程詳解

三.FastUtil.java 前提先將Nginx和FastDFS整合

@Componentpublic class FastUtil { private final Logger logger = LoggerFactory.getLogger(FastUtil.class); @Autowired private FastFileStorageClient fastFileStorageClient; /** * 文件上傳 * 最后返回fastDFS中的文件名稱; * * @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); return 'http://111.111.111.111/'+storePath.getFullPath(); } public byte[] downloadFile(String group,String path) throws IOException { DownloadByteArray downloadByteArray = new DownloadByteArray(); byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray); return bytes; } }

四.配置類 FdfsConfig.java

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

五.Controller

@RestControllerpublic class FdfsController { @Autowired private FastUtil fastDFSClientWrapper; private final Logger logger = LoggerFactory.getLogger(FdfsController.class); @PostMapping('/upload') @ResponseBody public String 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); return string; }}

六.前端頁面 index.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>

七.開始上傳

SpringBoot整合FastDFS方法過程詳解

最后在頁面上返回一個URL,可以直接訪問

SpringBoot整合FastDFS方法過程詳解

SpringBoot整合FastDFS方法過程詳解

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 日本久久综合视频 | 国产在线观看黄 | 永久免费人成网ww555kkk手机 | 啪啪啪毛片 | 黄色视屏免费 | 国产大片网站 | 国产成人综合亚洲一区 | 欧美一区视频 | 国产黄色二级片 | 国产亚洲精品福利片 | 特级aa毛片在线播放 | 国产精品福利在线播放 | 国产日韩精品一区二区在线观看播放 | 国产无套乱子伦精彩是白视频 | 国产精品久久久亚洲456 | 国产区成人精品视频 | 日本人xxxxxxx中国 | 日本精品久久久中文字幕 | 国产乱人视频免费观看 | 亚洲欧美日韩视频一区 | 免费操片| 国产成人精品免费 | 黄色精品| 亚洲国产系列久久精品99人人 | 国产精品久久久久久久午夜片 | 久久久婷婷亚洲5月97色 | 黄色片亚洲| 亚洲第一区精品观看 | 国产美女一级高清免费观看 | 精品欧美日韩一区二区三区 | 一级一级黄色片 | 免费人成又黄又爽的视频强 | 99久久这里只精品国产免费 | 成人午夜大片免费看爽爽爽 | 欧美日韩高清在线 | 亚洲国产欧美精品一区二区三区 | 国产亚洲美女精品久久久久狼 | 在线视频免费观看a毛片 | 播放欧亚一级特黄录像 | 国产在线视频国产永久视频 | 国产免费观看a大片的网站 国产免费观看网站黄页 |