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.mzph.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…