Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecifying the filename while creating a File Object it from byte array (without creating a physical file)
    primarykey
    data
    text
    <p>I was stuck to a problem exactly same as <a href="https://stackoverflow.com/questions/578305/create-a-java-file-object-or-equivalent-using-a-byte-array-in-memory-without/15023104#15023104">this</a> 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. 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. How do we specify the filename of our choice. The filename which i am passing as parameter doesnt gets reflected. Please suggest.</p> <p>The code which i am using is</p> <pre><code>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(); } </code></pre> <p>My email service Implementation for this goes here</p> <pre><code>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); }} </code></pre> <p>Please someone help in figuring this out </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload