MrSaber 通过本文主要向大家介绍了springmvc登录实例,springmvc项目实例,springmvc注解实例,springmvc实例,springmvc简单实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
说明:
文件上传的途径
文件上传主要有两种方式:
1.使用Apache Commons FileUpload元件。
2.利用Servlet3.0及其更高版本的内置支持。
客户端编程
1.为了上传文件,必须将HTML表格的enctype属性值设为multipart/form-data,像下面这样:
<form action="action" enctype="multipart/form-data" method="post"> Select a file<input type="file" name="fieldName"/> <input type="submit" value="Upload"/> </form></div>
2.在HTML5之前,如果要想上传多个文件,必须使用多个文件input元素。但是,在HTML5中,通过在input元素中引入多个multiple属性,使得多个文件的上传变得更加简单,下面均可使一个上传框支持多个文件上传。
<input type="file" name="fieldName" multiple/> <input type="file" name="fieldName" multiple="multiple"/> <input type="file" name="fieldName" multiple=""/></div>
MultipartFile接口
在SpringMVC中处理已经上传的文件十分简单,上传到SpringMVC应用程序中的文件会被包在一个MultipartFile对象中,你唯一要做的事情就是用类型为MultipartFile的属性编写一个Domain类。就像下面这样:
package domain; import org.springframework.web.multipart.MultipartFile; import java.io.Serializable; import java.util.List; public class Product implements Serializable { //实现了这个接口,可以安全的将数据保存到HttpSession中 private long id; private String name; private String description; private String price; //在Domain类中加入MultipartFile类型的属性,用来保存上传的文件 private List<MultipartFile> images; public List<MultipartFile> getImages() { return images; } public void setImages(List<MultipartFile> images) { this.images = images; } ......多个get和set方法。</div>
MultipartFile接口提供了以下方法:
Modifier and Type | Method and Description |
---|---|
byte[] | getBytes()Return the contents of the file as an array of bytes. |
String | getContentType()Return the content type of the file. |
InputStream | getInputStream()Return an InputStream to read the contents of the file from. |
String | getName()Return the name of the parameter in the multipart form. |
String | getOriginalFilename()Return the original filename in the client's filesystem. |
long | getSize()Return the size of the file in by
文章分类 |