使用内存中的字节数组(没有物理文件)创建 Java File 对象(或等效对象)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/578305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:55:29  来源:igfitidea点击:

Create a Java File object (or equivalent) using a byte array in memory (without a physical file)

javaspringfilememoryio

提问by Kevin

I want to create a Java Fileobject in memory (without creating a physical file) and populate its content with a byte array.

我想File在内存中创建一个 Java对象(不创建物理文件)并用字节数组填充其内容。

Can this be done?

这能做到吗?

The idea is to pass it to a Spring InputStreamSource. I'm trying the method below, but it returns saying "the byte array does not contain a file name.".

这个想法是将它传递给 Spring InputStreamSource。我正在尝试下面的方法,但它返回说“字节数组不包含文件名。”。

MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);      
helper.setFrom("[email protected]", "xyz");
helper.setTo(email);
helper.setText(body,true);
helper.setSubject(subject);
helper.addInline("cImage",
        new InputStreamResource(new ByteArrayInputStream(imageByteArr)));

mailSender.send(message);

回答by Kevin

Can you paste the full stack trace? There is no such thing as an "in memory" file. Using a ByteArrayInputStream should be sufficient.

你能粘贴完整的堆栈跟踪吗?没有“内存中”文件这样的东西。使用 ByteArrayInputStream 应该就足够了。



You need to implement Resource#getFilename(). Try the following:

您需要实现 Resource#getFilename()。请尝试以下操作:

helper.addInline("cImage", new ByteArrayResource(imageByteArr){
            @Override
            public String getFilename() {
                return fileName;
            }
        });

回答by starblue

Maybe you need to use the other constructor of InputStreamResource?

也许您需要使用InputStreamResource另一个构造函数

回答by dhable

Have you tried changing the resource you feed to addInline()? If you wanted the resource to be in memory, I would have tried a org.springframework.core.io.ByteArrayResource.

您是否尝试过将您提供的资源更改为 addInline()?如果您希望资源在内存中,我会尝试使用 org.springframework.core.io.ByteArrayResource。

Update: I think you might need to use the DataSource version of the addInline() method and then use a byte array bound data source object to feed the data into the helper class. I would try the following:

更新:我认为您可能需要使用 addInline() 方法的 DataSource 版本,然后使用字节数组绑定数据源对象将数据提供给帮助程序类。我会尝试以下方法:

MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);              
helper.setFrom("[email protected]", "xyz");
helper.setTo(email);
helper.setText(body,true);
helper.setSubject(subject);

// use javax.mail.util.ByteArrayDataSource
ByteArrayDataSource imgDS = new ByteArrayDataSource( imageByteArr, "image/png");
helper.addInline("cImage", imgDS);

mailSender.send(message);

回答by Vlad Gudim

Maybe its worth trying a different overload of the method:

也许值得尝试不同的方法重载:

addInline(String contentId, 
          InputStreamSource inputStreamSource, 
          String contentType) 

I.e.:

IE:

addInline("cImage", 
          new InputStreamSource () 
          {  
             final private InputStream src = 
                                     new ByteArrayInputStream(imageByteArr);
             public InputStream getInputStream() {return src;}
          },
          "image/jpeg"); // or whatever image type you use 

回答by Vlad Gudim

It is important to create the MimeMessageHelper object correctly to support attachments and inline resources.

正确创建 MimeMessageHelper 对象以支持附件和内联资源非常重要。

Example: MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

示例: MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

In this example since multipart is set to true MULTIPART_MODE_MIXED_RELATED will be used and attachments and inline resouces will be supported.

在此示例中,由于 multipart 设置为 true,因此将使用 MULTIPART_MODE_MIXED_RELATED 并支持附件和内联资源。