国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

瀏覽:6日期:2023-05-01 15:45:14

創(chuàng)建SpringBoot工程:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

再導(dǎo)入所需要的依賴:

<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>

創(chuàng)建上傳業(yè)務(wù)層程序:

package cn.dzz.fastdfs.service;import org.apache.commons.lang3.StringUtils;import org.csource.fastdfs.*;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import java.util.HashMap;import java.util.Map;/** * @author DaiZhiZhou * @file Boot-With-FastDFS * @create 2020-08-13 8:55 */// @PropertySource()@Componentpublic class UploadService { @Value('${fastdfs.tracker_servers}') private String tracker_servers; @Value('${fastdfs.connect_timeout_in_seconds}') private int connect_timeout; @Value('${fastdfs.network_timeout_in_seconds}') private int network_timeout; @Value('${fastdfs.charset}') private String charset; public Map<String,Object> upload(MultipartFile multipartFile) { if (multipartFile == null) { throw new RuntimeException('文件不能為空'); } // 上傳至fastDFS, 返回文件id String fileId = this.fdfsUpload(multipartFile); if (StringUtils.isEmpty(fileId)) { System.out.println('上傳失敗'); throw new RuntimeException('上傳失敗'); } Map<String, Object> map=new HashMap<>(); map.put('code',200); map.put('msg','上傳成功'); map.put('fileId',fileId); return map; } /** * 上傳至fastDFS * @param multipartFile * @return 文件id */ private String fdfsUpload(MultipartFile multipartFile) { // 1. 初始化fastDFS的環(huán)境 initFdfsConfig(); // 2. 獲取trackerClient服務(wù) TrackerClient trackerClient = new TrackerClient(); try { TrackerServer trackerServer = trackerClient.getConnection(); // 3. 獲取storage服務(wù) StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer); // 4. 獲取storageClient StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage); // 5. 上傳文件 (文件字節(jié), 文件擴展名, ) // 5.1 獲取文件擴展名 String originalFilename = multipartFile.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf('.') + 1); // 5.2 上傳 String fileId = storageClient1.upload_file1(multipartFile.getBytes(), extName, null); return fileId; } catch (Exception e) { System.out.println(e); return null; } } /** * 初始化fastDFS的環(huán)境 */ private void initFdfsConfig() { try { ClientGlobal.initByTrackers(tracker_servers); ClientGlobal.setG_connect_timeout(connect_timeout); ClientGlobal.setG_network_timeout(network_timeout); ClientGlobal.setG_charset(charset); } catch (Exception e) { System.out.println(e); } }}

創(chuàng)建上傳控制器:

package cn.dzz.fastdfs.controller;import cn.dzz.fastdfs.service.UploadService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import java.util.Map;/** * @author DaiZhiZhou * @file Boot-With-FastDFS * @create 2020-08-13 8:58 */@RestController@RequestMapping('upload')public class UploadController { @Autowired private UploadService uploadService; /** * 作上傳 */ @RequestMapping('doUpload') public Map<String,Object> doUpload(MultipartFile mf){ System.out.println(mf.getOriginalFilename()); Map<String, Object> map = uploadService.upload(mf); return map; }}

在static目錄中創(chuàng)建index.html用于上傳測試:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>文件上傳</h1><hr><form action='/upload/doUpload' method='post' enctype='multipart/form-data'> <input type='file' name='mf'> <input type='submit' value='上傳'></form></body></html>

運行SpringBoot進行測試:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

測試成功:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

查看文件位置也可以被訪問到:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

上傳文件實現(xiàn)方式二:

更改依賴:

<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.7</version> </dependency>

創(chuàng)建一個配置類UploadProperties

package cn.dzz.fastdfs.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.List;/** * @author DaiZhiZhou * @file Boot-With-FastDFS * @create 2020-08-13 9:10 */@Data@Component@ConfigurationProperties(prefix = 'upload')public class UploadProperties { private String baseUrl; private List<String> allowTypes;}

更改之前的yml配置:

fdfs: so-timeout: 2500 # 讀取時間 connect-timeout: 600 # 連接超時時間 thumb-image: # 縮略圖 width: 100 height: 100 tracker-list: # tracker服務(wù)配置地址列表 - 服務(wù)器或者虛擬機IP:22122upload: base-url: http://服務(wù)器或者虛擬機IP/ allow-types: - image/jpeg - image/png - image/bmp - image/gif

編寫UploadProperties.java

package cn.dzz.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import java.util.List;/** * @author DaiZhiZhou * @file fdfs * @create 2020-08-13 9:33 */@ConfigurationProperties(prefix = 'upload')@Datapublic class UploadProperties { private String baseUrl; private List<String> allowTypes;}

業(yè)務(wù)層:

package cn.dzz.service;import cn.dzz.config.UploadProperties;import com.github.tobato.fastdfs.domain.fdfs.StorePath;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.apache.commons.lang3.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.IOException;/** * @author DaiZhiZhou * @file fdfs * @create 2020-08-13 9:34 */@Component@EnableConfigurationProperties(UploadProperties.class)public class UploadService { private Log log= LogFactory.getLog(UploadService.class); @Autowired private FastFileStorageClient storageClient; @Autowired private UploadProperties prop; public String uploadImage(MultipartFile file) { // 1、校驗文件類型 String contentType = file.getContentType(); if (!prop.getAllowTypes().contains(contentType)) { throw new RuntimeException('文件類型不支持'); } // 2、校驗文件內(nèi)容 try { BufferedImage image = ImageIO.read(file.getInputStream()); if (image == null || image.getWidth() == 0 || image.getHeight() == 0) { throw new RuntimeException('上傳文件有問題'); } } catch (IOException e) { log.error('校驗文件內(nèi)容失敗....{}', e); throw new RuntimeException('校驗文件內(nèi)容失敗'+e.getMessage()); } try { // 3、上傳到FastDFS // 3.1、獲取擴展名 String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), '.'); // 3.2、上傳 StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null); // 返回路徑 return prop.getBaseUrl() + storePath.getFullPath(); } catch (IOException e) { log.error('【文件上傳】上傳文件失敗!....{}', e); throw new RuntimeException('【文件上傳】上傳文件失敗!'+e.getMessage()); } }}

控制器:

package cn.dzz.controller;import cn.dzz.service.UploadService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.util.HashMap;import java.util.Map;/** * @author DaiZhiZhou * @file fdfs * @create 2020-08-13 9:35 */@RequestMapping('upload')@RestControllerpublic class UploadController { @Autowired private UploadService uploadService; @RequestMapping('doUpload') public Map<String,Object> doUpload(MultipartFile multipartFile) { System.out.println(multipartFile.getOriginalFilename()); Map<String, Object> map = new HashMap<>(); String filePath = uploadService.uploadImage(multipartFile); map.put('filePath', filePath); return map; }}

還是一樣的上傳頁面:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><h1>文件上傳</h1><hr><form action='/upload/doUpload' method='post' enctype='multipart/form-data'> <input type='file' name='mf'> <input type='submit' value='上傳'></form></body></html>

運行發(fā)現(xiàn)空指針異常,檢查發(fā)現(xiàn)表單名稱沒對上,SpringMVC就無法轉(zhuǎn)換了

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

<input type='file' name='multipartFile'>

再次測試:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

訪問:

基于SpringBoot實現(xiàn)上傳2種方法工程代碼實例

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

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产成人在线视频网站 | 久久综合伊人77777 | 日韩亚洲一区中文字幕 | 日韩国产欧美一区二区三区 | 国产一区二区三区在线视频 | 国产免费a级片 | 偷自拍 | 天堂在线亚洲 | 国产精品久久久久久福利 | 中文字幕最新中文字幕中文字幕 | 国产三级中文字幕 | 毛片网站在线 | 韩国在线精品福利视频在线观看 | 成人毛片手机版免费看 | 中文在线视频观看 | 玖玖精品视频在线 | 爽死你个放荡粗暴小淫货双女视频 | 国产精品二区页在线播放 | 亚洲第一页在线视频 | 国产盗摄一区二区三区 | 欧美最刺激好看的一级毛片 | 本道久久综合88全国最大色 | 久久99国产精品免费观看 | 欧美成人se01短视频在线看 | 国产高清a毛片在线看 | 99在线免费观看视频 | 国产三级播放 | 高清一区二区 | 日本不卡一区二区三区在线观看 | 成人自拍在线 | 精品国产免费第一区二区三区日韩 | 欧美一级级a在线观看 | 久久久国产精品免费看 | 国产精品自拍第一页 | 99精品在线免费观看 | 亚洲精品三区 | 久久99亚洲精品久久久久99 | 国产精品久久成人影院 | 欧美一级毛片兔费播放 | 在线观看国产一级强片 | 亚洲最大黄网 |