我正在尝试开发EmployeeImage组件

时间:2019-07-15 17:23:21

标签: reactjs mongodb spring-boot gridfs

我正在尝试开发一个作为表单数据提交的EmployeeImage组件,它应该将图像保存在MongoDB中,而且我应该能够检索到该图像。这段代码将保存图像并将其检索为前端的端点。我可以通过objectID进行网格操作来检索图像,但是如何传递字符串而不是objectID?

@Autowired
private MongoDbFactory mongoDbFactory;
@GetMapping("/save")
public void saveFile() tenter code herehrows IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);
    DB db;
    db = mongo.getDB("resources");
    String dbFileName = "EmployeeLogo";


    File path = new File("pathwhereihavealltheemployeeheadshots");
    List imageCollection = new ArrayList();
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
        if (files[i].isFile()) { //this line weeds out other directories/folders
            imageCollection.add(loadImage(files[i]));
            //InputStream iamgeStream = new FileInputStream(String.valueOf(imageCollection.get(i)));
            InputStream iamgeStream = new FileInputStream(String.valueOf(imageCollection.get(i)));
            imageFileId = gridFsOperations.store(iamgeStream, "photo");
            /*  GridFS gfsPhoto = new GridFS(db, "photo");
              GridFSInputFile gfsFile = gfsPhoto.createFile(files[i]);
              String id = UuidUtility.getUuid();
              gfsFile.setId(id);
              gfsFile.setFilename(dbFileName);
              gfsFile.save();
              System.out.println(gfsFile);
              return;*/

        }
    }

}
private Object loadImage(File file) {
    return file;
}


@GetMapping("/retriveImage")
@ResponseBody
public void retriveImageFile(HttpServletRequest request,
    HttpServletResponse response) throws IOException {
    ObjectId imageFileId = new ObjectId("5d2bdc6d1a04ac2555a4a730");

    GridFSFile file = gridFsOperations.findOne(Query.query(Criteria.where("_id").is(imageFileId)));

    InputStream is = gridFsOperations.getResource(file).getInputStream();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];
    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    byte[]imagenEnBytes = buffer.toByteArray();

    response.setHeader("Accept-ranges", "bytes");
    response.setContentType("image/jpeg");
    response.setContentLength(imagenEnBytes.length);
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Content-Description", "File Transfer");
    response.setHeader("Content-Transfer-Encoding:", "binary");


    //InputStream in = getClass()
    //      .getResourceAsStream("/com/baeldung/produceimage/image.jpg");
    //    return IOUtils.toByteArray(in);


    OutputStream out = response.getOutputStream();
    out.write(imagenEnBytes);
    out.flush();
    out.close();

}

private GridFSBucket getGridFs() {
    MongoDatabase db = mongoDbFactory.getDb();
    return GridFSBuckets.create(db);
}

0 个答案:

没有答案