//前端图片是Base64字符串形式传递图片参数;需要用Base解密,写入到本地磁盘中
public String upload(String string){

	解析图片(Base64):
	response.setHeader("Access-Control-Allow-Origin","*");  // 第二个参数填写允许跨域的域名称,不建议直接写 "*"
	response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
	response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");

	// 接收跨域的cookie
	response.setHeader("Access-Control-Allow-Credentials", "true");
	url = url.substring(url.indexOf(",")+1,url.length());
	String imagePath = null;
	BASE64Decoder decoder = new BASE64Decoder();
	String strName = null;
	String string2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
	System.out.println(string2);
	try {
		byte[] bytes = decoder.decodeBuffer(url);
		// 处理数据
		for (int i = 0; i < bytes.length; ++i) {
			if (bytes[i] < 0) {
				bytes[i] += 256;
			}
		}
		strName = UUID.randomUUID().toString();
		System.out.println(strName);
		imagePath = "E:/javaworkspace/project/rbApi/image/"+strName+".png";
		OutputStream out = new FileOutputStream(imagePath);
		out.write(bytes);
		out.flush();
		out.close();
	}catch (Exception e){
		e.printStackTrace();
	}
}
//springboot项目
//以@RequestParam("url") List<MultipartFile> url接收上传图片;写入到本地磁盘中
//返回路径是磁盘路径,将磁盘路径映射到外部静态资源;访问:项目路径+映射路径+文件名
@RequestMapping(value = "/uploadImage")
@ResponseBody
public PictureUrl upload(@RequestParam("url") List<MultipartFile> url, HttpServletRequest request, HttpServletResponse response){
	PictureUrl pictureUrl = new PictureUrl();
	String contentPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
	try{
		if(url.size() == 0){
			pictureUrl.setStatusText("false");
		}else{
			OutputStream os = null;
			List<String> urlList = new ArrayList<>();
			for (MultipartFile file : url){
				String fileName = file.getOriginalFilename();
				String fileNam2 = UUID.randomUUID().toString();
				String imagePath = "E:/javaworkspace/project/rbApi/image/"+fileNam2+".png";
				File file1 = new File(imagePath);
				os = new FileOutputStream(file1);
				os.write(file.getBytes());
				//String imagePath2 = contentPath + "/showImage?id=" + fileNam2+".png";
				//String imagePath2 = upload + fileNam2+".png";
				String imagePath2 = contentPath+"/image/"+fileNam2+".png";
				urlList.add(imagePath2);
				System.out.println("文件路径:"+imagePath2);
			}
			pictureUrl.setStatusText("ok");
			pictureUrl.setImageUrl(urlList);
			os.flush();
			os.close();
		}

	}catch (Exception e){
		e.printStackTrace();
		pictureUrl.setStatusText("false");
		return pictureUrl;
	}
	
	return pictureUrl;

}
//将图片以二进制的形式输出,前端路径可为:(项目路径+方法+文件名)
 /**
 * 回显图片
 * @param id
 * @param response
 * @return
 */
@RequestMapping("/showImage")
@ResponseBody
public AjaxResult download(String id, HttpServletResponse response){
	try{
		response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
		String path ="E:/javaworkspace/project/rbApi/image/"+id;
		File file = new File(path);//括号里参数为文件图片路径
		if(file.exists()){   //如果文件存在
			InputStream in = new FileInputStream(path);   //用该文件创建一个输入流
			OutputStream os = response.getOutputStream();  //创建输出流
			byte[] b = new byte[1024];
			while( in.read(b)!= -1){
				os.write(b);
			}
			in.close();
			os.flush();
			os.close();
		}
		return null;
	}catch (Exception e){
		e.printStackTrace();
		return AjaxResult.build("false",null );
	}
}

//将磁盘文件路径映射为项目访问路径
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").addResourceLocations("file:E:/javaworkspace/project/rbApi/image/");
    }
}

  得到

posted on 2019-07-03 15:51  lazyli  阅读(5065)  评论(0编辑  收藏  举报