最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot實現(xiàn)文件上傳示例代碼

 更新時間:2017年03月22日 09:39:45   作者:catoop  
本篇文章主要介紹了Spring Boot實現(xiàn)文件上傳示例代碼,可以實現(xiàn)單文件和多文件的上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

使用SpringBoot進行文件上傳的方法和SpringMVC差不多,本文單獨新建一個最簡單的DEMO來說明一下。

主要步驟包括:

1、創(chuàng)建一個springboot項目工程,本例名稱(demo-uploadfile)。

2、配置 pom.xml 依賴。

3、創(chuàng)建和編寫文件上傳的 Controller(包含單文件上傳和多文件上傳)。

4、創(chuàng)建和編寫文件上傳的 HTML 測試頁面。

5、文件上傳相關(guān)限制的配置(可選)。

6、運行測試。

項目工程截圖如下:

文件代碼:

  <dependencies>

    <!-- spring boot web支持 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- thmleaf模板依賴. -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
package com.example.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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 org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 文件上傳的Controller
 * 
 * @author 單紅宇(CSDN CATOOP)
 * @create 2017年3月11日
 */
@Controller
public class FileUploadController {

  // 訪問路徑為:http://ip:port/upload
  @RequestMapping(value = "/upload", method = RequestMethod.GET)
  public String upload() {
    return "/fileupload";
  }

  // 訪問路徑為:http://ip:port/upload/batch
  @RequestMapping(value = "/upload/batch", method = RequestMethod.GET)
  public String batchUpload() {
    return "/mutifileupload";
  }

  /**
   * 文件上傳具體實現(xiàn)方法(單文件上傳)
   *
   * @param file
   * @return
   * 
   * @author 單紅宇(CSDN CATOOP)
   * @create 2017年3月11日
   */
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  @ResponseBody
  public String upload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
      try {
        // 這里只是簡單例子,文件直接輸出到項目路徑下。
        // 實際項目中,文件需要輸出到指定位置,需要在增加代碼處理。
        // 還有關(guān)于文件格式限制、文件大小限制,詳見:中配置。
        BufferedOutputStream out = new BufferedOutputStream(
            new FileOutputStream(new File(file.getOriginalFilename())));
        out.write(file.getBytes());
        out.flush();
        out.close();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
        return "上傳失敗," + e.getMessage();
      } catch (IOException e) {
        e.printStackTrace();
        return "上傳失敗," + e.getMessage();
      }
      return "上傳成功";
    } else {
      return "上傳失敗,因為文件是空的.";
    }
  }

  /**
   * 多文件上傳 主要是使用了MultipartHttpServletRequest和MultipartFile
   *
   * @param request
   * @return
   * 
   * @author 單紅宇(CSDN CATOOP)
   * @create 2017年3月11日
   */
  @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
  public @ResponseBody String batchUpload(HttpServletRequest request) {
    List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
    MultipartFile file = null;
    BufferedOutputStream stream = null;
    for (int i = 0; i < files.size(); ++i) {
      file = files.get(i);
      if (!file.isEmpty()) {
        try {
          byte[] bytes = file.getBytes();
          stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
          stream.write(bytes);
          stream.close();
        } catch (Exception e) {
          stream = null;
          return "You failed to upload " + i + " => " + e.getMessage();
        }
      } else {
        return "You failed to upload " + i + " because the file was empty.";
      }
    }
    return "upload successful";
  }
}

package com.example.configuration;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;

/**
 * 文件上傳配置
 * 
 * @author 單紅宇(CSDN CATOOP)
 * @create 2017年3月11日
 */
public class FileUploadConfiguration {

  @Bean
  public MultipartConfigElement multipartConfigElement() {
    MultipartConfigFactory factory = new MultipartConfigFactory();
    // 設(shè)置文件大小限制 ,超出設(shè)置頁面會拋出異常信息,
    // 這樣在文件上傳的地方就需要進行異常信息的處理了;
    factory.setMaxFileSize("256KB"); // KB,MB
    /// 設(shè)置總上傳數(shù)據(jù)總大小
    factory.setMaxRequestSize("512KB");
    // Sets the directory location where files will be stored.
    // factory.setLocation("路徑地址");
    return factory.createMultipartConfig();
  }
}

@SpringBootApplication
public class DemoUploadfileApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoUploadfileApplication.class, args);
  }
}
<!DOCTYPE html>
<html>
<head>
<title>文件上傳示例</title>
</head>
<body>
  <h2>文件上傳示例</h2>
  <hr/>
  <form method="POST" enctype="multipart/form-data" action="/upload">
    <p>
      文件:<input type="file" name="file" />
    </p>
    <p>
      <input type="submit" value="上傳" />
    </p>
  </form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>批量文件上傳示例</title>
</head>
<body>
  <h2>批量文件上傳示例</h2>
  <hr/>
  <form method="POST" enctype="multipart/form-data"
    action="/upload/batch">
    <p>
      文件1:<input type="file" name="file" />
    </p>
    <p>
      文件2:<input type="file" name="file" />
    </p>
    <p>
      文件3:<input type="file" name="file" />
    </p>
    <p>
      <input type="submit" value="上傳" />
    </p>
  </form>
</body>
</html>

最后啟動服務(wù),訪問 http://localhost:8080/upload 和 http://localhost:8080/upload/batch 測試文件上傳。

Demo源代碼下載地址:uploadfile_jb51.rar

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

相關(guān)文章

  • Windows同時配置兩個jdk環(huán)境變量的操作步驟

    Windows同時配置兩個jdk環(huán)境變量的操作步驟

    Java Development Kit (JDK) 是開發(fā)Java應用程序的基礎(chǔ),包含了編譯器、調(diào)試器以及其他必要的工具,本指南將一步步指導您完成在Windows操作系統(tǒng)上同時配置兩個jdk環(huán)境變量的操作步驟,需要的朋友可以參考下
    2024-09-09
  • SpringMVC接收復雜集合對象(參數(shù))代碼示例

    SpringMVC接收復雜集合對象(參數(shù))代碼示例

    這篇文章主要介紹了SpringMVC接收復雜集合對象(參數(shù))代碼示例,舉接收List<String>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)幾種較為復雜的集合參數(shù),具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 深入理解Java new String()方法

    深入理解Java new String()方法

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著Java new String()展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Spring Bean實例化實現(xiàn)過程解析

    Spring Bean實例化實現(xiàn)過程解析

    這篇文章主要介紹了Spring Bean實例化實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot使用Flyway進行數(shù)據(jù)庫遷移的實現(xiàn)示例

    SpringBoot使用Flyway進行數(shù)據(jù)庫遷移的實現(xiàn)示例

    Flyway是一個數(shù)據(jù)庫遷移工具,它提供遷移歷史和回滾的功能,本文主要介紹了如何使用Flyway來管理Spring Boot應用程序中的SQL數(shù)據(jù)庫架構(gòu),感興趣的可以了解一下
    2023-08-08
  • Spring?Boot?中事務(wù)的用法示例詳解

    Spring?Boot?中事務(wù)的用法示例詳解

    本文詳細介紹了Spring Boot中事務(wù)管理的使用方法,包括事務(wù)的基本概念、配置、傳播行為、隔離級別以及回滾機制,通過使用@Transactional注解,可以方便地實現(xiàn)事務(wù)的控制,文章還討論了事務(wù)方法的可見性、自我調(diào)用問題以及超時設(shè)置等注意事項,感興趣的朋友一起看看吧
    2025-02-02
  • JAVA隨機打亂數(shù)組順序的方法

    JAVA隨機打亂數(shù)組順序的方法

    這篇文章主要介紹了JAVA隨機打亂數(shù)組順序的方法,包含了隨機數(shù)的應用及數(shù)組的排序等操作,是Java操作數(shù)組的典型應用,需要的朋友可以參考下
    2014-11-11
  • Java?Web?Axios實現(xiàn)前后端數(shù)據(jù)異步交互實例代碼

    Java?Web?Axios實現(xiàn)前后端數(shù)據(jù)異步交互實例代碼

    Axios作為一個流行的前端?HTTP?通信庫,可以極大地簡化前端與后端之間的數(shù)據(jù)交互,這篇文章主要介紹了Java?Web?Axios實現(xiàn)前后端數(shù)據(jù)異步交互的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • SpringBoot中并發(fā)定時任務(wù)的實現(xiàn)、動態(tài)定時任務(wù)的實現(xiàn)(看這一篇就夠了)推薦

    SpringBoot中并發(fā)定時任務(wù)的實現(xiàn)、動態(tài)定時任務(wù)的實現(xiàn)(看這一篇就夠了)推薦

    這篇文章主要介紹了SpringBoot并發(fā)定時任務(wù)動態(tài)定時任務(wù)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Java實現(xiàn)定時備份文件

    Java實現(xiàn)定時備份文件

    這篇文章主要為大家詳細介紹了Java實現(xiàn)定時備份文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論

图木舒克市| 巴中市| 绥芬河市| 灵山县| 晋州市| 农安县| 安顺市| 阜新市| 永川市| 横山县| 肇源县| 台州市| 新巴尔虎左旗| 大厂| 特克斯县| 阜康市| 资溪县| 塔河县| 平陆县| 子长县| 嘉禾县| 荔浦县| 依兰县| 泽普县| 涟源市| 东源县| 随州市| 聊城市| 南安市| 麦盖提县| 金川县| 旅游| 遵义县| 石家庄市| 梅州市| 南皮县| 防城港市| 宁河县| 庆元县| 岳普湖县| 高淳县|