将文件(图片,excel表等)上传到文件服务器保存,之后会得到一个保存路径,根据这个路径可以进行文件下载(根据路径下载前端可以实现)。
具体代码如下:
@PostMapping("/upload") public void uploadPhoto(@RequestParam("uploadFile")MultipartFile uploadFile, @RequestParam("aiIdCard")String aiIdCard, HttpServletRequest request) { //定义上传文件存放的路径 String path = request.getSession().getServletContext().getRealPath("/uploadFile/"); System.out.println(path); //定义文件在上传路径中的文件夹名称 File folder = new File(path + aiIdCard); //检测folder是否是文件夹,不是就创建 if (!folder.isDirectory()) { folder.mkdirs(); } //获取文件的原始名称 String oldName = uploadFile.getOriginalFilename(); //oldName.substring(oldName.lastIndexOf(".")) 获取文件的后缀名 //生成新的文件名(下面根据自己需要决定是否使用) //String newName = oldName.substring(oldName.lastIndexOf(".")); //文件保存操作 uploadFile.transferTo(new File(folder, oldName)); //返回保存的url,根据url可以进行文件查看或者下载 String filePath = request.getScheme() + "://" + request.getServerName() + ":" +request.getServerPort() + "/uploadFile/" + aiIdCard + oldName;
}