1、在配置文件中配置
applicationContext-upload.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
< bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- 设置上传文件大小的参数 one of the properties available; the maximum file size in bytes -->
< property name = "maxUploadSize" value = "100000" />
</ bean >
</ beans >
2、在项目中添加 commons-io.jar、commons-fileupload.jar包
或者在maven配置中添加这两个包
< dependency >
< groupId > commons-fileupload </ groupId >
< artifactId > commons-fileupload </ artifactId >
< version > 1.2 </ version >
</ dependency >
< dependency >
< groupId > commons-io </ groupId >
< artifactId > commons-io </ artifactId >
< version > 1.4 </ version >
</ dependency >
3、Controller类中的方法
@RequestMapping ( "/uploadProperties" )
public String uploadProperties( @RequestParam () MultipartFile file)
throws IOException {
if (!file.isEmpty()) {
InputStream in = file.getInputStream();
Properties p= new Properties();
p.load(in);
Enumeration<?> keys = p.propertyNames();
while (keys.hasMoreElements()){
String key = (String) keys.nextElement();
String value = p.getProperty(key);
System. out .println(key + " " + value);
}
}
return "redirect:/goUploadProperties" ;
}
4、html文件中的代码
< form method= "post" action= "$rc.contextPath/uploadProperties" enctype= "multipart/form-data" >
< input type= "file" name= "file" />
< input type= "submit" value= "upload" />
</ form >
参考资料:
上传
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ch13s08.html
http://teddywang.javaeye.com/blog/652089
java解析properties文件
http://shengmo8611.javaeye.com/blog/832516
propertie是文件一般是用来保存键值对数据的比如:
username=xiaoqiang
password=xiaopohai
这样的形式
java解析properties文件首先要有核心对象
Properties p=new Properties();
解析文件首先降文件读取到程序中
FileInputStream fis=new FileInputStream(String filename);
读取p.load(fis);
然后可以使用
p.propertyNames();得到所有的key值(得到的是Enumeration 类型)
p.getProperty(key);可以得到指定的value值
spring中防止重复提交
http://elf8848.javaeye.com/blog/875830
通过redirect:url 防止表单重复提交
return "redirect:/index.jsp";