| 
					
				 | 
			
			
				@@ -15,79 +15,87 @@ import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * mongdb GridFs 文件管理服务 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * @author TRX 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * @date 2024/3/25 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 public class GridFsService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  @Autowired GridFsTemplate gridFsTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    GridFsTemplate gridFsTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 存储文件数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param content 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param filename 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  public String addGridFs(InputStream content, String filename) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    ObjectId objectId = gridFsTemplate.store(content, filename); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return objectId.toHexString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 存储文件数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param content 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param filename 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String addGridFs(InputStream content, String filename) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ObjectId objectId = gridFsTemplate.store(content, filename); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return objectId.toHexString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 根据ID查询文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @throws IOException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  public InputStream getFile(String id) throws IOException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (file != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      GridFsResource resource = gridFsTemplate.getResource(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      return resource.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 根据ID查询文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws IOException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public InputStream getFile(String id) throws IOException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (file != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            GridFsResource resource = gridFsTemplate.getResource(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return resource.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 根据名称查询多个文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param name 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @throws IOException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  public List<InputStream> getFilesByName(String name) throws IOException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    List<InputStream> inputStreams = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    Query query = new Query(Criteria.where("filename").is(name)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    List<GridFSFile> files = gridFsTemplate.find(query).into(new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 根据名称查询多个文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param name 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws IOException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<InputStream> getFilesByName(String name) throws IOException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<InputStream> inputStreams = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Query query = new Query(Criteria.where("filename").is(name)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<GridFSFile> files = gridFsTemplate.find(query).into(new ArrayList<>()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    for (GridFSFile file : files) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      GridFsResource resource = gridFsTemplate.getResource(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      InputStream inputStream = resource.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (inputStream != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        inputStreams.add(inputStream); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        files.stream().forEach((file) -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            GridFsResource resource = gridFsTemplate.getResource(file); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            InputStream inputStream = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                inputStream = resource.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new RuntimeException(e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (inputStream != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                inputStreams.add(inputStream); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return inputStreams; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    return inputStreams; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 根据文件删除文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param filename 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  public void deleteFileByName(String filename) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    gridFsTemplate.delete(new Query(Criteria.where("filename").is(filename))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 根据文件删除文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param filename 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void deleteFileByName(String filename) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        gridFsTemplate.delete(new Query(Criteria.where("filename").is(filename))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 根据文件ID删除文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   * @param id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  public void deleteFileById(String id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    gridFsTemplate.delete(new Query(Criteria.where("_id").is(id))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 根据文件ID删除文件 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void deleteFileById(String id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        gridFsTemplate.delete(new Query(Criteria.where("_id").is(id))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |