springMVC3學習(十一)--文件上傳CommonsMultipartFile

版權聲明:本文為博主原創文章,未經博主同意不得轉載。 https://blog.csdn.net/itmyhome/article/details/27976873

使用springMVC提供的CommonsMultipartFile類進行讀取文件

須要用到上傳文件的兩個jar包 commons-logging.jar、commons-io-xxx.jar


1、在spring配置文件里配置文件上傳解析器

<!-- 文件上傳解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8"></property><property name="maxUploadSize" value="10485760000"></property><!-- 最大上傳文件大小 --><property name="maxInMemorySize" value="10960"></property>
</bean>

2、文件上傳頁面(index.jsp)

<!-- method必須為post 及enctype屬性-->
<form action="fileUpload.do" method="post" enctype="multipart/form-data"><input type="file" name="file"><input type="submit" value="上傳">
</form>

3、FileController類

@Controller
public class FileController{@RequestMapping("/fileUpload.do")public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){long startTime=System.currentTimeMillis();   //獲取開始時間if(!file.isEmpty()){try {//定義輸出流 將文件保存在D盤    file.getOriginalFilename()為獲得文件的名字 FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename());InputStream in = file.getInputStream();int b = 0;while((b=in.read())!=-1){ //讀取文件 os.write(b);}os.flush(); //關閉流 in.close();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}long endTime=System.currentTimeMillis(); //獲取結束時間System.out.println("上傳文件共使用時間:"+(endTime-startTime));return "success";}
}

上傳了一個3.54M的PDF文件 共使用29132毫秒(以自己計算機實際為準)

上面計算了上傳文件所使用時間。目的為了和下篇還有一種上傳方法進行比較 看哪個效率更高


測試URL: ?http://localhost:8080/spring/


項目源代碼下載地址:http://download.csdn.net/detail/itmyhome/7447419






轉載于:https://www.cnblogs.com/ldxsuanfa/p/9972796.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278898.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278898.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278898.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于React和SpringBoot的快速開發模板QuickAdmin

經過一段時間的總結和完善&#xff0c;我的管理系統快速開發模板已經基本成型&#xff0c;現在GitHub上開源啦&#xff1a; QuickAdmin QuickAdmin是基于Spring Boot和React.js實現的管理系統開發框架。用于開發網站的后臺管理系統。 本框架提供了如下功能&#xff1a; 完整的基…

android sim iso,android – 意外的telephonyManager.getSimCountryIso()行為

您可以使用MCC MNC獲取SIM卡國家/地區,它是SIM配置的,與您所在的網絡無關.Configuration config getResources().getConfiguration();int countryCode config.mcc;您可以在此處找到MCC列表MccTable.java例如,西班牙是214,法國是208MCC should work on all GSM devices with S…

火狐 增強查找工具欄_在“提示”框中:簡單的IE至Firefox同步,輕松的Windows工具欄和識別USB電纜...

火狐 增強查找工具欄() Every week we tip into our mail bag and share great tips from your fellow readers. This week we’re looking at an easy way to sync your bookmarks between IE and Firefox, using simple Windows toolbars, and a clever way to ID USB cables…

day22 模塊-collections,time,random,pickle,shelve等

一、引入模塊的方式: 1. 認識模塊 模塊可以認為是一個py文件. 模塊實際上是我們的py文件運行后的名稱空間 導入模塊: 1. 判斷sys.modules中是否已經導入過該模塊 2. 開辟一個內存 3. 在這個內存中執行該py文件 4. 給這個內存起個名字&#xff0c; 一般用的是py文件的名字。返回…

基于Redis實現分布式鎖,避免重復執行定時任務

Spring提供了定時任務的功能&#xff0c;但是在多個實例的集群中&#xff0c;會出現定時任務重復執行多次的情況。 使用Qutaz框架自帶的分布式定時任務可以很好的解決這個問題&#xff0c;但是講道理功能有些過于強大&#xff0c;對于需求不高&#xff0c;乃至可以一定程度上允…

Input Director使用一個鍵盤和鼠標即可控制多臺Windows計算機

The problem is having two or more PC’s and having to go back and forth between workstation. Input Director solves the problem by allowing you to control multiple Windows systems with only one keyboard and mouse on the Master PC. 問題是擁有兩臺或更多臺PC…

viper4android 生效,另一種讓V4a音效在Poweramp上生效的方法

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓本人按照網上的方法進Poweramp設置—音頻—高級選項—直接音量控制—不打勾后 V4a音效沒有生效 我又把V4a音效兼容模式里的正常模式切換成為兼容模式 結果還是不行......后來我郁悶了三天三夜有一天我去了v4a官網論壇看到了admin帖…

[學習筆記]狀壓dp

狀壓 \(dp\) 1、[SDOI2009]Bill的挑戰 \(f[i][j]\) 表示匹配到字符串的第 \(i\) 位狀態為 \(j\) 的方案數 那么方程就很明顯了&#xff0c;每次枚舉第 \(i\) 位的字母 \(alpha\) 然后 \(O(n)\) 判斷就好了 時間復雜度 \(O(26Tlen2^nn)\) \(Code\ Below:\) #include <bits/st…

excel導入csv文件_如何將包含以0開頭的列的CSV文件導入Excel

excel導入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel將在打開…

MySQL之進化篇

MySQL之實用篇 MySQL之牛刀小試 子查詢是指出現在其他SQL語句內的SELECT子句. 例如: SELECT * FROM t1 WHERE column1 (SELECT column2 FROM t2) 其中 SELECT * FRIN t1 稱為outerQuery SELECT column2 FROM t2 稱為subQuery 注意:子查詢指嵌套在查詢內部,且必須始終出現在圓括…

android 9.0新ui,SystemUI分析(Android9.0)

8種機械鍵盤軸體對比本人程序員&#xff0c;要買一個寫代碼的鍵盤&#xff0c;請問紅軸和茶軸怎么選&#xff1f;一、SystemUI組成SystemUI是Android的系統界面&#xff0c;包括狀態欄statusbar、鎖屏keyboard、任務列表recents等等&#xff0c;都繼承于SystemUI這個類&#xf…

WMI技術介紹和應用——WMI概述

https://blog.csdn.net/breaksoftware/article/details/8424317轉載于:https://www.cnblogs.com/diyunpeng/p/9982885.html

解決App啟動時白屏的問題

第一次 03-25 11:02:34.431 6908-6908/com.newenergyjinfu.jytz D/App: before_onCreate: 239 03-25 11:02:34.513 6908-6908/com.newenergyjinfu.jytz D/App: after_initOkGo( initPicasso): 316 03-25 11:02:34.570 6908-6908/com.newenergyjinfu.jytz D/App: after_ J…

chromebook刷機_如何為不支持Chrome操作系統的網站欺騙Chromebook用戶代理

chromebook刷機Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

什么時候可以升級HarmonyOS,華為鴻蒙OS即將迎來升級 手機版本或仍需時間

原標題&#xff1a;華為鴻蒙OS即將迎來升級 手機版本或仍需時間在2019年的華為開發者大會上&#xff0c;華為消費者業務CEO余承東正式對外發布了HarmonyOS。時隔一年后&#xff0c;華為開發者大會2020即將拉開帷幕。此次大會&#xff0c;HarmonyOS無疑仍會是重頭戲之一&#xf…

Shell_mysql命令以及將數據導入Mysql數據庫

連接MYSQL數據庫 mysql -h${db_ip} -u${db_user} -p${db_pawd} -P${db_port} -D${db_name} -s -e "${sql}" db_ip&#xff1a;主機地址 db_user &#xff1a;數據庫用戶名 db_pwd&#xff1a;密碼 db_port&#xff1a;端口號 db_name&#xff1a;數據庫名稱 sql&…

cocos android-1,cocos2dx在windows下開發,編譯到android上(1)

轉自&#xff1a;http://www.2cto.com/kf/201205/130697.html下面我給大家介紹下&#xff0c;用vs2010開發cocos2dx&#xff0c;然后如何使其編譯到android上。步驟如下&#xff1a;1、必要條件&#xff0c;你的eclipse能把代碼編譯到安卓手機或虛擬機上&#xff0c;如果這一步…

中藥ppi網絡圖太雜亂_太雜亂了嗎? 這是您的iPhone,iPad,Android或臺式機的15張簡約壁紙...

中藥ppi網絡圖太雜亂Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墻紙圖像在iPhon…

算法61---兩個字符串的最小ASCII刪除和【動態規劃】

一、題目&#xff1a; 給定兩個字符串s1, s2&#xff0c;找到使兩個字符串相等所需刪除字符的ASCII值的最小和。 示例 1: 輸入: s1 "sea", s2 "eat" 輸出: 231 解釋: 在 "sea" 中刪除 "s" 并將 "s" 的值(115)加入總和。 在…

android設置時間widget,【Android】時間與日期Widget(DatePicker 與 TimePicker)

public class Activity01 extends Activity{TextViewm_TextView;//聲明dataPickerDatePickerm_DatePicker;//聲明TimePickerTimePickerm_TimePicker;Button m_dpButton;Button m_tpButton;//java中的Calendar類Calendar c;/** Called when the activity is first created. */Ov…