1.页面的form表单首先必须是method="post"提交, 其次: enctype="multipart/form-data"
<form action="<%=ContextPath() %>/product/add.do" method="post" enctype="multipart/form-data">
<table align="center" border="1">
<tr>
<td>附件1:</td>
<td>
<input type="file" name="files" >
</td>
</tr>
<tr>
<td>附件2:</td>
<td>
<input type="file" name="files" >
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
<input type="reset" value="重置">
</td>
</tr>
</table>
2.页面传过来的file文件域的name必须与Action接收的一致,
3.Action设特定的参数类型必须是@RequestParam MultipartFile[]类型
4.在上传文件的新文件名创建好之后,用multipartFile的transferTo方法把上传的文件写到指定的位置
@RequestMapping("/product/add.do")
public String add(Product product,@RequestParam MultipartFile[] files){
System.out.Pname()+"\t"+Price());
try {
for (MultipartFile multipartFile : files) {
System.out.OriginalFilename());
String newFileName="f:/uploadfiles/"+UUID.randomUUID().toString()+"_"+OriginalFilename();
ansferTo(new File(newFileName));
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
inputtypefile不上传文件 e.printStackTrace();
}
//service.add(product);
return "redirect:/product/getList.do";
}
5.在l中配好配置文件的解析和设定上传文件的最大大小
<bean id="multipartResolver"
class="org.springframework.web.multipartmons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000000"/>
</bean>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论