文件上传多地保存问题

@RequestMapping(value="/insertGoodsPicture")
	@ResponseBody
	public int insertGoodsPicture(@RequestParam("file")MultipartFile file, String goodsDetailId, HttpServletRequest request) {
		String str=file.getOriginalFilename();
		String filename = goodsDetailId+str.substring(str.lastIndexOf("."), str.length());
		if (file.isEmpty()) {
			return 400;
		} else {
			try {
				file.transferTo(new File("/tomcat/fcserver/webapps/miMall/images/goodsPicture/"+filename));
				Files.copy(new File("/tomcat/fcserver/webapps/miMall/images/goodsPicture/"+filename).toPath(),new File("/tomcat/fcserver/webapps/backssm/mimall/images/goodsPicture/"+filename).toPath());
				goodsPictureService.insertGoodsPicture(new GoodsDetailPicture(null,Long.valueOf(goodsDetailId),"mimall/images/goodsPicture/"+filename));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
				return 400;
			}
		}
		return 200;
	}

最开始我使用file.transferTo执行两次进行多地保存,发现存在第二个保存失败的问题,了解发现file.transferTo()是转换,转换完成之后file不存在了,所以需要使用文件复制来解决这个问题,引入了Files.copy(sourcePath,targetPath)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章