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

Spring boot 實現(xiàn)單個或批量文件上傳功能

 更新時間:2018年08月17日 13:48:11   作者:佳佳樂2503  
這篇文章主要介紹了Spring boot 實現(xiàn)單個或批量文件上傳功能,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧

一:添加依賴:

<!-- thymeleaf模板插件 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
   <!-- jsp依賴 -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <!--<scope>provided</scope>-->
</dependency>

二:application.xml配置文件路徑:

#配置上傳文件地址
image.location.path=f:/image/
#配置文件大小限制
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
#靜態(tài)頁面的訪問配置
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5

三:編寫靜態(tài)頁面(src/main/resources下建文件夾static(static存放靜態(tài)文件,比如 css、js、image…)和templates(存放靜態(tài)頁面)兩個是同級目錄),先在templates 中新建一個 uploadimg.html。

<!DOCTYPE html>
<html>
 <head>
  <title>uploadimg.html</title>
  <meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
  <meta name="description" content="this is my page"></meta>
  <meta name="content-type" content="text/html; charset=UTF-8"></meta>
  <!--<link rel="stylesheet" type="text/css" href="./styles.css" rel="external nofollow" >-->
 </head>
 <body>
 <form enctype="multipart/form-data" method="post" action="/dc/fileUpload">
  圖片<input type="file" name="file"/>
  <input type="submit" value="上傳"/>
  </form>
 </body>
</html>

四:編寫Controller層:

package com.hot.analysis.controller.file;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.hot.analysis.exception.MyException;
@RestController
public class FileUploadController {
 //獲取配置文件的路徑
 @Value("${image.location.path}")
 private String resourceDir;
 /**
   * 實現(xiàn)文件上傳
   * */
 @RequestMapping(value = "/index")
 public ModelAndView toIndex() {
 ModelAndView mv = new ModelAndView("uploadimg");
 return mv;
 }
  //單個文件上傳
  @RequestMapping("/dc/fileUpload")
  @ResponseBody 
  public String fileUpload( MultipartFile file){
   // 獲取上傳文件路徑
    String uploadPath = file.getOriginalFilename();
    // 獲取上傳文件的后綴
    String fileSuffix = uploadPath.substring(uploadPath.lastIndexOf(".") + 1, uploadPath.length());
    if (fileSuffix.equals("apk")) {
    uploadPath = resourceDir;
    } else {
    // 上傳目錄地址
    // String uploadpath="E:/hot-manage/image/";//windows路徑
    uploadPath =resourceDir;// liux路勁
    }
    // 上傳文件名
    String fileName = new Date().getTime() + new Random().nextInt(100) + "." + fileSuffix;
    File savefile = new File(uploadPath + fileName);
    if (!savefile.getParentFile().exists()) {
    savefile.getParentFile().mkdirs();
    }
    try {
    file.transferTo(savefile);
    } catch (IllegalStateException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    if (fileSuffix.equals("apk")) {
    return "/apk/" + fileName;
    } else {
    return "/image/" + fileName;
    }
   }
 // 批量上傳
  @PostMapping("/dc/moreFileUpload")
 public String bacthFileUpload(MultipartFile[] file) throws MyException {
  StringBuffer buffer = new StringBuffer();
  for (MultipartFile multipartFile : file) {
  String str = fileUpload(multipartFile);
  buffer.append(str);
  buffer.append(",");
  }
  String all = buffer.substring(0, buffer.length() - 1);
  return all;
 }
 // 刪除文件
  @PostMapping("/dc/deleteFile")
  public String delFile(String path) {
   String resultInfo = null;
 int lastIndexOf = path.lastIndexOf("/");
 String sb = path.substring(lastIndexOf + 1, path.length());
 sb = "f:/image/" + sb;
 File file = new File(sb);
 if (file.exists()) {
  if (file.delete()) {
  resultInfo = "1-刪除成功";
  } else {
  resultInfo = "0-刪除失敗";
  }
 } else {
  resultInfo = "文件不存在!";
 }
 return resultInfo;
 }
 //文件下載相關(guān)代碼
  @RequestMapping("/download")
  public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
    String fileName = "aim_test.txt";// 設(shè)置文件名,根據(jù)業(yè)務(wù)需要替換成要下載的文件名
    if (fileName != null) {
      //設(shè)置文件路徑
      String realPath = "D://aim//";
      File file = new File(realPath , fileName);
      if (file.exists()) {
        response.setContentType("application/force-download");// 設(shè)置強(qiáng)制下載不打開
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 設(shè)置文件名
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
          fis = new FileInputStream(file);
          bis = new BufferedInputStream(fis);
          OutputStream os = response.getOutputStream();
          int i = bis.read(buffer);
          while (i != -1) {
            os.write(buffer, 0, i);
            i = bis.read(buffer);
          }
          System.out.println("success");
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          if (bis != null) {
            try {
              bis.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          if (fis != null) {
            try {
              fis.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }
    return null;
  }

  }

測試:

成功返回路徑:

查看文件夾:

總結(jié)

以上所述是小編給大家介紹的Spring boot 實現(xiàn)單個或批量文件上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java?Collections工具類中常用算法解析

    Java?Collections工具類中常用算法解析

    在軟件開發(fā)中,算法是非常重要的一部分,它們可以提供高效的數(shù)據(jù)處理和操作,這篇文章主要為大家介紹了Collections?工具類集合框架中常用算法,感興趣的可以了解一下
    2023-06-06
  • Spring Security CsrfFilter過濾器用法實例

    Spring Security CsrfFilter過濾器用法實例

    這篇文章主要介紹了Spring Security CsrfFilter過濾器用法實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • Java 深入淺出講解泛型與包裝類

    Java 深入淺出講解泛型與包裝類

    泛型是在Java SE 1.5引入的的新特性,本質(zhì)是參數(shù)化類型,也就是說所操作的數(shù)據(jù)類型被指定為一個參數(shù)。這種參數(shù)類型可以用在類、接口和方法的創(chuàng)建中,分別稱為泛型類、泛型接口、泛型方法,本篇我們一起來學(xué)習(xí)泛型以及包裝類
    2022-04-04
  • mybatis-plus getOne和邏輯刪除問題詳解

    mybatis-plus getOne和邏輯刪除問題詳解

    這篇文章主要介紹了mybatis-plus getOne和邏輯刪除,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 學(xué)習(xí)Java多線程之線程定義、狀態(tài)和屬性

    學(xué)習(xí)Java多線程之線程定義、狀態(tài)和屬性

    這篇文章主要為大家詳細(xì)介紹了Java多線程之線程定義、狀態(tài)和屬性,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 如何使用Jenkins構(gòu)建GIT+Maven項目

    如何使用Jenkins構(gòu)建GIT+Maven項目

    這篇文章主要介紹了如何使用Jenkins構(gòu)建GIT+Maven項目,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java語言實現(xiàn)基數(shù)排序代碼分享

    Java語言實現(xiàn)基數(shù)排序代碼分享

    這篇文章主要介紹了Java語言實現(xiàn)基數(shù)排序代碼分享,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Spring Gateway基本使用示例小結(jié)

    Spring Gateway基本使用示例小結(jié)

    Springcloud Gateway使用了Webflux中的reactor-netty響應(yīng)式編程組件,底層使用了Netty通訊框架,具體一些特征,本文結(jié)合實例代碼對Spring Gateway使用給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2023-11-11
  • List集合多個復(fù)雜字段判斷去重的案例

    List集合多個復(fù)雜字段判斷去重的案例

    今天小編就為大家分享一篇關(guān)于List集合多個復(fù)雜字段判斷去重的案例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 簡單了解java volatile關(guān)鍵字實現(xiàn)的原理

    簡單了解java volatile關(guān)鍵字實現(xiàn)的原理

    這篇文章主要介紹了簡單了解volatile關(guān)鍵字實現(xiàn)的原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08

最新評論

长白| 宁海县| 德安县| 青浦区| 阿拉善盟| 巍山| 镇巴县| 庆阳市| 马公市| 若尔盖县| 阳信县| 廊坊市| 融水| 辽宁省| 藁城市| 甘洛县| 德钦县| 荔波县| 海丰县| 蓝田县| 福州市| 自治县| 宜都市| 西畴县| 荔波县| 内丘县| 东乡县| 错那县| 亚东县| 文山县| 海宁市| 平武县| 西平县| 阜平县| 江油市| 红原县| 河北省| 武安市| 石楼县| 寿阳县| 新巴尔虎右旗|