SpringBoot(7) - - SpringBoot文件上傳

項目地址:https://github.com/zhaopeng01/springboot-study/tree/master/study7

在我們開發的項目中,多多少少會遇到文件上傳的問題,或者上傳個圖片亦或者是個文本,這篇就寫寫文件上傳;

在這篇中我打算寫2中方式,先來個簡單方式的,再來個複雜方式的;
廢話少說,代碼走起

1.偷懶方式,簡單處理

首先先寫個簡簡單單的上傳頁面

頁面

然後寫文件上傳的controller方法
來處理一些邏輯 比如獲取文件名稱 或者寫入到哪裏

 controller

這時候如果上傳大文件時候會報錯 這時候要手動設置要上傳的文件大小

設置文件大小

2.相對上面複雜點處理

依賴

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zyc</groupId>
    <artifactId>study7</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>study7</name>
    <description>zyc study</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在這裏使用了thymeleaf,它 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。
在這裏不對它做過多解釋,想學習可以-> https://www.baidu.com/s?ie=UTF-8&wd=thymeleaf

controller

package com.zyc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 圖片上傳
 *
 * @author zhaopeng
 * @email [email protected]
 */


@Controller
@RequestMapping("/uploads")
public class FileUploadController {

    private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);

    @GetMapping
    public String index() {
        return "index";
    }

    /**
     * @Description: 單文件
     * @author zhaopeng
     * @email [email protected]
     */
    @PostMapping("/uploadSing")
    @ResponseBody
    public Object uploadSing(@RequestParam("file") MultipartFile file) throws IOException {
        file.transferTo(new File("E:\\idea_workspace\\springboot-study\\" + file.getOriginalFilename()));
        return "SUCCESS";
    }

    /**
     * @Description: 多文件
     * @author zhaopeng
     * @email [email protected]
     */
    @PostMapping("/uploads")
    @ResponseBody
    public Object uploads(@RequestParam("file") MultipartFile[] files) throws IOException {
        if (files == null || files.length == 0) {
            return null;
        }
        for (MultipartFile file : files) {
            file.transferTo(new File("E:\\idea_workspace\\springboot-study\\" + file.getOriginalFilename()));
        }
        return "SUCCESS";
    }
}

創建一個FileUploadController,@PostMapping相關方法則是對應的 單文件上傳和多文件上傳。

@RequestParam(“file”) 此處的"file"對應的就是html 中 name=“file” 的 input 標籤

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文件上傳</title>
</head>
<body>

<h2>單文件上傳</h2>
<div>
    <form method="POST" enctype="multipart/form-data" action="/uploads/uploadSing">
        <p>
            文件1:<input type="file" name="file"/>
            <input type="submit" value="上傳"/>
        </p>
    </form>
</div>

<hr/>
<h2>多文件上傳</h2>

<div>
    <form method="POST" enctype="multipart/form-data"
          action="/uploads/uploads">
        <p>
            文件1:<input type="file" name="file"/>
        </p>
        <p>
            文件2:<input type="file" name="file"/>
        </p>
        <p>
            <input type="submit" value="上傳"/>
        </p>
    </form>
</div>

</body>
</html>

主函數

package com.zyc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/*
 * @author zhaopeng
 */

@SpringBootApplication
public class Study7Application {

    public static void main(String[] args) {
        SpringApplication.run(Study7Application.class, args);
    }
}

測試

完成準備事項後,啓動主函數,訪問 http://localhost:8080/uploads 進入到文件上傳頁面
就可以自己搞搞搞了!

結果
這裏寫圖片描述

好的到這裏本篇文章就先到此了,創作不易,如果那裏有不合適的地方還請大家多多指教,寫這篇博的目的主要就是爲了方便自己以後的一個回憶和朋友學習時的一個參考,希望爲大家可以帶來幫助 ~ ~&

虛心的去學習,自信的去工作~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章