在前台写一个类型为file的按钮,点击按钮选择要上传的文件。
//文件上传
if(file != null && file.getOriginalFilename() !=null && file.getOriginalFilename().length()>0) {
String filepath=request.getSession().getServletContext().getRealPath("/");
String pic_path = filepath+"\\pic_file\\";
String wjmc = file.getOriginalFilename();
//扩展名
String extendsName = wjmc.substring(wjmc.lastIndexOf("."));
//文件名使用UUID是避免文件名重复
String newFilename = UUID.randomUUID().toString() + extendsName;
//文件
File uploadFile = new File(pic_path + newFilename);
file.transferTo(uploadFile);
model.addAttribute("filepath", "/file/" + newFilename);
}