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

前端 未结 5 645
情深已故
情深已故 2020-12-15 12:38

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

Can this be done?

T

相关标签:
5条回答
  • 2020-12-15 13:23

    Maybe you need to use the other constructor of InputStreamResource?

    0 讨论(0)
  • 2020-12-15 13:24

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


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

    helper.addInline("cImage", new ByteArrayResource(imageByteArr){
                @Override
                public String getFilename() {
                    return fileName;
                }
            });
    
    0 讨论(0)
  • 2020-12-15 13:24

    Maybe its worth trying a different overload of the method:

    addInline(String contentId, 
              InputStreamSource inputStreamSource, 
              String contentType) 
    

    I.e.:

    addInline("cImage", 
              new InputStreamSource () 
              {  
                 final private InputStream src = 
                                         new ByteArrayInputStream(imageByteArr);
                 public InputStream getInputStream() {return src;}
              },
              "image/jpeg"); // or whatever image type you use 
    
    0 讨论(0)
  • 2020-12-15 13:29

    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.

    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:

    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);              
    helper.setFrom("no-reply@example.com", "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);
    
    0 讨论(0)
  • 2020-12-15 13:34

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

    Example: 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.

    0 讨论(0)
提交回复
热议问题