Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mule ESB:提供zip文件但无法解压缩的java组件_Java_Http_Mule_Esb_Payload - Fatal编程技术网

Mule ESB:提供zip文件但无法解压缩的java组件

Mule ESB:提供zip文件但无法解压缩的java组件,java,http,mule,esb,payload,Java,Http,Mule,Esb,Payload,流获取http请求,java组件创建一个zip文件并发回。zip文件已下载,但解压时出现错误,显示“压缩文件夹无效”。由java在临时位置创建的临时zip工作正常。问题是如何将zip文件设置为有效负载 public Object onCall(MuleEventContext eventContext) throws Exception { MuleMessage message = eventContext.getMessage(); message.setOutboundP

流获取http请求,java组件创建一个zip文件并发回。zip文件已下载,但解压时出现错误,显示“压缩文件夹无效”。由java在临时位置创建的临时zip工作正常。问题是如何将zip文件设置为有效负载

public Object onCall(MuleEventContext eventContext) throws Exception {
    MuleMessage message = eventContext.getMessage();

    message.setOutboundProperty("Content-type", "application/zip");
    message.setOutboundProperty("Content-Disposition", "attachment;filename=\"my_Logs.zip\"");
    File tempZip = File.createTempFile("LOGS", ".zip");
    FileOutputStream fos = new FileOutputStream(tempZip);
    ZipOutputStream zipOut = new ZipOutputStream(fos);

    File logDir = new File("C:/Temp/logs");
    zipOut.setMethod(ZipOutputStream.DEFLATED);
    zipOut.setLevel(9);
    FileInputStream in =null;
    int len =0;
    byte[] buf = new byte[1024];
    for(File file : logDir.listFiles()){
        String filename = file.getName();
        in = new FileInputStream(file);
        zipOut.putNextEntry(new ZipEntry(filename));
        while ((len = in.read(buf))>0){
            zipOut.write(buf,0,len);
        }
        zipOut.closeEntry();
        in.close();
    }
    zipOut.close();
    message.setPayload(tempZip);
    return message;
}
流动



我做错了什么?非常感谢您的帮助

您可以尝试此功能,它可以完美地压缩和提供文件

换衣服

在ZipTransformer构造函数中,不推荐使用以下内容

registerSourceType(InputStream.class);
registerSourceType(byte[].class);
改用这个:

registerSourceType(DataTypeFactory.create(InputStream.class));
registerSourceType(DataTypeFactory.create(byte[].class));
希望这有帮助。

我替换了以下代码 message.setPayload(tempZip); 具有 message.setPayload(org.apache.commons.io.FileUtils.readFileToByteArray(tempZip))

registerSourceType(DataTypeFactory.create(InputStream.class));
registerSourceType(DataTypeFactory.create(byte[].class));