SpringBoot实现图片上传

图片上传:

NewsController
@Controller
@RequestParam("file")MultipartFile file

file.getOriginalFilename().lastIndexOf(".");

String fileName = UUID.randomUUID().toString().replaaceAll("-","")+"."+fileExt;
Files.copy(file.getInputStream(),new File(ToutiaoUtil.IMAGE_DIR+fileName).toPath().
	StandardCopyOption.REPLACE_EXISTING);

图片下载:

public void getImage(@RequestParam("name")String imageName,HttpServletResponse response){
	try{
		response.setContentType("image/jpeg");
		StremUtil.copy(new FileInputStream(new File(ToutiaoUtil.IMAGE_DIR+imageName)),
		response.getOutputStream());
	}catch(Exception e){
		logger.error("读取图片错误"+e.getMessage());
	}
}

你可能感兴趣的:(中间件框架)