mongoDB 文件操作
****************************
相关类与接口
GridFsTemplate
public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver {
************
构造方法
public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter) {
public GridFsTemplate(MongoDbFactory dbFactory, MongoConverter converter, @Nullable String bucket) {
************
常用方法
public ObjectId store(InputStream content, String filename) {
public ObjectId store(InputStream content, @Nullable Object metadata) {
public ObjectId store(InputStream content, @Nullable Document metadata) {
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
public ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Object metadata) {
public ObjectId store(InputStream content, @Nullable String filename, @Nullable Document metadata) {
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType, @Nullable Document metadata) {
public GridFSFindIterable find(Query query) {
public GridFSFile findOne(Query query) {
public void delete(Query query) {
public GridFsResource getResource(GridFSFile file) {
GridFsResource
public class GridFsResource extends InputStreamResource {
static final String CONTENT_TYPE_FIELD = "_contentType";
private static final ByteArrayInputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
@Nullable
private final GridFSFile file;
private final String filename;
*********
构造方法
private GridFsResource(String filename)
public GridFsResource(GridFSFile file)
public GridFsResource(GridFSFile file, InputStream inputStream)
*********
常用方法
public static GridFsResource absent(String filename)
public InputStream getInputStream() throws IOException, IllegalStateException {
public long contentLength() throws IOException {
public String getFilename() throws IllegalStateException {
public boolean exists() {
public long lastModified() throws IOException {
public String getContentType() {
GridFsBuckets
public final class GridFSBuckets {
public static GridFSBucket create(MongoDatabase database) {
return new GridFSBucketImpl(database);
}
public static GridFSBucket create(MongoDatabase database, String bucketName) {
return new GridFSBucketImpl(database, bucketName);
}
private GridFSBuckets() {
}
}
GridFsBucket:下载接口
@ThreadSafe
public interface GridFSBucket {
GridFSDownloadStream openDownloadStream(ObjectId var1);
。。。
}
****************************
使用示例
***************
controller 层
HelloController
@RestController
public class HelloController {
@Autowired
private GridFsTemplate gridFsTemplate;
@Autowired
private MongoDbFactory mongoDbFactory;
private Map<String,String> map=new HashMap<>();
@RequestMapping("/upload")
public String upload(MultipartFile file){
ObjectId objectId=null;
System.out.println(file.getContentType());
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
try{
objectId=gridFsTemplate.store(file.getInputStream(),file.getOriginalFilename(),file.getContentType());
map.put("fileId",objectId.toString());
}catch (Exception e){
e.printStackTrace();
}
assert objectId != null;
return objectId.toString();
}
@RequestMapping("/get")
public void get(HttpServletResponse response){
Query query=Query.query(Criteria.where("_id").is(map.get("fileId")));
GridFSFile gridFSFile=gridFsTemplate.findOne(query);
assert gridFSFile != null;
GridFsResource gridFsResource=new GridFsResource(gridFSFile,
GridFSBuckets.create(mongoDbFactory.getDb()).openDownloadStream(gridFSFile.getObjectId()));
response.reset();
try{
response.setContentType("application/octet-stream");
response.setContentLength((int)gridFSFile.getLength());
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(Objects.requireNonNull(gridFsResource.getFilename()),"utf-8"));
IOUtils.copy(gridFsResource.getInputStream(),response.getOutputStream());
}catch (Exception e){
e.printStackTrace();
}
}
@RequestMapping("/delete")
public String delete(){
Query query=Query.query(Criteria.where("_id").is(map.get("fileId")));
gridFsTemplate.delete(query);
return "success";
}
}