Appearance
getResourceAsStream
Flowable 7.1.0 摘要:获取部署中指定资源的输入流。
方法签名与说明
InputStream getResourceAsStream(String deploymentId, String resourceName)
获取指定部署中某个资源文件的输入流,可用于下载BPMN文件、表单定义、决策表等。
Parameters:
- deploymentId - 部署ID,不能为null
- resourceName - 资源名称,不能为null
Returns:
- InputStream - 资源文件的输入流
Throws:
- FlowableObjectNotFoundException - 当部署或资源不存在时
常见使用场景
1. 下载BPMN文件
从系统中下载已部署的BPMN流程定义文件。
2. 导出流程配置
导出部署中的所有资源文件,用于备份或迁移。
3. 查看流程源码
查看流程定义的XML源码,用于学习和调试。
Kotlin + Spring Boot 调用示例
示例1:下载BPMN文件
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping("/api/deployments")
class ResourceDownloadController(
private val repositoryService: RepositoryService
) {
/**
* 下载部署资源
* 企业场景:导出流程定义文件
*/
@GetMapping("/{deploymentId}/resources/{resourceName}")
fun downloadResource(
@PathVariable deploymentId: String,
@PathVariable resourceName: String
): ResponseEntity<ByteArray> {
val inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName)
val bytes = inputStream.readBytes()
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"$resourceName\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(bytes)
}
}示例2:查看BPMN源码
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.stereotype.Service
@Service
class BpmnSourceViewService(
private val repositoryService: RepositoryService
) {
/**
* 查看BPMN XML源码
* 企业场景:开发人员查看流程定义源码
*/
fun viewBpmnSource(processDefinitionId: String): String {
val processDefinition = repositoryService.getProcessDefinition(processDefinitionId)
val inputStream = repositoryService.getResourceAsStream(
processDefinition.deploymentId,
processDefinition.resourceName
)
return inputStream.bufferedReader().use { it.readText() }
}
}示例3:导出部署的所有资源
kotlin
import org.flowable.engine.RepositoryService
import org.springframework.stereotype.Service
import java.io.File
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
@Service
class DeploymentExportService(
private val repositoryService: RepositoryService
) {
/**
* 导出部署包
* 企业场景:备份流程定义,导出为ZIP文件
*/
fun exportDeployment(deploymentId: String, outputPath: String) {
// 获取部署中的所有资源
val resourceNames = repositoryService.getDeploymentResourceNames(deploymentId)
val zipFile = File(outputPath)
ZipOutputStream(zipFile.outputStream()).use { zipOut ->
resourceNames.forEach { resourceName ->
val inputStream = repositoryService.getResourceAsStream(deploymentId, resourceName)
zipOut.putNextEntry(ZipEntry(resourceName))
inputStream.copyTo(zipOut)
zipOut.closeEntry()
}
}
println("部署包已导出: ${zipFile.absolutePath}")
println("包含 ${resourceNames.size} 个资源文件")
}
}注意事项
1. 资源释放
- InputStream使用完毕后必须关闭
- 建议使用
use函数自动管理资源
2. 资源名称
- 资源名称通常可以从ProcessDefinition获取
- 使用
getDeploymentResourceNames()获取所有资源名称
3. 异常处理
- 资源不存在时会抛出异常,需要捕获处理
相关 API
RepositoryService.getResourceAsStream()- 获取资源流RepositoryService.getDeploymentResourceNames()- 获取资源名称列表RepositoryService.getProcessDiagram()- 获取流程图
本文档说明
- 基于 Flowable 7.1.0 版本编写
- 所有示例均可直接在 Spring Boot + Kotlin 项目中使用