欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂java培訓(xùn)簡(jiǎn)述java中Spring boot實(shí)現(xiàn)文件上傳功能

2018-06-28 09:25:01 1287瀏覽

 本文實(shí)例為大家分享了Springboot實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下





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è)表單:
<formmethod="POST"enctype="multipart/form-data"action="/upload"> </body>

<html>
<body>
<formmethod="POST"enctype="multipart/form-data"action="/upload">
Filetoupload:<inputtype="file"name="file"><br/>
Name:<inputtype="text"name="name"><br/><br/>
<inputtype="submit"value="Upload">
Pressheretouploadthefile!
</form>
</body>
這個(gè)表單就是我們模擬的上傳頁面

3.編寫處理這個(gè)表單的Controller:


importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.multipart.MultipartFile;
@Controller
publicclassFileUploadController{
@RequestMapping(value="/upload",method=RequestMethod.GET)
public@ResponseBodyStringprovideUploadInfo(){
return"YoucanuploadafilebypostingtothissameURL.";
}
@RequestMapping(value="/upload",method=RequestMethod.POST)
public@ResponseBodyStringhandleFileUpload(@RequestParam("name")Stringname,
@RequestParam("file")MultipartFilefile){
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
BufferedOutputStreamstream=
newBufferedOutputStream(newFileOutputStream(newFile(name+"-uploaded")));
stream.write(bytes);
stream.close();
return"Yousuccessfullyuploaded"+name+"into"+name+"-uploaded!";
}catch(Exceptione){
return"Youfailedtoupload"+name+"=>"+e.getMessage();
}
}else{
return"Youfailedtoupload"+name+"becausethefilewasempty.";
}
}
}


4.然后我們對(duì)上傳的文件做一些限制,同時(shí)編寫main方法來啟動(dòng)這個(gè)web:

importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;
importorg.springframework.boot.context.embedded.MultiPartConfigFactory;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importjavax.servlet.MultipartConfigElement;
@Configuration
@ComponentScan
@EnableAutoConfiguration
publicclassApplication{
@Bean
publicMultipartConfigElementmultipartConfigElement(){
MultiPartConfigFactoryfactory=newMultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
returnfactory.createMultipartConfig();
}
publicstaticvoidmain(String[]args){
SpringApplication.run(Application.class,args);
}
}

5.然后訪問http://localhost:8080/upload就可以看到頁面了。

上面的例子是實(shí)現(xiàn)的是單個(gè)文件上傳的功能,假定我們現(xiàn)在要實(shí)現(xiàn)文件批量上傳的功能的話,我們只需要簡(jiǎn)單的修改一下上面的代碼就行,考慮到篇幅的問題,下面只是貼出和上面不同的代碼,沒有貼出的說明和上面一樣。:

1.新增batchUpload.jsp文件
<html>
<body>
<formmethod="POST"enctype="multipart/form-data"
action="/batch/upload">
Filetoupload:<inputtype="file"name="file"><br/>
Filetoupload:<inputtype="file"name="file"><br/>
<inputtype="submit"value="Upload">Pressheretouploadthefile!
</form>
</body>
</html>


2.新增BatchFileUploadController.java文件:

importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.multipart.MultipartFile;
importorg.springframework.web.multipart.MultipartHttpServletRequest;
importjavax.servlet.http.HttpServletRequest;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.List;
/**
*Createdbywenchao.renon2014/4/26.
*/
@Controller
publicclassBatchFileUploadController{
@RequestMapping(value="/batch/upload",method=RequestMethod.POST)
public@ResponseBody
StringhandleFileUpload(HttpServletRequestrequest){
List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles("file");
for(inti=0;i<files.size();++i){
MultipartFilefile=files.get(i);
Stringname=file.getName();
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
BufferedOutputStreamstream=
newBufferedOutputStream(newFileOutputStream(newFile(name+i)));
stream.write(bytes);
stream.close();
}catch(Exceptione){
return"Youfailedtoupload"+name+"=>"+e.getMessage();
}
}else{
return"Youfailedtoupload"+name+"becausethefilewasempty.";
}
}
return"uploadsuccessful";
}
}


這樣一個(gè)簡(jiǎn)單的批量上傳文件的功能就ok了,是不是很簡(jiǎn)單啊。以上就是扣丁學(xué)堂為大家?guī)淼膉ava中Spring boot實(shí)現(xiàn)文件上傳功能,如果你對(duì)java感興趣的話,請(qǐng)登錄扣丁學(xué)堂官網(wǎng),或者關(guān)注微信公眾號(hào),大量java在線視頻教程等你來觀看!

扣丁學(xué)堂微信公眾號(hào)

【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】




查看更多關(guān)于“Java開發(fā)資訊的相關(guān)文章>>


標(biāo)簽: java java培訓(xùn) java在線視頻教程 java編程 java語言

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部