1. 程式人生 > 實用技巧 >SpringBoot 實現檔案上傳,圖片上傳並顯示功能

SpringBoot 實現檔案上傳,圖片上傳並顯示功能

我先看一下《頸椎病康復指南》再給大家說怎麼實現的這兩個功能,畢竟只是一個新手,解決這種複雜點的問題(相對而言),還是需要花費大量時間的,這篇文章花了兩天的時間才實現的功能,現在就記錄一下使用springboot怎麼實現檔案上傳下載的。

我這裡使用的是 springboot 2.0.3,不需要匯入相關jar包,2.x 的版本已經整合進去了,直接使用即可。

spring官網提供了 springboot 的檔案上傳下載案例,這是網址:https://spring.io/guides/gs/uploading-files/,使用的是流的輸出,對於我這個新手來說,直接不理解,所以略過,通過網上查閱大量資料,終於把問題解決了。下面的案例是 springboot2.x 圖片上傳與回顯。我使用的工具是idea。

1、建立idea預設的springboot專案,我的版本是2.0.3

2、建立一個控制層FileController

package com.rainy.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * 檔案上傳
 */
@Controller
public class FileController {

    @GetMapping(value = "/file")
    public String file() {
        return "file";
    }

    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("檔案為空空");
        }
        String fileName = file.getOriginalFilename();  // 檔名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 字尾名
        String filePath = "D://temp-rainy//"; // 上傳後的路徑
        fileName = UUID.randomUUID() + suffixName; // 新檔名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/temp-rainy/" + fileName;
        model.addAttribute("filename", filename);
        return "file";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

3、建立MyWebMvcConfigurer,這裡是配置資源對映路徑,詳細點的介紹看這篇文章:https://blog.csdn.net/qq_38762237/article/details/81283241

/**
 * 資源對映路徑
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/temp-rainy/**").addResourceLocations("file:D:/temp-rainy/");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4、jsp頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/fileUpload" method="post" enctype="multipart/form-data">
        <label>上傳圖片</label>
        <input type="file" name="file"/>
        <input type="submit" value="上傳"/>
    </form>
    <p>圖片:</p>
    <img src="${filename }"/>
</body>
</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

注意一點:我是使用jsp引擎來渲染,因為我不會用 Thymeleaf,新增jsp頁面,springboot使用jsp頁面是需要進行配置jsp整合的,預設的是 Thymeleaf 的頁面,簡單的就是HTML頁面

springboot配置jsp頁面的方法:https://blog.csdn.net/qq_38762237/article/details/81283352