学习SpringMVC(二十二)之文件上传

来源:互联网 发布:网络爬虫工具下载 编辑:程序博客网 时间:2024/06/06 05:44

文件上传的步骤:

1.在index.jsp中写一个表单:

<span style="font-family:SimSun;font-size:24px;"> <form action="springmvc/testFileUpload" method="POST" <span style="color:#ff0000;">enctype="multipart/form-data"></span>File: <input type="file" name="file"/>Desc: <input type="text" name="desc"/><input type="submit" value="Submit"/></form></span>

2.写一个controller:

<span style="font-family:SimSun;font-size:24px;">package com.cgf.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;@RequestMapping(value="/springmvc")@Controllerpublic class MyTestFileUpload {@RequestMapping(value="/testFileUpload")public String testFileUpload(<span style="color:#ff0000;">@RequestParam(value="file")MultipartFile file,</span>@RequestParam(value="desc")String desc){System.out.println(desc);System.out.println("file name:"+file.getOriginalFilename());return "success";}}</span>
3.在springmvc.xml中配置CommonsMultipartResolver

<span style="font-family:SimSun;font-size:24px;"><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="UTF-8"></property><property name="maxUploadSize" value="1024000"></property></bean></span>



0 0
原创粉丝点击