简体   繁体   English

如何发送zip文件而不在实际位置上创建它?

[英]How to send zip file without creating it on physical location?

I want to send email with zip file attachment.. I m able to send pdf files without saving them on physical location using ByteArrayOutputStream. 我想发送带有zip文件附件的电子邮件。我能够发送pdf文件,而无需使用ByteArrayOutputStream将其保存在实际位置上。 But when I try zip those file an send it its not working. 但是,当我尝试将那些文件压缩后,将其发送失败。 It gives exception illegal attachment. 它给出异常非法附件。

Below is the code which I have written to create zip. 以下是我编写的用于创建zip的代码。

private MimeBodyPart zipAttachment( List<ByteArrayOutputStream> attachmentList, List<String> reportFileNames )
{
    MimeBodyPart messageBodyPart = null;
    try
    {
        // File file = File.createTempFile( "Reports.zip",".tmp" );
        // FileOutputStream fout = new FileOutputStream(file);
        ByteArrayOutputStream bout = new ByteArrayOutputStream(attachmentList.size());
        ZipOutputStream zos = new ZipOutputStream( bout );
        ZipEntry entry;
        for( int i = 0; i < attachmentList.size(); i++ )
        {
            ByteArrayOutputStream attachmentFile = attachmentList.get( i );
            byte[] bytes = attachmentFile.toByteArray();
            entry = new ZipEntry( reportFileNames.get( i ) );
            entry.setSize( bytes.length );
            zos.putNextEntry( entry );
            zos.write( bytes );
        }
        messageBodyPart = new MimeBodyPart();
        DataSource source = new ByteArrayDataSource( bout.toByteArray(), "application/zip" );
        messageBodyPart.setDataHandler( new DataHandler( source ) );
        messageBodyPart.setFileName( "Reports.zip" );

    }
    catch( Exception e )
    {
        // TODO: handle exception            
    }
    return messageBodyPart;
}

You forgot to call zos.closeEntry() after each item is written, at the end of your for loop. 在for循环结束时,您在写入每个项目之后忘记调用zos.closeEntry()。 And as noted, you haven't closed your ZipOutputStream. 如前所述,您尚未关闭ZipOutputStream。

I don't think you need to call entry.setSize(), either. 我也不认为您需要调用entry.setSize()。

Otherwise, this should work. 否则,这应该起作用。

I think that you have not flushed and closed ZipOutputStream . 我认为您还没有刷新并关闭ZipOutputStream Try to call zos.flush(); zos.close() 尝试调用zos.flush(); zos.close() zos.flush(); zos.close() . zos.flush(); zos.close() I hope this will help. 我希望这将有所帮助。

If not try to extract byte array from your ByteArrayOutputStream , save it into file and open with zip-enable tool. 如果不尝试从ByteArrayOutputStream提取字节数组,请将其保存到文件中并使用启用zip的工具打开。 It is just for debugging to be sure that your ZIP is OK and is not corrupted. 仅用于调试,以确保您的ZIP正常且未损坏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中创建ZIP InputStream而不先创建ZIP文件? - How to create a ZIP InputStream in Android without creating a ZIP file first? 将byte [](来自plsql)写入文件,而无需保存到物理路径并压缩它 - Write byte[](from plsql) into a file without saving into the physical path & ZIP it 如何在 Java 中从另一个文件创建 GZIP 文件而不创建新的物理文件 - How to create a GZIP file in Java from another file without creating a new physical file 在服务器(硬盘)上未创建ZIP文件的情况下,如何让客户端下载包含自定义选定文件的ZIP文件? - Without creating ZIP file on server (hard disk) how to let client download a ZIP file with custom selected files? 需要上传一个 xml 文件,解析并存储在数据库中,而不在任何物理物理位置存储文件 - need to upload an xml file, parse and store in database without storing file at any physical physical location 在FTP服务器上创建XML文件而不创建物理文件本地 - Create XML file on FTP server without creating physical file localy 将CSV文件上传到ftp而不在本地创建物理文件 - Uploading a csv file to ftp without creating a physical file locally 如何从字节数组制作文件并从这些文件制作 zip 并将 zip 发送到服务器而不在 Android 的存储中创建文件? - How to make files from an array of bytes and make zip from these files and send zip to the server without creating files in the storage of Android? 如何在不创建任何新文件的情况下访问zip文件目录中的文件? - How does one access a file within a directory in a zip file without creating any new files? 从字节数组创建文件对象时指定文件名(不创建物理文件) - Specifying the filename while creating a File Object it from byte array (without creating a physical file)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM