我正在尝试使用前端的ajax和后端的java API上载zip文件。 我的前端代码是-
var formData = new FormData();
var flowName = this.view.getFlowName();
var flowDescription = this.view.getFlowDescription();
formData.append('flow-package', file, file.name); // file is input type file
formData.append('name', flowName)
formData.append('description', flowDescription)
net.ajax({
url: '/flowautomation/v1/flows',
type: 'POST',
contentType: undefined,
processData: false,
data: formData,
success: this.importFlowSuccess.bind(this),
error: this.importFlowError.bind(this)
});
return true;
},
importFlowSuccess: function (response) {
},
importFlowError: function (error) {
}
在后端,我首先验证zip文件,并且代码在这里-
private FlowPackageErrorCode validate(final byte[] value) {
if (value == null) {
return FLOW_PACKAGE_NOT_FOUND;
}
try (final ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(value), Charset.forName("UTF-8"))) {
ZipEntry entry = zipStream.getNextEntry();
if (entry == null) {
return FLOW_PACKAGE_EMPTY;
}
boolean configExists = false;
while (entry != null) {
entry.getCrc();
entry.getCompressedSize();
final String entryName = entry.getName();
if (FLOW_CONFIG_FILE_NAME.equals(entryName)) {
configExists = true;
}
zipStream.closeEntry();
entry = zipStream.getNextEntry();
}
if (!configExists) {
return FLOW_CONFIG_NOT_FOUND;
}
} catch (final IOException e) {
LOGGER.debug("Failed to process flow package.", e);
return FLOW_PACKAGE_CORRUPT;
}
return null;
}
问题在于,后端zipInputstram条目中包含一些奇怪的垃圾字符,因此,该字符失败-
请帮助。