简体   繁体   English

从字节数组创建文件对象时指定文件名(不创建物理文件)

[英]Specifying the filename while creating a File Object it from byte array (without creating a physical file)

I was stuck to a problem exactly same as this and with the solution posted i was able to solve my issue. 我陷入了一个与完全相同的问题,并且在发布解决方案后,我能够解决我的问题。 But now the problem is, when the attachment is received, there is no name for it. 但是现在的问题是,当收到附件时,没有名称。 In my method, i have asked for receiver's email id, subject , content, filename and byte[] for the File. 在我的方法中,我要求提供文件的收件人的电子邮件ID,主题,内容,文件名和字节[]。 There is no problem in format of the file i am passing but problem is with the name. 我传递的文件格式没有问题,但名称有问题。 The recipient gets "noname" as file name. 收件人获取“ noname”作为文件名。 How do we specify the filename of our choice. 我们如何指定所选文件名。 The filename which i am passing as parameter doesnt gets reflected. 我作为参数传递的文件名没有得到反映。 Please suggest. 请提出建议。

The code which i am using is 我正在使用的代码是

File file = new File("D:/my docs/Jetty.pdf");
int len1 = (int)(file.length());
FileInputStream fis1 = null;
try {
    fis1 = new FileInputStream(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
byte buf1[] = new byte[len1];
try {
    fis1.read(buf1);
    EmailServiceClient.sendEmailWithAttachment("xyz@gmail.com", "abc@gmail.com", "Hi", "PFA", "Jetty.pdf", buf1);

    System.out.println("SENT");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

My email service Implementation for this goes here 我的电子邮件服务的实现在这里

public void sendEmailWithAttachment(String emailIdTo, String emailIdFrom, String subject, String content,
    final String fileName, byte[] file) {
MimeMessage message = mailSender.createMimeMessage();
try {
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(emailIdTo);
    helper.setFrom(emailIdFrom);
    helper.setSubject(subject);
    helper.setText(content, true);
    helper.addInline("attachment", new ByteArrayResource(file) {
        @Override
        public String getFilename() {
            return fileName;
        }
    });
    mailSender.send(message);
} catch (MessagingException e) {
    throw new MailParseException(e);
}}

Please someone help in figuring this out 请有人帮忙解决这个问题

From the Spring Documentation I can say that the inlined elements don't have special names besides the contentId. 从Spring文档中,我可以说内联元素除了contentId之外没有其他特殊名称。 Maybe you want to add an attachment instead using the addAttachment method? 也许您想使用addAttachment方法添加附件? Then you can have a name for your file. 然后,您可以为文件命名。

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html

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

相关问题 从字节数组创建文件 - Creating a File from byte array 创建zip文件到字节数组 - creating zip file to byte array 使用内存中的字节数组创建Java File对象(或等效对象)(无物理文件) - Create a Java File object (or equivalent) using a byte array in memory (without a physical file) 如何在 Java 中从另一个文件创建 GZIP 文件而不创建新的物理文件 - How to create a GZIP file in Java from another file without creating a new physical file 在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 创建一个ClassLoader以从字节数组加载JAR文件 - Creating a ClassLoader to load a JAR file from a byte array 从数据库中读取BLOB(PDF内容)并编辑和输出PDF编辑的文件,而无需创建物理文件 - Read BLOB (PDF Content) from database and edit and output PDF edited file without creating physical file 将byte [](来自plsql)写入文件,而无需保存到物理路径并压缩它 - Write byte[](from plsql) into a file without saving into the physical path & ZIP it 如何发送zip文件而不在实际位置上创建它? - How to send zip file without creating it on physical location?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM