本文主要介绍在SpringMVC环境下实现文件上传的方式,是对自己工作中的经验的一点总结,第一次写博文,还请大家多多包涵。
环境篇
本次使用的环境是Maven+SpringMVC+MyBatis+Postgresql,因为只是个单纯的文件上传,本文并没有涉及数据库,MyBatis+Postgresql方面不会涉及。
Maven
Maven环境的搭建此处不再赘述,其他博文都有很详细的说明,这里主要说明一下在SpringMVC中需要添加的依赖,如下所示。
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
这里需要注意的一点是,commons-fileupload这个jar包的1.0版本是包含commons-io这个jar包的,因此,如果使用的是commons-fileupload的1.0版本,不用引用commons-io的jar包
SpringMVC
SpringMVC的具体搭建过程此处也不再过多赘述,主要说明一下为了实现文件上传所需要加的配置。
为了实现文件上传,需要在springContext.xml中加入如下的内容
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
这里加的bean主要是问了接收页面传过来的文件对象,在这个配置里面设置了上传文件最大值以及文字编码。
代码篇
本篇主要介绍文件上传这个功能的从后台到前台的代码实现,将会在下面的篇幅中一次介绍。
JSP
首先来看JSP上的实现,具体代码如下。
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>ID:</td>
<td><input type="text" name="id"></td>
</tr>
<c:forEach items ="${bean.fileList}" var = "file" varStatus="status">
<tr>
<td>filePath"${status.index}":</td>
<td><input type="file" name="fileList[${status.index}]"></td>
</tr>
</c:forEach>
<tr>
<td colspan="2"><input type="submit" value="insert"></td>
</tr>
</table>
</form>
这里只贴出了form表单部分,其他部分没有贴出。
JSP中需要着重注意的一点是,form标签中的enctype属性必不可少,这个属性如果不添加的话,后台只能接收到文件名,具体文件内容是接收不到的。
Controller
下面将贴出Controller类的代码。
@RequestMapping("/fileupload")
public ModelAndView showMessage() {
//设置页面现实的内容,此部分内容可以从数据库读取,此处是随便设置的值
FileDataContext bean = new FileDataContext();
bean.setId("testId");
List<CommonsMultipartFile> tempList = new ArrayList<CommonsMultipartFile>();
for (int i = 0; i < 2; i++) {
tempList.add(null);
}
bean.setFileList(tempList);
ModelAndView mv = new ModelAndView("fileupload");// 指定视图
// 向视图中添加所要展示或使用的内容,将在页面中使用
mv.addObject("bean", bean);
return mv;
}
@RequestMapping("/upload")
public ModelAndView uploadFile(HttpServletRequest request, FileDataContext bean) {
//这部分的控制台输出是DEBUG用的,实际代码中可不写
System.out.println(bean.getId());
System.out.println(bean.getFileList().get(0).getOriginalFilename());
System.out.println(request.getCharacterEncoding());
File tempFile;
//这部分循环是循环上传文件至服务器
try {
for (int i = 0; i < bean.getFileList().size(); i++) {
//上传的服务器地址
String path = "/Users/skylia/Downloads/" + new Date().getTime()
+ bean.getFileList().get(i).getOriginalFilename();
//生成新文件
tempFile = new File(path);
//上传文件数据
bean.getFileList().get(i).transferTo(tempFile);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//指定视图
ModelAndView mv = new ModelAndView("fileupload");
//添加视图上所要显示的内容
mv.addObject("bean", bean);
return mv;
}
这部分的Controller类的代码中,我将分别说明这两个方法
第一个方法showMessage
这个方法是进入这个页面时所调用的方法,主要是设置初期表示时,想要在页面中显示的值,这个值可以从前一个页面传过来,也可以从数据库读取,请根据自己的需要修改值的类型和来源。
第二个方法uploadFile
这个方法实现了文件上传,在DataBean中接收文件时需要使用multipart提供的文件类。
实际上,实现文件上传的方法有三种。
1.采用流的方式上传文件
2.采用multipart提供的file.transfer方法上传文件
3.使用spring mvc提供的类的方法上传文件
此处使用的是第二种方法,因为第二种方法经测试是效率最高的。
DataBean
下面贴出DataBean的相关代码
public class FileDataContext {
//普通类型的值
private String id;
//文件类型的值
private List<CommonsMultipartFile> fileList;
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<CommonsMultipartFile> getFileList() {
return fileList;
}
public void setFileList(List<CommonsMultipartFile> fileList) {
this.fileList = fileList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
这个Bean是用来承接页面内容的,在这里需要注意的一点是CommonsMultipartFile这个类型。
这个类型是multipart提供的用来承接文件数据的类,请务必将此类型的参数对应到jsp页面上的file输入框(即type=file的input标签)。这样的话就可以收到页面上传来的客户端的文件数据了。
结束语
至此,文件上传的功能就实现了,大家可以根据自己的需要,再添加更新数据库等功能,谢谢阅读!
希望大家可以共同进步,如果有什么写的错误的地方,请指正,谢谢!