【问题标题】:Specifying the filename while creating a File Object it from byte array (without creating a physical file)从字节数组创建文件对象时指定文件名(不创建物理文件)
【发布时间】:2013-02-25 05:03:10
【问题描述】:

我遇到了与this 完全相同的问题 并发布了解决方案,我能够解决我的问题。 但是现在的问题是,当收到附件时,它没有名字。在我的方法中,我要求提供文件的接收者的电子邮件 ID、主题、内容、文件名和字节 []。我传递的文件格式没有问题,但问题在于名称。收件人将“noname”作为文件名。我们如何指定我们选择的文件名。我作为参数传递的文件名没有得到反映。请提出建议。

我使用的代码是

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();
}

我的电子邮件服务实现在这里

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);
}}

请有人帮忙解决这个问题

【问题讨论】:

    标签: java file-io jakarta-mail


    【解决方案1】:

    从 Spring 文档中,我可以说除了 contentId 之外,内联元素没有特殊名称。也许您想使用 addAttachment 方法添加附件?然后你可以为你的文件命名。

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

    【讨论】:

    • 感谢您的回复@toomasr,但我之前的问题是我提到的链接。我的问题是在电子邮件中添加附件而不创建物理文件,而那里的解决方案就成功了。但是现在收到的电子邮件附件没有任何名称,我不知道如何解决。
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2021-10-09
    相关资源
    最近更新 更多