SpringMVC的文件上传

导包:
在pom.xml中导入FileUpload所需的包:

commons-fileupload commons-fileupload 1.3.3

注:FileUpload需要commons-fileupload-1.3.3.jar和commons-io-2.4.jar两个包,导入FileUpload后两个包都存在。


H5实现选择多个文件:




springMVC的核心配置

Controoler代码
@Autowired
private ServletContext context;
@RequestMapping(value = “/fileUpload”,method = RequestMethod.POST)

public String upload(@RequestParam("fieldName")  MultipartFile fieldName,Model model,HttpServletRequest request) throws  Exception{

// 判断文件是否为空
if(!fieldName.isEmpty()){
//获取源文件名
String filename = fieldName.getOriginalFilename();
//拼路径
String filePath =context.getRealPath("")+“upload”+ File.separator;

//判断路径是否存在
File file = new File(filePath);
if(!file.exists()){
file.mkdir();
}
//截取后缀名
String lastName=filename.substring(filename.lastIndexOf("."));
//设置时间戳
//拼接完整的路径
filePath = filePath + System.currentTimeMillis() +lastName;
System.out.println(filePath);
//上传
fieldName.transferTo(new File(filePath));
model.addAttribute(“filePath”,filePath);
}
return “kk.jsp”;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值