文件的上傳

基于SpringBoot的文件上傳

上傳方式:

  • 1.直接上傳到應(yīng)用服務(wù)器
  • 2.上傳到OSS(阿里云、七牛云)
  • 3.前端將圖片轉(zhuǎn)成Base64編譯上傳(僅用于小容量圖片)

使用第一種方式的步驟

  • 1、創(chuàng)建upload模塊


    image.png

    填寫模塊名和包名


    image.png

    注意勾選web和thymeleaf選項(xiàng)
    image.png

    image.png
  • 2、添加web、thymeleaf依賴
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 3、限制文件大小(在resources目錄下的application.properties下寫限制條件)
spring.servlet.multipart.max-file-size=100MB
  • 4、使用java.nio.files實(shí)現(xiàn)文件的上傳(在com.spring.upload包下建一個(gè)controller包,controller包下建一個(gè)UploadController類,在UploadController下編寫代碼)
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 上傳文件控制器
 * 直接上傳到服務(wù)器
 */
@Controller
public class UploadController {
    //制定一個(gè)臨時(shí)路徑
    //private  static String UPLOAD_FOLDER="E:/temp/";

    //遇到http://localhost:8080則跳轉(zhuǎn)到upload.html
    @GetMapping("/")
    public String index(){
        return "upload";
    }
    @PostMapping("/upload")
    public String fileUpload(@RequestParam("file") MultipartFile srcFile, RedirectAttributes redirectAttributes){
        if(srcFile.isEmpty()){
            redirectAttributes.addFlashAttribute("message","選擇一個(gè)文件");
            return "redirect:upload_status";
        }
        try {
            File destFile=new File(ResourceUtils.getURL("classpath").getPath());
            if(destFile.exists()){
                destFile=new File("");
            }
            //輸出目標(biāo)文件的絕對(duì)路徑
            System.out.println("file path:"+destFile.getAbsolutePath());
            //拼接子路經(jīng)
            File upload=new File(destFile.getAbsolutePath(),"static/");
            //目標(biāo)文件不存在,創(chuàng)建一個(gè)
            if(!upload.exists()){
                upload.mkdirs();
            }
            System.out.println("完整的上傳路徑:"+upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            byte[] bytes=srcFile.getBytes();
            //拼接上傳路徑
            //Path path= Paths.get(UPLOAD_FOLDER+srcFile.getOriginalFilename());
            //通過項(xiàng)目路徑,拼接上傳路徑
            Path path=Paths.get(upload.getAbsolutePath()+"/"+srcFile.getOriginalFilename());
            Files.write(path,bytes);
            redirectAttributes.addFlashAttribute("message","文件上傳成功");
        }catch (IOException e){
            e.printStackTrace();
        }
        return "redirect:upload_status";
    }
    //匹配upload_status
    @GetMapping("/upload_status")
    public String uploadStatusPage(){
        return "upload_status";
    }
}
  • 5、resources->templates下建html文件
  • upload.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>spring boot文件上傳</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上傳">
</form>
</body>
</html>
  • upload_status.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>文件上傳狀態(tài)顯示</title>
</head>
<body>
<h2>spring boot文件上傳狀態(tài)</h2>
<div th:if="${message}">
    <h2 th:text="${message}"></h2>
</div>
</body>
</html>

運(yùn)行結(jié)果


image.png

選擇文件


image.png

上傳
image.png

查看文件夾(上傳成功)


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容