Spring Boot 文件上傳簡易教程


上傳文件是我們日常使用最為廣泛的功能之一,比如App端上傳頭像。本章演示如何從客戶端上傳到 Spring Boot 開發的 Api中。

本項目源碼 github 下載

1 新建 Spring Boot Maven 示例工程項目

注意:本示例是用 IDEA 開發工具

  1. File > New > Project,如下圖選擇 Spring Initializr 然后點擊 【Next】下一步
  2. 填寫 GroupId(包名)、Artifact(項目名) 即可。點擊 下一步
    groupId=com.fishpro
    artifactId=upload
  3. 選擇依賴 Spring Web Starter 前面打鈎。
  4. 項目名設置為 spring-boot-study-upload.

文件上傳不需要引入第三方組件。

2 依賴引入 Pom.xml

為了演示代碼,這里引入 thymeleaf

<dependencies>
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3 編寫上傳示例

本章代碼主要演示單文件上傳和多文件上傳,前端采用 thymeleaf 模板,實際上就是一個html文件。文件清單包括

  1. uploadfile.html 前端文件
  2. FileController.java 控制層文件
  3. FileUtil.java 文件常用類

3.1 控制層代碼

主要使用 MultipartFile 來實現,如下代碼 /upload 和 /uploads 分別為單文件上傳和多文件上傳。其中 @Value("${fishpro.uploadPath}") 是配置文件中設置的。

server.port=8086
fishpro.uploadPath=/Users/jiaojunkang/Desktop/upload/
@Controller
public class FileController {

    @Value("${fishpro.uploadPath}")
    private String uploadPath;

    @GetMapping("/uploadfile")
    public String uploadfile(){
        return "uploadfile";
    }


    /**
     * 單文件
     * */
    @PostMapping("/upload")
    @ResponseBody
    Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Map<String,Object> map=new HashMap();
        map.put("status",0);
        String fileName = file.getOriginalFilename();
        fileName = UUID.randomUUID().toString(); //對文件名稱重命名

        try {
            FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
            map.put("filename",fileName);
        } catch (Exception e) {
           map.put("status",-1);
           map.put("message",e.getMessage());

        }


        return map;
    }

    /**
     * 多文件
     * */
    @PostMapping("/uploads")
    @ResponseBody
    Object uploads(@RequestParam("files") MultipartFile [] files, HttpServletRequest request) {
        Map<String,Object> map=new HashMap();
        map.put("status",0);
        List<String> filenames=new ArrayList<>();
        for (MultipartFile file: files
             ) {
            String ext = file.getOriginalFilename().split("\\.")[1];
            String fileName = file.getOriginalFilename();
            //fileName = UUID.randomUUID().toString()+"."+ext; //對文件名稱重命名

            try {
                FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
                filenames.add(fileName);
            } catch (Exception e) {
                map.put("status",-1);
                map.put("message",e.getMessage());
                return  map;

            }
        }

        map.put("filename",filenames);
        return map;
    }
}

3.2 前端文件

前端文件這里演示的比較簡單,可以有多中形態,這里使用 form 來提交。

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

<form enctype="multipart/form-data" method="post" action="/uploads">
    <p>文件1<input type="file" name="files"/></p>
    <p>文件2<input type="file" name="files"/></p>
    <p>文件3<input type="file" name="files"/></p>
    <p><input type="submit" value="上傳"/></p>
</form>
</body>
</html>

3.3 文件保存類

public class FileUtil {

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路徑所對應的文件存在,並且是一個文件,則直接刪除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static String renameToUUID(String fileName) {
        return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
    }
}

本項目源碼 github 下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM