<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot+Vue+Element+FastDFS實(shí)現(xiàn)圖片的上傳功能

          共 2833字,需瀏覽 6分鐘

           ·

          2020-12-05 23:35

          點(diǎn)擊上方?Java老鐵,并選擇?設(shè)為星標(biāo)

          優(yōu)質(zhì)文章和資料會(huì)及時(shí)送達(dá)

          添加Maven依賴
          1. <dependency>

          2. <groupId>com.github.tobatogroupId>

          3. <artifactId>fastdfs-clientartifactId>

          4. <version>1.26.2version>

          5. dependency>

          編寫配置文件
          1. server:

          2. port: 9009

          3. spring:

          4. application:

          5. name: springBoot-upload

          6. servlet:

          7. multipart:

          8. max-file-size: 5MB # 限制文件上傳的大小

          9. fdfs:

          10. so-timeout: 1501 # 超時(shí)時(shí)間

          11. connect-timeout: 601 # 連接超時(shí)時(shí)間

          12. thumb-image: # 縮略圖

          13. width: 60

          14. height: 60

          15. tracker-list: # tracker地址:你的虛擬機(jī)服務(wù)器地址+端口(默認(rèn)是22122)

          16. - 192.168.206.140:22122

          編寫上傳的service
          1. @Service

          2. public class UploadService {


          3. private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);


          4. private static final List<String> CONTENT_TYPES = Arrays.asList("image/jpeg", "image/gif","image/png");


          5. @Autowired

          6. private FastFileStorageClient storageClient;



          7. public String upload(MultipartFile file) {

          8. //獲取文件名稱

          9. String originalFilename = file.getOriginalFilename();

          10. //校檢文件的類型

          11. String contentType = file.getContentType();

          12. if (!CONTENT_TYPES.contains(contentType)){

          13. LOGGER.info("文件類型不合法:{}",originalFilename);

          14. return null;

          15. }

          16. try {

          17. // 校驗(yàn)文件的內(nèi)容

          18. BufferedImage bufferedImage = ImageIO.read(file.getInputStream());

          19. if (bufferedImage == null){

          20. LOGGER.info("文件內(nèi)容不合法:{}", originalFilename);

          21. return null;

          22. }


          23. // 保存到服務(wù)器

          24. //file.transferTo(new File("D:\\upload\\images\\" + originalFilename));

          25. String ext = StringUtils.substringAfterLast(originalFilename, ".");

          26. StorePath storePath = this.storageClient.uploadFile(file.getInputStream(), file.getSize(), ext, null);


          27. // 生成url地址,返回

          28. //return "http://127.0.0.1/" + originalFilename;

          29. return "http://192.168.206.140/" + storePath.getFullPath();

          30. } catch (IOException e) {

          31. LOGGER.info("服務(wù)器內(nèi)部錯(cuò)誤:{}", originalFilename);

          32. e.printStackTrace();

          33. }

          34. return null;

          35. }

          36. }

          編寫Controller
          1. @RestController

          2. @RequestMapping("/upload")

          3. @CrossOrigin

          4. public class UploadController {


          5. @Autowired

          6. private UploadService uploadService;


          7. @PostMapping("/image")

          8. public Result uploadImage(@RequestParam("file") MultipartFile file){

          9. String url = this.uploadService.upload(file);

          10. if (StringUtils.isBlank(url)) {

          11. return new Result(true, StatusCode.OK,"上傳失敗");

          12. }

          13. return new Result(true, StatusCode.OK,"上傳成功",url);

          14. }

          15. }

          編寫前端部分

          前端使用了Vue加上ElementUI框架

          1. <div>

          2. <el-upload :action="uploadURL" list-type="picture" :on-preview="handlePreview" multiple>

          3. <el-button size="small" type="primary">點(diǎn)擊上傳el-button>

          4. el-upload>

          5. <el-dialog :visible.sync="previewVisible" title="圖片預(yù)覽">

          6. <img :src="previewPath" alt="" class="previewImg">

          7. el-dialog>

          8. div>

          圖片預(yù)覽的邏輯

          1. // 處理圖片預(yù)覽效果

          2. handlePreview(file) {

          3. console.log(file)

          4. this.previewPath = file.response.data

          5. this.previewVisible = true

          6. },

          測(cè)試

          運(yùn)行地址:http://localhost:8080/upload

          界面

          上傳

          可以一次上傳多張圖片,點(diǎn)擊圖片的名字還可以查看預(yù)覽


          前端代碼地址:https://gitee.com/mytimelife/fastdfs_vue.git



          注:圖片來源網(wǎng)絡(luò),侵權(quán),聯(lián)系刪除


          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久18禁| 亚洲AV成人无码久久精品毛片 | 国产一区二区在线播放 | 人妻公日日澡久久久 | 啊啊啊啊啊在线 |