base64字符串转图片

base64字符串转图片

Posted by wenfengSAT on November 15, 2018

base64字符串转图片

  • 代码


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;


  @Autowired
  private MongoFsDao mongoFsDao;

  @Autowired
  private GridFsTemplate gridFsTemplate;

/**
   * base64转文件
   * @param base64
   * @return
   */
  public String base64ToFile(String base64) {
	  if(StringUtils.isBlank(base64)) {
		  return null;
	  }
	  base64 = base64.substring(base64.indexOf(",")+1);
	  byte[] bytes = Base64.decodeBase64(base64);
	  String type = "picture";
      Map<String, String> params = new HashMap<>();
      params.put("type", type);
      InputStream inputStream = new ByteArrayInputStream(bytes);
      try {
		String url = (String) mongoFsDao.insert(params, inputStream);
		return url;
		} catch (Exception e) {
			logger.error("=============MongoDB存储图片失败==================");
			return null;
		}
	}


  • 工具类代码


public class MongoFsDao {

  private String imgPrefix;

  private GridFsTemplate gridFsTemplate;

  public MongoFsDao(String imgPrefix, GridFsTemplate gridFsTemplate) {
    this.imgPrefix = imgPrefix;
    this.gridFsTemplate = gridFsTemplate;
  }

  /**
   * 图片存储(内容类型为image或PNG)
   *
   * @param params params的内容中一定要有fileName,其他的可以自定义传值
   * @param image
   * @return
   */
  public Object insert(Map params, InputStream image) throws Exception {
    String filename = RegexUtil.getId().toString();
    DBObject metadata = new BasicDBObject(params);
    gridFsTemplate.store(image, filename, "image/png", metadata);
    return imgPrefix + filename;
  }

  /*
  用户关爱音频存储
   */
  public Object insertAudio(Map params, InputStream audio) throws Exception {
    String filename = RegexUtil.getId().toString();
//    String name = (String) params.get("fileName");
//    String back = name.substring(name.lastIndexOf("."));
    String back = ".mp3";
    DBObject metadata = new BasicDBObject(params);
    gridFsTemplate.store(audio, filename + back, "mp3/wav", metadata);
    return imgPrefix + filename + back;
  }

  /*
  用户关爱图片存储
 */
  public Object insertImg(Map params, InputStream audio) throws Exception {
    String filename = RegexUtil.getId().toString();
//    String name = (String) params.get("fileName");
//    String back = name.substring(name.lastIndexOf("."));
    String back = ".jpg";
    DBObject metadata = new BasicDBObject(params);
    gridFsTemplate.store(audio, filename + back, "image/png", metadata);
    return imgPrefix + filename + back;
  }

  /**
   * 其他类型文件上传.
   *
   * @param params
   * @param contentType
   * @param audio
   * @return
   * @throws Exception
   */
  public Object insert(Map params, String contentType, InputStream audio) throws Exception {
    String filename = RegexUtil.getId().toString();
    DBObject metadata = new BasicDBObject(params);
    gridFsTemplate.store(audio, filename, contentType, metadata);
    return imgPrefix + filename;
  }

  /**
   * 图片查询
   *
   * @param filename id为mongoDB中图片存储的唯一标识
   * @return
   */
  public List<GridFSDBFile> select(String filename) {
    List<GridFSDBFile> resultList = gridFsTemplate.find(new Query()
        .addCriteria(Criteria.where("filename").is(filename)));
    return resultList;
  }

  /**
   * 删除图片
   *
   * @param id id为mongoDB中图片存储的唯一标识
   */
  public void delete(String id) {
    gridFsTemplate.delete(new Query()
        .addCriteria(Criteria.where("_id").is(id)));
  }

  /**
   * 根据文件名称删除文件.
   *
   * @param fileName
   */
  public void deleteByName(String fileName) {
    gridFsTemplate.delete(new Query()
        .addCriteria(Criteria.where("filename").is(fileName)));
  }
}