欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

一:添加依賴:

為來賓等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及來賓網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站建設、網(wǎng)站設計、來賓網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

<!-- 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;
 }
 //文件下載相關代碼
  @RequestMapping("/download")
  public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
    String fileName = "aim_test.txt";// 設置文件名,根據(jù)業(yè)務需要替換成要下載的文件名
    if (fileName != null) {
      //設置文件路徑
      String realPath = "D://aim//";
      File file = new File(realPath , fileName);
      if (file.exists()) {
        response.setContentType("application/force-download");// 設置強制下載不打開
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 設置文件名
        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;
  }

  }

測試:

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

成功返回路徑:

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

查看文件夾:

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

總結

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

分享名稱:Springboot實現(xiàn)單個或批量文件上傳功能
文章來源:http://aaarwkj.com/article44/iidihe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、手機網(wǎng)站建設、云服務器、網(wǎng)站收錄、定制開發(fā)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設
中文字幕在线五月婷婷| 久久99国产精品成人免费| 久久免费欧美日韩亚洲| 中文字幕在线五月婷婷| 亚洲日本国产精品一区| 最新日本欧美一区二区| 久久精品亚洲熟女av蜜謦| 狠狠综爱五月天的婷婷| 久久人妻一区二区三区免费 | 亚洲成人黄色在线网站| 久久日韩精品人妻一区二区| 亚洲码与欧洲码一二三| 亚洲一区二区三区精品在线| 日韩av中文一区二区| 一区二区三区高清人妻| 99热只有这里才有精品| 日韩在线不卡免费视频一区| 国产精品亚洲欧美中字| 国语对白视频在线观看| 日韩欧美国产麻豆91在线精品| 18禁黄久久久一区二区三区| 亚洲精品福利一二三区| 国产一区在线免费在线观看| 成人午夜在线三级内射| 国产剧情av一区在线观看 | 国产91极品尤物白丝美女| 国产精品自拍激情在线观看| 亚洲国产中文一区二区久久| 亚洲男人天堂免费观看| 国产国产成人精品久久蜜| 日韩精品一二区电影| 欧美精品日韩精品一区二区| 97在线视频在线播放| 日韩精品第一区第二区| 亚洲国产精品激情在线| 激情婷婷亚洲五月综合网| 久久亚洲综合色一区二区三区| 99精品国产高清一区二区三区| 国产乱肥老妇国产一区二| 国产精品麻豆一区二区三区| 精品欧美一区二区精品|