日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springboot 上传文件_基于SpringBoot的文件上传

發布時間:2024/7/5 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 上传文件_基于SpringBoot的文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在實際的企業開發中,文件上傳是最常見的功能之一,SpringBoot集成了SpringMVC常用的功能,當然也包含了文

件上傳的功能,實現起來沒有太多的區別。

下面我們來講解一下,使用SpringBoot如何實現多個文件上傳操作。使用的環境是IntelliJ IDE開發工具。

開發過程如下:

1.1 配置pom.xml文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.0.3.RELEASE

com.qianfeng.springboot

fileupload

0.0.1-SNAPSHOT

fileupload

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-test

test

1.2 application.properties

1.3 Controller類

org.springframework.boot

spring-boot-maven-plugin

server.port=8080

package com.qianfeng.springboot.fileupload.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.File;

import java.io.IOException;

@Controller

public class UploadFileController {

@RequestMapping("/uploadfile")

public String uploadOneFile(){

return "uploadonefile"; // 最終由ThymeleafView處理,轉發 classpath:templates

}

@RequestMapping("/uploadfiles")

public String uploadMultiFile(){

return "uploadmultifile"; // 最終由ThymeleafView處理,轉發 classpath:templates

}

/**

* 上傳單個文件

* @param file

* @param request

* @return

*/

@RequestMapping(value = "/uploadsimplefile", method = RequestMethod.POST)

@ResponseBody

public String uploadSimpleFile(@RequestParam("attachment") MultipartFile file,

HttpServletRequest request) {

try {

String fileDir =

request.getSession().getServletContext().getRealPath("/upload/");

System.out.println("------->>"+fileDir);

File dir = new File(fileDir);

if (!dir.exists()) {

dir.mkdirs();

}

String fileName = file.getOriginalFilename();

File upload_file = new File(fileDir + fileName);

file.transferTo(upload_file);

} catch (Exception e) {

e.printStackTrace();

return "上傳失敗";

}

return "上傳成功";

}

/**

* 上傳多個文件

* @param file

* @param request

* @return

*/

@RequestMapping(value = "/uploadmultifile", method = RequestMethod.POST)

@ResponseBody

public String uploadMultiFile(@RequestParam("attachment") MultipartFile[] file,

HttpServletRequest request) {

try {

String fileDir =

request.getSession().getServletContext().getRealPath("/upload/");

System.out.println("------->>"+fileDir);

File dir = new File(fileDir);

if (!dir.exists()) {

dir.mkdirs();

}

for (int i=0;i

if (file[i]!=null){

uploadFile(fileDir,file[i]);

}

}

} catch (Exception e) {

e.printStackTrace();

return "上傳失敗";

}

return "上傳成功";

}

1.4 單個文件上傳的頁面

/**

*

* @param uploadDir

* @param file

* @throws IOException

*/

public void uploadFile(String uploadDir,MultipartFile file) throws IOException{

String file_name = file.getOriginalFilename();

File upload_file = new File(uploadDir+file_name);

file.transferTo(upload_file);

}

}

文件上傳

單個文件上傳

th:action="@{/uploadsimplefile}" role="form">

文件上傳:

id="attachment" placeholder="請選擇郵件附件"/>

上 傳

1.5 上傳多個文件頁面

文件上傳

單個文件上傳

th:action="@{/uploadmultifile}" role="form">

文件上傳:

id="attachment1" placeholder="請選擇郵件附件"/>

文件上傳:

id="attachment2" placeholder="請選擇郵件附件"/>

文件上傳:

id="attachment3" placeholder="請選擇郵件附件"/>

1.6 測試

1.6.1 單文件上傳

啟動項目后,在瀏覽器中輸入:http://localhost:8080/uploadfile,如下所示:

選擇單個文件上傳。

1.6.2 多文件上傳

在瀏覽器中輸入:http://localhost:8080/uploadfiles,如下所示:

選擇多個文件上。

總結

以上是生活随笔為你收集整理的springboot 上传文件_基于SpringBoot的文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。