老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

Springboot實現文件上傳功能

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

成都創新互聯公司是創新、創意、研發型一體的綜合型網站建設公司,自成立以來公司不斷探索創新,始終堅持為客戶提供滿意周到的服務,在本地打下了良好的口碑,在過去的十載時間我們累計服務了上千家以及全國政企客戶,如陽臺護欄等企業單位,完善的項目管理流程,嚴格把控項目進度與質量監控加上過硬的技術實力獲得客戶的一致表揚。

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

 
 org.springframework.boot 
 spring-boot-starter-web 
 1.0.2.RELEASE 
 

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

 
 
File to upload:
Name:

Press here to upload the file!

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

3. 編寫處理這個表單的Controller:

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 :

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文件

 
 
File to upload:
File to upload:
Press here to upload the file!

2. 新增BatchFileUploadController.java文件:

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 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也可以實現文件上傳功能。參考文章:


新聞標題:Springboot實現文件上傳功能
分享網址:http://www.xueling.net.cn/article/gsecoc.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 亚洲成人信息集中地 | 久久久久久久久久综合 | 亚洲国产成人久久精品软件 | 91久国产 | 国产日产久久高清 | 在线色网址 | 国产免费区一区二区三视频免费 | 91chinesevideo永久地址 | 久久久女人与动物群交毛片 | 亚洲欧美日本久久综合网站 | avtom影院入口永久在线 | 一本色道综合久久亚洲精品 | 欧美一区二区在线看 | 国产内射在线激情一区 | 国产99久久久精品 | 少妇人妻真实偷人精品视频 | 99免费看香蕉视频 | 亚洲精品天堂成人片AV在线播放 | 欧美日韩亚洲免费 | 狠狠爱免播放器 | 337P大胆日本欧美人体艺术噜噜噜 | 99久热re在线精品99re8热视频 | 久久久久久久中文 | 精品国品一二三产品区别在线观看 | 粉嫩metart女人下部 | 亚洲精品美女久久久久久久 | 国产精品一区二区三区观看 | 国产成人精品手机在线观看 | 久久久久九九 | 久久久国产精品麻豆 | 欧美男女日b视频 | www.日韩在线观看 | 亚洲国产精品网 | 日韩在线视频免费观看 | 午夜精品一区二区三区在线视 | 亚洲高清国产精品 | 国产一区国产二区国产三区 | 成全视频在线观看免费高清 | 91在线一区二区 | 丰满人妻一区二区三区av猛交 | ktv做爰视频一区二区 |