国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring boot 實現單個或批量文件上傳功能

Spring boot 實現單個或批量文件上傳功能

2021-05-26 12:18佳佳樂2503 Java教程

這篇文章主要介紹了Spring boot 實現單個或批量文件上傳功能,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧

一:添加依賴:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 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配置文件路徑:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#配置上傳文件地址
image.location.path=f:/image/
#配置文件大小限制
spring.http.multipart.maxfilesize=100mb
spring.http.multipart.maxrequestsize=100mb
#靜態頁面的訪問配置
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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!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層:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
 /**
   * 實現文件上傳
   * */
 @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";// 設置文件名,根據業務需要替換成要下載的文件名
    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 實現單個或批量文件上傳功能

成功返回路徑:

Spring boot 實現單個或批量文件上傳功能

查看文件夾:

Spring boot 實現單個或批量文件上傳功能

總結

以上所述是小編給大家介紹的spring boot 實現單個或批量文件上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/qq_33355858/article/details/81747101

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久综合久久久 | 精品国产网站 | 日韩在线综合 | 国产一级片| 亚洲视频观看 | 青青久久北条麻妃 | 一区二区三区免费播放 | 狠狠操狠狠操 | 人人99| 日韩精品免费一区二区三区 | 狠狠av | 久久久久久国产精品 | 91精品国产综合久久香蕉的用户体验 | 国产精品一区二区在线观看 | 国产精品区二区三区日本 | 欧美国产日韩视频 | 国产资源在线免费观看 | 一级性色 | 欧美日韩一区二区三 | 亚洲大奶网 | 午夜在线观看视频 | 久草视频免费看 | 国产精品久久久久久久久大全 | 色啪网站| 国产片在线免费播放 | 成年免费观看 | 亚洲国产精品成人va在线观看 | 免费一及片 | 欧美福利在线观看 | 午夜影院免费看 | 88tv成人| 国产亚洲精品精品国产亚洲综合 | 日韩激情在线 | 成人羞羞视频在线观看免费 | 精品久久久久久久 | 欧美高清免费 | 亚洲国产日韩一区 | 中文字幕2019 | 国产精品一区三区 | 蜜桃精品久久久久久久免费影院 | 狠狠干av |