You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mendix Java Action导入多文件Zip包时遭遇Stream closed错误及单文件正常问题求助

Mendix Java Action导入多文件Zip包时遭遇Stream closed错误及单文件正常问题求助

大家好,我现在在做一个Mendix的Java Action,目标是导入一个包含多个XML文件的压缩包,但运行时一直抛出以下错误:

java.base/java.util.zip.ZipInputStream.ensureOpen(Unknown Source)
java.base/java.util.zip.ZipInputStream.getNextEntry(Unknown Source)

我的Java代码如下:

public class Zip_Upload extends CustomJavaAction<java.util.List<IMendixObject>>
{

private IMendixObject __InputZipFile;

private system.proxies.FileDocument InputZipFile;

public Zip_Upload(IContext context, IMendixObject InputZipFile)
{
super(context);
this.__InputZipFile = InputZipFile;
}

@java.lang.Override
public java.util.List<IMendixObject> executeAction() throws Exception
{
this.InputZipFile = this.__InputZipFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __InputZipFile);

// BEGIN USER CODE
List<IMendixObject> ZipFiles = new ArrayList<IMendixObject>();

InputStream fileInputStream = Core.getFileDocumentContent(getContext(), InputZipFile.getMendixObject());
byte[] buffer = IOUtils.toByteArray(fileInputStream);
fileInputStream.close();

int bufferlength = buffer.length;

/*String filename = "temp.zip";
File myFile = new File(Core.getConfiguration().getResourcesPath() + File.separator + filename);
myFile.createNewFile();
OutputStream outStream = new FileOutputStream(myFile);
outStream.write(buffer);
outStream.close();
*/

InputStream clonedStream = new ByteArrayInputStream(buffer);
int clonedlength = clonedStream.available();

Core.getLogger("Zip Import").log(LogLevel.INFO, "Byte Array Length = " + bufferlength);
Core.getLogger("Zip Import").log(LogLevel.INFO, "Cloned Input Stream Length = " + clonedlength);

ZipEntry ze;
ZipInputStream zis = new ZipInputStream(clonedStream);
int zislength = zis.available();

Core.getLogger("Zip Import").log(LogLevel.INFO, "Zip Input Stream availability = " + zislength);

if ((ze = zis.getNextEntry()) != null) {
IMendixObject FD = Core.instantiate(getContext(),flatfileinterfacecustomizations.proxies.ImportExportDocument.getType());
Core.storeFileDocumentContent(getContext(), FD, zis);
FD.setValue(getContext(), flatfileinterfacecustomizations.proxies.ImportExportDocument.MemberNames.Name.toString(), ze.getName());
ZipFiles.add(FD);
}

zis.close();
clonedStream.close();

return ZipFiles;
// END USER CODE
}

/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "Zip_Upload";
}

// BEGIN EXTRA CODE
// END EXTRA CODE
}

我试过把代码里的while循环改成if判断,这样能成功运行,但只能导入压缩包里的一个文件。我需要导入所有的文件,所以必须用while来遍历所有ZipEntry,但一换回while就会触发上面的Stream关闭错误,有没有大佬能帮我排查下问题所在?

备注:内容来源于stack exchange,提问作者Sarah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 10:48:03