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

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

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

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

Spring boot實現文件上傳功能

2021-05-11 15:28卡通人物nnn Java教程

這篇文章主要為大家詳細介紹了Spring boot實現文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了spring boot實現文件上傳的具體代碼,供大家參考,具體內容如下

1. 創建一個maven的web工程,然后配置pom.xml文件,增加依賴:

?
1
2
3
4
5
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
 <version>1.0.2.release</version>
</dependency>

2. 在webapp目錄下的index.jsp文件中輸入一個表單:

?
1
2
3
4
5
6
7
8
9
<html>
<body>
<form method="post" enctype="multipart/form-data" action="/upload">
 file to upload: <input type="file" name="file"><br />
 name: <input type="text" name="name"><br /> <br />
 <input type="submit" value="upload">
  press here to upload the file!
</form>
</body>

這個表單就是我們模擬的上傳頁面

3. 編寫處理這個表單的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
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileoutputstream;
 
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;
 
@controller
public class fileuploadcontroller {
 
 @requestmapping(value="/upload", method=requestmethod.get)
 public @responsebody string provideuploadinfo() {
  return "you can upload a file by posting to this same url.";
 }
 
 @requestmapping(value="/upload", method=requestmethod.post)
 public @responsebody string handlefileupload(@requestparam("name") string name,
   @requestparam("file") multipartfile file){
  if (!file.isempty()) {
   try {
    byte[] bytes = file.getbytes();
    bufferedoutputstream stream =
      new bufferedoutputstream(new fileoutputstream(new file(name + "-uploaded")));
    stream.write(bytes);
    stream.close();
    return "you successfully uploaded " + name + " into " + name + "-uploaded !";
   } catch (exception e) {
    return "you failed to upload " + name + " => " + e.getmessage();
   }
  } else {
   return "you failed to upload " + name + " because the file was empty.";
  }
 }
 
}

4. 然后我們對上傳的文件做一些限制,同時編寫main方法來啟動這個web :

?
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
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.enableautoconfiguration;
import org.springframework.boot.context.embedded.multipartconfigfactory;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
 
import javax.servlet.multipartconfigelement;
 
@configuration
@componentscan
@enableautoconfiguration
public class application {
 
 @bean
 public multipartconfigelement multipartconfigelement() {
  multipartconfigfactory factory = new multipartconfigfactory();
  factory.setmaxfilesize("128kb");
  factory.setmaxrequestsize("128kb");
  return factory.createmultipartconfig();
 }
 
 public static void main(string[] args) {
  springapplication.run(application.class, args);
 }
}

5. 然后訪問http://localhost:8080/upload就可以看到頁面了。

上面的例子是實現的是單個文件上傳的功能,假定我們現在要實現文件批量上傳的功能的話,我們只需要簡單的修改一下上面的代碼就行,考慮到篇幅的問題,下面只是貼出和上面不同的代碼,沒有貼出的說明和上面一樣。:

1.新增batchupload.jsp文件

?
1
2
3
4
5
6
7
8
9
10
<html>
<body>
<form method="post" enctype="multipart/form-data"
  action="/batch/upload">
 file to upload: <input type="file" name="file"><br />
 file to upload: <input type="file" name="file"><br />
 <input type="submit" value="upload"> press here to upload the file!
</form>
</body>
</html>

2. 新增batchfileuploadcontroller.java文件:

?
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
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.responsebody;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.multipart.multiparthttpservletrequest;
 
import javax.servlet.http.httpservletrequest;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.util.list;
 
/**
 * created by wenchao.ren on 2014/4/26.
 */
 
@controller
public class batchfileuploadcontroller {
 
 @requestmapping(value="/batch/upload", method= requestmethod.post)
 public @responsebody
 string handlefileupload(httpservletrequest request){
  list<multipartfile> files = ((multiparthttpservletrequest)request).getfiles("file");
  for (int i =0; i< files.size(); ++i) {
   multipartfile file = files.get(i);
   string name = file.getname();
   if (!file.isempty()) {
    try {
     byte[] bytes = file.getbytes();
     bufferedoutputstream stream =
       new bufferedoutputstream(new fileoutputstream(new file(name + i)));
     stream.write(bytes);
     stream.close();
    } catch (exception e) {
     return "you failed to upload " + name + " => " + e.getmessage();
    }
   } else {
    return "you failed to upload " + name + " because the file was empty.";
   }
  }
  return "upload successful";
 }
}

這樣一個簡單的批量上傳文件的功能就ok了,是不是很簡單啊。

注意:上面的代碼只是為了演示而已,所以編碼風格上采取了隨性的方式,不建議大家模仿。

參考資料:multipartresolver實現文件上傳功能

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

1. multipartresolver也可以實現文件上傳功能。參考文章:原文鏈接:https://blog.csdn.net/woshizhangliang999/article/details/46711747

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线观看一区 | 日韩欧美在线观看视频 | 色综合天天天天做夜夜夜夜做 | 91av电影网 | 国产精品免费一区二区三区 | 日韩视频免费 | 亚洲欧洲精品视频 | 亚洲成人精品久久久 | 亚洲视频一区二区三区在线观看 | 韩日精品视频 | 亚洲国产区| 激情欧美一区二区三区中文字幕 | 免费在线成人网 | 亚洲第一成人在线视频 | 免费在线观看一区二区 | 黄色网页大全 | 亚洲va中文字幕 | av中文在线 | 国产精品区二区三区日本 | 亚洲一区二区三区免费观看 | 91最新网址 | 免费成人在线观看 | 在线一二三区 | 国产中文字幕一区 | 国产视频精品免费 | 欧美日韩一区二区在线观看 | 综合久久亚洲 | 亚洲第十页 | 久久久久久综合 | 久久国产精品亚洲 | 午夜电影网址 | 日本一区二区三区免费观看 | 91亚洲日本aⅴ精品一区二区 | 亚洲精品国产一区 | 玖玖视频| 日日日日干干干干 | 久久一区 | 中文字幕第一页在线 | 成年人免费在线观看网站 | 伊人欧美一区 | 成人免费在线电影 |