Spring實(shí)現(xiàn)文件上傳功能
本篇文章,我們要來(lái)做一個(gè)Spring的文件上傳功能:
1. 創(chuàng)建一個(gè)Maven的web工程,然后配置pom.xml文件,增加依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.0.2.RELEASE</version> </dependency>
2.在webapp目錄下的index.jsp文件中輸入一個(gè)表單:
<html>
<body>
<form method="POST" enctype="multipart/form-data"
action="/upload">
File to upload: <input type="file" name="file"><br /> Name: <input
type="text" name="name"><br /> <br /> <input type="submit"
value="Upload"> Press here to upload the file!
</form>
</body>
</html>
這個(gè)表單就是我們模擬的上傳頁(yè)面。
3. 編寫處理這個(gè)表單的Controller:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
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;
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
4. 然后我們對(duì)上傳的文件做一些限制,同時(shí)編寫main方法來(lái)啟動(dòng)這個(gè)web :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
5. 然后訪問(wèn)http://localhost:8080/upload就可以看到頁(yè)面了。
上面的例子是實(shí)現(xiàn)的是單個(gè)文件上傳的功能,假定我們現(xiàn)在要實(shí)現(xiàn)文件批量上傳的功能的話,我們只需要簡(jiǎn)單的修改一下上面的代碼就行,考慮到篇幅的問(wèn)題,下面只是貼出和上面不同的代碼,沒(méi)有貼出的說(shuō)明和上面一樣。:
1. 新增batchUpload.jsp文件
<html> <body> <form method="POST" enctype="multipart/form-data" action="/batch/upload"> File to upload: <input type="file" name="file"><br /> File to upload: <input type="file" name="file"><br /> <input type="submit" value="Upload"> Press here to upload the file! </form> </body> </html>
2. 新增BatchFileUploadController.java文件:
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.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
/**
* Created by wenchao.ren on 2014/4/26.
*/
@Controller
public class BatchFileUploadController {
@RequestMapping(value="/batch/upload", method= RequestMethod.POST)
public @ResponseBody
String handleFileUpload(HttpServletRequest request){
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
for (int i =0; i< files.size(); ++i) {
MultipartFile file = files.get(i);
String name = file.getName();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + i)));
stream.write(bytes);
stream.close();
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
return "upload successful";
}
}
這樣一個(gè)簡(jiǎn)單的批量上傳文件的功能就ok了,是不是很簡(jiǎn)單啊。
注意:上面的代碼只是為了演示而已,所以編碼風(fēng)格上采取了隨性的方式,不建議大家模仿。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC文件上傳 多文件上傳實(shí)例
- Spring實(shí)現(xiàn)文件上傳(示例代碼)
- springMVC配置環(huán)境實(shí)現(xiàn)文件上傳和下載
- jquery.form.js框架實(shí)現(xiàn)文件上傳功能案例解析(springmvc)
- Spring學(xué)習(xí)筆記2之表單數(shù)據(jù)驗(yàn)證、文件上傳實(shí)例代碼
- springMVC實(shí)現(xiàn)前臺(tái)帶進(jìn)度條文件上傳的示例代碼
- SpringMvc導(dǎo)出Excel實(shí)例代碼
- 詳解springMVC兩種方式實(shí)現(xiàn)多文件上傳及效率比較
- Spring MVC 文件上傳下載的實(shí)例
相關(guān)文章
詳解spring mvc 請(qǐng)求轉(zhuǎn)發(fā)和重定向
這篇文章主要介紹了詳解spring mvc 請(qǐng)求轉(zhuǎn)發(fā)和重定向,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
Java+opencv3.2.0實(shí)現(xiàn)模板匹配
這篇文章主要為大家詳細(xì)介紹了Java+opencv3.2.0實(shí)現(xiàn)模板匹配的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
java實(shí)現(xiàn)動(dòng)態(tài)代理示例分享
動(dòng)態(tài)代理作為代理模式的一種擴(kuò)展形式,廣泛應(yīng)用于框架(尤其是基于AOP的框架)的設(shè)計(jì)與開發(fā),本文將通過(guò)實(shí)例來(lái)講解Java動(dòng)態(tài)代理的實(shí)現(xiàn)過(guò)程。2014-03-03
spring boot自定義404錯(cuò)誤信息的方法示例
這篇文章主要介紹了spring boot自定義404錯(cuò)誤信息的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
如何解決java.util.concurrent.CancellationException問(wèn)題
這篇文章主要介紹了如何解決java.util.concurrent.CancellationException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
深入淺出Java mvc_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了MVC的基礎(chǔ)知識(shí),MVC是一個(gè)框架模式,它強(qiáng)制性的使應(yīng)用程序的輸入、處理和輸出分開,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Java實(shí)現(xiàn)多級(jí)表頭和復(fù)雜表頭的導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)多級(jí)表頭和復(fù)雜表頭的導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Spring Data JPA 復(fù)雜/多條件組合分頁(yè)查詢
本文主要介紹了Spring Data JPA 復(fù)雜/多條件組合分頁(yè)查詢的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
深入理解Java責(zé)任鏈模式實(shí)現(xiàn)靈活的請(qǐng)求處理流程
本文詳細(xì)介紹了Java中的責(zé)任鏈模式,幫助您理解其工作原理,以及如何在代碼中實(shí)現(xiàn)。該模式可以將請(qǐng)求沿著處理鏈路傳遞,實(shí)現(xiàn)靈活的請(qǐng)求處理流程。通過(guò)本文的學(xué)習(xí),您將獲得在Java應(yīng)用程序中使用責(zé)任鏈模式的知識(shí)和技能2023-04-04

