SpringMVC中文件上传

文件上传同时也需要用两个jar

commons-io-2.2

链接:https://pan.baidu.com/s/1dafwENwC2f0FzWtwF1pMYQ 
提取码:l6vu 
 

commons-fileupload-1.3.1

链接:https://pan.baidu.com/s/1r3kejVKTPBvGxHThRVKNJg 
提取码:dunu 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

表单数据类型分类

.<form>中的enctype属性控制表单类型

1 默认值 application/x-www-form-urlencoded,普通表单数据.(少量文字信息)

2 text/plain 大量文字使用,比如论文/邮件.

3 multipart/form-data 表单中包含二进制文件内容.

步骤一:JSP页面编写代码:

<form action="upload" enctype="multipart/form-data" method="post"> 
    姓名:<input type="text" name="name"/><br/>
    文件:<input type="file" name="file"/><br/>
    <input type="submit" value=" 提交 "/>
</form>

 

步骤二:配置springmvc.xml

4.3 配置 springmvc.xml
<!-- MultipartResovler 解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!--上传文件最大100KB-->
<property name="maxUploadSize" value="100"></property>
</bean>
<!-- 异常解析器  当上传文件异常时候 跳转到对应页面-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop>
</props>
</property>
</bean>

步骤三:控制器部分的编写代码:

@RequestMapping("upload")
public String upload(MultipartFile file,String name)
throws IOException{
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));//取得文件的后缀 包含.
//判断上传文件类型  .txt类型
if(suffix.equalsIgnoreCase(".txt")){
String uuid = UUID.randomUUID().toString();
//获取流保存到服务器F盘
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("F:/"+uuid+suffix));
return "/index.jsp";
}else{
return "error.jsp";
}
}

文件下载:https://blog.csdn.net/Qve1995/article/details/85326987

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值