gridFsTemplate存文件时候报空指针异常

原因有好多但是,我烦的是一个很低级的错误,因为自己使用的的springboot,所以在测试的时候需要使用springboot的配置文件,才存到MongoDB数据库中去,但是居然忘记了添加下面的注解,才导致的报空指针异常。

@SpringBootTest
@RunWith(SpringRunner.class)

完整代码如下:

@SpringBootTest
@RunWith(SpringRunner.class)
public class GrifsTest {


    @Autowired
    GridFsTemplate gridFsTemplate;
    @Test
    public void testGridFs() throws FileNotFoundException {
//要存储的文件
        File file = new File("d:/index_banner.ftl");
//定义输入流
        FileInputStream inputStram = new FileInputStream(file);
//向GridFS存储文件
       ObjectId objectId =  gridFsTemplate.store(inputStram, "index_banner.ftl", "");
//得到文件ID
        String fileId = objectId.toString();
        System.out.println(file);
    }

}

 

你可能感兴趣的:(springboot)