文件上傳多地保存問題

@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)

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