欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2021年10月26日 09:31:45   作者:卡通人物nnn  
這篇文章主要為大家詳細(xì)介紹了Spring boot實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

1. 創(chuàng)建一個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文件中輸入一個表單:

<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> 

這個表單就是我們模擬的上傳頁面

3. 編寫處理這個表單的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. 然后我們對上傳的文件做一些限制,同時(shí)編寫main方法來啟動這個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. 然后訪問http://localhost:8080/upload就可以看到頁面了。

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

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"; 
 } 
} 

這樣一個簡單的批量上傳文件的功能就ok了,是不是很簡單啊。

注意:上面的代碼只是為了演示而已,所以編碼風(fēng)格上采取了隨性的方式,不建議大家模仿。

參考資料:MultipartResolver實(shí)現(xiàn)文件上傳功能

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

1. MultipartResolver也可以實(shí)現(xiàn)文件上傳功能。參考文章:

相關(guān)文章

  • Springboot集成Mybatis-Flex的示例詳解

    Springboot集成Mybatis-Flex的示例詳解

    Mybatis-Flex 是一個優(yōu)雅的?Mybatis 增強(qiáng)框架,它非常輕量、同時(shí)擁有極高的性能與靈活性,本文主要介紹了Springboot集成Mybatis-Flex的示例詳解,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • java TreeUtil菜單遞歸工具類

    java TreeUtil菜單遞歸工具類

    這篇文章主要為大家詳細(xì)介紹了java TreeUtil菜單遞歸工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 深入淺析Java 循環(huán)中標(biāo)簽的作用

    深入淺析Java 循環(huán)中標(biāo)簽的作用

    這篇文章主要介紹了深入淺析Java 循環(huán)中標(biāo)簽的作用的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • SpringBoot啟動時(shí)自動執(zhí)行sql腳本的方法步驟

    SpringBoot啟動時(shí)自動執(zhí)行sql腳本的方法步驟

    本文主要介紹了SpringBoot啟動時(shí)自動執(zhí)行sql腳本的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 如何設(shè)計(jì)一個安全的API接口詳解

    如何設(shè)計(jì)一個安全的API接口詳解

    在日常開發(fā)中,總會接觸到各種接口,前后端數(shù)據(jù)傳輸接口,第三方業(yè)務(wù)平臺接口,下面這篇文章主要給大家介紹了關(guān)于如何設(shè)計(jì)一個安全的API接口的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Struts2 的國際化實(shí)現(xiàn)方式示例

    Struts2 的國際化實(shí)現(xiàn)方式示例

    這篇文章主要介紹了Struts2 的國際化實(shí)現(xiàn)方式示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 詳解Spring Cloud Hystrix斷路器實(shí)現(xiàn)容錯和降級

    詳解Spring Cloud Hystrix斷路器實(shí)現(xiàn)容錯和降級

    本篇文章主要介紹了詳解Spring Cloud Hystrix斷路器實(shí)現(xiàn)容錯和降級,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java web過濾器驗(yàn)證登錄防止未登錄進(jìn)入界面

    Java web過濾器驗(yàn)證登錄防止未登錄進(jìn)入界面

    這篇文章主要介紹了Java web過濾器驗(yàn)證登錄防止未登錄進(jìn)入界面,在一些系統(tǒng)中經(jīng)常可以用到此功能,對java web 驗(yàn)證登錄知識感興趣的朋友一起看下吧
    2016-08-08
  • SpringBoot使用JSP作為視圖模板的方法

    SpringBoot使用JSP作為視圖模板的方法

    這篇文章主要介紹了SpringBoot使用JSP作為視圖模板的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • kafka并發(fā)寫大消息異常TimeoutException排查記錄

    kafka并發(fā)寫大消息異常TimeoutException排查記錄

    這篇文章主要為大家介紹了kafka并發(fā)寫大消息異常TimeoutException的排查記錄及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02

最新評論