Beispiel #1
0
  public Photo saveImg(FileItem item) throws Exception {
    Photo photo = new Photo();
    String filename = item.getName();
    if (filename == null || filename.length() == 0) {
      log.error("img name illegal");
      return null;
    }
    int index = filename.lastIndexOf(".");
    String type = filename.substring(index + 1);
    if (!ImgTools.checkImgFormatValidata(type)) {
      log.error("img type illegal");
      return null;
    }
    ObjectId id = ObjectIdGenerator.generate();
    // filename = new ObjectId() + filename.substring(index);
    photo.setId(id.toString());
    photo.setType(type);

    GridFS mphoto = new GridFS(MongoDBPool.getInstance().getDB(), collection);
    GridFSInputFile in = null;
    in = mphoto.createFile(item.getInputStream());
    in.setId(id);
    in.setFilename(id.toString());
    in.setContentType(type);
    in.save();
    item.getInputStream().close();
    return photo;
  }
 @Override
 public void saveBlob(final MD5 md5, final InputStream data, final boolean sorted)
     throws BlobStoreCommunicationException {
   if (data == null || md5 == null) {
     throw new NullPointerException("Arguments cannot be null");
   }
   if (getFile(md5) != null) {
     return; // already exists
   }
   final GridFSInputFile gif = gfs.createFile(data, true);
   gif.setId(md5.getMD5());
   gif.setFilename(md5.getMD5());
   gif.put(Fields.GFS_SORTED, sorted);
   try {
     gif.save();
   } catch (DuplicateKeyException dk) {
     // already here, done
   } catch (MongoException me) {
     throw new BlobStoreCommunicationException("Could not write to the mongo database", me);
   }
 }
  public FileEntity saveFile(
      String filePath,
      String fileName,
      String contentType,
      InputStream inputStream,
      String objectId) {

    if (filePath == null) filePath = "fs";
    GridFS gridFS = new GridFS(db, filePath);
    GridFSInputFile gfsFile = gridFS.createFile(inputStream);
    gfsFile.setFilename(fileName);
    gfsFile.setContentType(contentType);
    if (objectId != null) gfsFile.setId(new ObjectId(objectId));
    // String objectId = gfsFile.getId().toString();
    gfsFile.save();

    GridFSDBFile dbFile = gridFS.findOne(new ObjectId(gfsFile.getId().toString()));

    FileEntity entity = new FileEntity(dbFile);

    return entity;
  }