基本上 通用的 文件 上传 前台 通过 from 表单 进行提交
前台提交代码
<form action="/zxsy/Upload" enctype="multipart/form-data" method="post">
<input type="file" name="file" id="file"/>
</form>
后台代码
/**
* 基于文件上传 xlsx 并且文件上传到 E:/content/xlsx
* @auther wangmx
* @param file
* @param entity
* @return 跳转到 save
*/
@RequestMapping("/Upload")
public String imgUpload(@RequestParam("file")MultipartFile file,OnlineExperimentApply entity){
String newname="";
String path="";
if(file!=null){
String name = file.getOriginalFilename();
newname = System.currentTimeMillis()+file.getOriginalFilename();
path = "E:/content/xlsx" ;
File aaa= new File("E:/content/xlsx");
if(!aaa.exists()){//文件夹不存在创建 存在直接用
aaa.mkdir();
}
try {
file.transferTo(new File(path,newname));
entity.setExperimentResourcesFile(path+"/"+newname);//把 新的名字 存储到 实体中
/*return path+newname;*/
return "forward:/zxsy/save?ExperimentResourcesFile="+path+"/"+newname;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return "redirect:/zxsy/save";
}