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

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è)置強制下載不打開
        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中的connection reset 異常處理分析

    java中的connection reset 異常處理分析

    本文主要介紹了java中的connection reset 異常處理分析的相關(guān)資料,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • springboot+mybatis-plus實現(xiàn)自動建表的示例

    springboot+mybatis-plus實現(xiàn)自動建表的示例

    本文主要介紹了springboot+mybatis-plus實現(xiàn)自動建表的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • springboot+mybaties項目中掃描不到@mapper注解的解決方法

    springboot+mybaties項目中掃描不到@mapper注解的解決方法

    本文主要介紹了springboot+mybaties項目中掃描不到@mapper注解的解決方法,該報錯表明掃描不到Mapper層,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • Java追加文件內(nèi)容的三種方法實例代碼

    Java追加文件內(nèi)容的三種方法實例代碼

    本篇文章主要介紹了Java追加文件內(nèi)容的三種方法實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • java中mybatis和hibernate的用法總結(jié)

    java中mybatis和hibernate的用法總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于java中mybatis和hibernate的用法總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • 深入理解java動態(tài)代理的兩種實現(xiàn)方式(JDK/Cglib)

    深入理解java動態(tài)代理的兩種實現(xiàn)方式(JDK/Cglib)

    本篇文章主要介紹了java動態(tài)代理的兩種實現(xiàn)方式,詳細(xì)的介紹了JDK和Cglib的實現(xiàn)方法,具有一定的參考價值,有興趣的可以了解一下
    2017-04-04
  • Java ClassCastException異常解決方案

    Java ClassCastException異常解決方案

    這篇文章主要介紹了Java ClassCastException異常解決方案,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java中類加載過程全面解析

    Java中類加載過程全面解析

    這篇文章主要介紹了Java中類加載過程全面解析,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Eclipse配置SVN的幾種方法及使用詳情

    Eclipse配置SVN的幾種方法及使用詳情

    這篇文章主要介紹了Eclipse配置SVN的幾種方法及使用詳情,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • java中Javers?比較兩個類的差異

    java中Javers?比較兩個類的差異

    本文主要介紹了Javers?比較兩個類的差異,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論