2012-07-26 46 views
1

我正在使用apache commons fileupload上传文件。我想将此文件附加到电子邮件。我不想写入临时文件,但我想将数据保存在内存中并作为附件发送。我需要从这里开始。在此先感谢将上传的文件附加为电子邮件

DiskFileItemFactory factory = new DiskFileItemFactory(); 
       PortletFileUpload upload = new PortletFileUpload(factory); 
       List items = upload.parseRequest(request); 

       Iterator iter = items.iterator(); 

       while(iter.hasNext()) { 
        FileItem item = (FileItem) iter.next(); 
        if(item.isFormField()) { 

         String name = item.getFieldName(); 
         String value = item.getString(); 
         response.setRenderParameter(name, value); 

        } else { 

         String fieldName = item.getFieldName(); 
         String fileName = item.getName(); 
         //String contentType = item.getContentType(); 
         boolean isInMemory = item.isInMemory(); 
         long sizeInBytes = item.getSize(); 

         InputStream uploadedStream = item.getInputStream(); 

        } 
       } 

UPDATE 我有一个与attachement及其工作正常发送电子邮件以下方法签名。

sendWithFileAttachment (String[] recipients, 
      String subject, 
      File message, 
      String from, 
      String filename) { 

      BodyPart messageBodyPart = new MimeBodyPart(); 

    // Fill the message 
    messageBodyPart.setText("Pardon Ideas"); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 

    // Part two is attachment 
    messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(message); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 

    // Put parts in message 

    msg.setContent(multipart); 

    Transport.send(msg); 

}

更新2: 我在执行你的代码后,提示以下错误。能否请你帮我这个

HIT ME! 15782 
Jul 31, 2012 11:17:56 AM test.test.EmailUtility1$InputStreamMimeBodyPart getContentStream 
SEVERE: null 
Throwable occurred: java.io.IOException: Stream closed 
    at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156) 
    at java.io.BufferedInputStream.reset(BufferedInputStream.java:425) 
    at test.test.EmailUtility1$InputStreamMimeBodyPart.getContentStream(EmailUtility1.java:174) 
    at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:94) 
    at javax.activation.DataHandler.writeTo(DataHandler.java:302) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845) 
    at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361) 
    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85) 
    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:881) 
    at javax.activation.DataHandler.writeTo(DataHandler.java:314) 
    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350) 
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585) 
    at javax.mail.Transport.send0(Transport.java:169) 
    at javax.mail.Transport.send(Transport.java:98) 
    at test.test.EmailUtility1.sendWithFileAttachment(EmailUtility1.java:155) 
    at test.test.TestEmail.main(TestEmail.java:32) 
Exception in thread "main" javax.mail.MessagingException: IOException while sending message; 
    nested exception is: 
    java.io.IOException: Stream closed 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625) 
    at javax.mail.Transport.send0(Transport.java:169) 
    at javax.mail.Transport.send(Transport.java:98) 
    at test.test.EmailUtility1.sendWithFileAttachment(EmailUtility1.java:155) 
    at test.test.TestEmail.main(TestEmail.java:32) 
Caused by: java.io.IOException: Stream closed 
    at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:156) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:319) 
    at java.io.FilterInputStream.read(FilterInputStream.java:101) 

回答

1

你需要重写MimeBodyPart的(你很可能使用,我认为),能够通过流来获得他们的内容。

public void sendWithFileAttachment(String recipient, 
       String subject, 
       InputStream message, 
       String from, 
       String filename) throws MessagingException { 
    MimeMessage msg = new MimeMessage(getSession()); 

    // set the from and to address 
    InternetAddress addressFrom = new InternetAddress(from); 
    msg.setFrom(addressFrom); 

    InternetAddress addressTo = new InternetAddress(recipient); 
    msg.setRecipient(Message.RecipientType.TO, addressTo); 

    BodyPart messageBodyPart = new MimeBodyPart(); 

    // Fill the message 
    messageBodyPart.setText("Pardon Ideas"); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 

    // Part two is attachment 
    messageBodyPart = new InputStreamMimeBodyPart(message); 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 

    // Put parts in message 

    msg.setContent(multipart); 

    Transport.send(msg); 
    // important: you need to close the message stream manually 
    try { 
     message.close(); 
    } catch (IOException ex) { 
     // meh. So what. 
    } 

} 

private class InputStreamMimeBodyPart extends MimeBodyPart { 

private InputStream inputStream; 

    public InputStreamMimeBodyPart(InputStream source) { 
     this.inputStream = source; 
     if(!inputStream.markSupported()) { 
      throw new IllegalArgumentException("only streams with mark supported are ok"); 
     } 
     inputStream.mark(Integer.MAX_VALUE); // remeber the whole stream. 
    } 

    @Override 
    protected InputStream getContentStream() throws MessagingException { 
     throw new IllegalStateException("getContentStream is not implemented on purpose."); 
    } 

    @Override 
    public void writeTo(OutputStream os) throws IOException, MessagingException { 
     System.out.println("writing to somewhere."); 
     byte[] buf = new byte[32]; 
     int length; 
     inputStream.reset(); 
     while((length = inputStream.read(buf)) > -1) { 
      os.write(buf, 0, length); 
     } 
    } 
} 

private Session getSession() { 
    // here you do authentication etc. 
    Properties properties = new Properties(); 
    return Session.getInstance(properties); 
} 
+0

请找到更新。 – user525146 2012-07-26 19:37:17

+0

你能帮我解决吗? – user525146 2012-07-31 05:51:10

+0

@ user525146你走了。你在你的问题中遗漏了你的方法的一些部分,所以你将不得不适应。请注意(这很重要),您的InputStream必须支持标记功能。这是因为在这个实现中(在我的机器上)邮件API会调用'getContentStream'两次。我还没有弄清楚它为什么这样做,但只要没有理由这样做,我就不会打扰。 – 2012-07-31 12:33:03