package cn.it.entity;
import java.io.Serializable;
import org.springframework.web.multipart.MultipartFile;
public class Student implements Serializable {
private String stuName;
private String stuPass;
private MultipartFile [] files;
public MultipartFile[] getFiles() {
return files;
}
public void setFiles(MultipartFile[] files) {
this.files = files;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuPass() {
return stuPass;
}
public void setStuPass(String stuPass) {
this.stuPass = stuPass;
}
@Override
public String toString() {
return "Student [stuName=" + stuName + ", stuPass=" + stuPass + "]";
}
}
package cn.it.utils;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FileUploadUtils {
/*
* 注入字符串,#{}为spel语言,其中uploadProperties,是xml配置文件中注入properties文件的bean id,
* path为properties文件的其中一个key ,也可以通过下边的set方法注入
*/
@Value("#{uploadProperties.path}")
private String path;
//获取文件的扩展名
private String getExtName(MultipartFile file){
return FilenameUtils.getExtension(file.getOriginalFilename());
}
//创建文件的新名
private String createNewName(MultipartFile file){
return UUID.randomUUID().toString()+"."+getExtName(file);
}
//
public String upload(MultipartFile file){
String newName = null;
try {
newName =createNewName(file);
System.out.println("path:"+path);
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path+newName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
newName = null;
}
return newName;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<context:component-scan base-package="*"></context:component-scan>
-->
<!-- mvc:annotation-driven,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
两个Bean的配置 -->
<!-- 基于上面的示例,在spring3中可以进一步简化配置,取代上面的注解方式.
步骤如下
1.使用上面的action类,仍然给类及方法添加@Controller(类)、@RequestMapping(类及方法)注解
2.本文件顶部添加spring mvc 命名空间的信息(可以参考org.springframework.web.servlet.config包)
3.添加下面注解驱动<mvc:annotation-driven>,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter,
并启动了json的注解支持
-->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="*"/>
<!--文件上传使用, 配置multipartResolver,id名为约定好的 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件(每次上传的所有文件总大小)大小,单位为b, 1024000表示1000kb -->
<property name="maxUploadSize" value="102400000" />
</bean>
<!--PropertiesFactoryBean对properties文件可用 ,可以用来注入properties配置文件的信息 -->
<bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:upload.properties"></property>
</bean>
</beans>
<form action="student/add.action" method="post" enctype="multipart/form-data">
学生姓名:<input type="text" name="stuName"><br>
密码:<input type="text" name="stuPass"><br>
请选择文件:<input type="file" name="files"><br>
<input type="file" name="files"><br>
<input type="submit" value="add">
</form><hr>
<a href="student/add.action?stuName=吴文亮">测试get方式</a>
</body>
</html>
原文:http://blog.51cto.com/357712148/2141116