导入jar包
commons-fileupload.jar
commons-io.jar
springmvc.xml中配置
<!-- 配置CommonsMultipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="100000"></property>
</bean>
前端页面
<form action="testFileupload" method="post" enctype="multipart/form-data">
File:<input type="file" name="file"/><br>
Desc:<input type="text" name="desc"/><br>
<button type="submit">提交</button>
</form>
目标方法
@RequestMapping("/testFileupload")
public String testFileupload(
@RequestParam("desc") String desc
,@RequestParam("file") MultipartFile file){
System.out.println("目标文件名:"+desc);
System.out.println("源文件名:"+file.getOriginalFilename());
System.out.println("组件名:"+file.getName());
try {
System.out.println("文件字节流:"+file.getInputStream());
System.out.println("文件大小(字节):"+file.getSize());
} catch (IOException e) {
e.printStackTrace();
}
return "successs";
}
打印结果
目标文件名:descFile
源文件名:dd.txt
组件名:file
文件字节流:java.io.ByteArrayInputStream@3b760980
文件大小(字节):20