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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot整合FastDFS方法過程詳解

瀏覽:9日期: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 #超時(shí)時(shí)間thumb-image:width: 150height: 150tracker-list:- 111.111.111.111:22122 #ip:端口號(hào)spring:thymeleaf:prefix: classpath:/templates/servlet:multipart:max-file-size: 50MB #單次單個(gè)文件最大大小max-request-size: 50MB #單次上傳所有文件的總大小<br>#注意,這里springboot默認(rèn)配置的大小是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 文件字節(jié) * @param fileSize 文件大小 * @param extension 文件擴(kuò)展名 * @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('獲取文件錯(cuò)誤'); 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; }}

六.前端頁(yè)面 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方法過程詳解

最后在頁(yè)面上返回一個(gè)URL,可以直接訪問

SpringBoot整合FastDFS方法過程詳解

SpringBoot整合FastDFS方法過程詳解

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日韩欧美一区二区三区在线播放 | 亚洲最新 | 中文字幕三区 | 久久aa毛片免费播放嗯啊 | 国产精品入口麻豆免费观看 | 在线观看黄色网 | 亚洲在线一区二区三区 | 一区二区三区四区无限乱码 | 亚洲成年看片在线观看男男 | a爱做片免费网站 | 国内精品1区1区3区4区 | 国产美女91呻吟求 | 久热这里只精品99re8久 | 欧美日韩专区国产精品 | 国产短视频精品一区二区三区 | 三级黄色毛片 | 久久伊人热 | 欧美污污网站 | 网址在线| h网站国产 | 亚洲图色网站 | 国产制服丝袜在线 | 欧美国产一区二区二区 | jizz国产精品免费麻豆 | 妞干网在线播放 | 青青草99热久久 | 国产精品无码专区在线观看 | 亚洲自拍色 | 欧美日韩在线视频不卡一区二区三区 | 日韩一区二区三区在线播放 | 96精品专区国产在线观看高清 | 49pao强力免费打造在线高清 | www.亚洲国产 | 91麻豆网址| 伊人色综合网一区二区三区 | 女神穿上情趣丝袜啪啪一整晚 | 老头边吃奶边做边爱 | 丁香婷婷在线观看 | 欧美一级毛级毛片 | 一级毛片成人午夜 | 免费看国产精品麻豆 |