當前位置: 首頁>>代碼示例>>Java>>正文


Java FileDataSource類代碼示例

本文整理匯總了Java中javax.activation.FileDataSource的典型用法代碼示例。如果您正苦於以下問題:Java FileDataSource類的具體用法?Java FileDataSource怎麽用?Java FileDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


FileDataSource類屬於javax.activation包,在下文中一共展示了FileDataSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: setAttachment

import javax.activation.FileDataSource; //導入依賴的package包/類
public static void setAttachment(Message message, String filename) throws MessagingException {
    // Create a multipar message
    Multipart multipart = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart();

    //Set File
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);

    //Add "file part" to multipart
    multipart.addBodyPart(messageBodyPart);

    //Set multipart to message
    message.setContent(multipart);
}
 
開發者ID:avedensky,項目名稱:JavaRushTasks,代碼行數:17,代碼來源:Solution.java

示例2: addAttachment

import javax.activation.FileDataSource; //導入依賴的package包/類
public static String addAttachment(MimeMultipart mm, String path)
{
	if(count == Integer.MAX_VALUE)
	{
		count = 0;
	}
	int cid = count++;
       try
	{
   		java.io.File file = new java.io.File(path);
   		MimeBodyPart mbp = new MimeBodyPart();
   		mbp.setDisposition(MimeBodyPart.INLINE);
   		mbp.setContent(new MimeMultipart("mixed"));
   		mbp.setHeader("Content-ID", "<" + cid + ">");
		mbp.setDataHandler(new DataHandler(new FileDataSource(file)));
        mbp.setFileName(new String(file.getName().getBytes("GBK"), "ISO-8859-1"));
        mm.addBodyPart(mbp);
        return String.valueOf(cid);
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
       return "";
}
 
開發者ID:skeychen,項目名稱:dswork,代碼行數:26,代碼來源:EmailUtil.java

示例3: buildMessage

import javax.activation.FileDataSource; //導入依賴的package包/類
private static Message buildMessage(Session session, String from, String recipients, String subject, String text, String filename) throws MessagingException, AddressException
{
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    message.setSubject(subject);

    BodyPart messageTextPart = new MimeBodyPart();
    messageTextPart.setText(text);

    BodyPart messageAttachmentPart = new MimeBodyPart();
    DataSource source = new FileDataSource(new File(filename));
    messageAttachmentPart.setDataHandler(new DataHandler(source));
    messageAttachmentPart.setFileName(filename);

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageTextPart);
    multipart.addBodyPart(messageAttachmentPart);
    message.setContent(multipart);

    return message;
}
 
開發者ID:marcelovca90,項目名稱:anti-spam-weka-gui,代碼行數:23,代碼來源:MailHelper.java

示例4: submitFaxJobValidTest

import javax.activation.FileDataSource; //導入依賴的package包/類
/**
 * Test 
 * 
 * @throws  Exception
 *          Any exception
 */
@Test
public void submitFaxJobValidTest() throws Exception
{
    Message message=new MimeMessage((Session)null);
    message.setSubject("fax:123456789");
    message.setFrom(new InternetAddress("[email protected]"));

    File file=File.createTempFile("temp_",".txt");
    file.deleteOnExit();
    IOHelper.writeTextFile("abc",file);
    DataSource source=new FileDataSource(file);
    BodyPart messageFileAttachmentBodyPart=new MimeBodyPart();
    messageFileAttachmentBodyPart.setDataHandler(new DataHandler(source));
    messageFileAttachmentBodyPart.setFileName(file.getName());
    Multipart multipart=new MimeMultipart();
    multipart.addBodyPart(messageFileAttachmentBodyPart);
    message.setContent(multipart);

    FaxJob faxJob=this.faxBridge.submitFaxJob(message);
    Assert.assertNotNull(faxJob);
    Assert.assertNotNull(faxJob.getFile());
    
    file.delete();
}
 
開發者ID:sagiegurari,項目名稱:fax4j,代碼行數:31,代碼來源:EMail2FaxBridgeTest.java

示例5: sendAttachMail

import javax.activation.FileDataSource; //導入依賴的package包/類
public boolean sendAttachMail(MailSenderInfo mailInfo) {
    MyAuthenticator authenticator = null;
    Properties pro = mailInfo.getProperties();
    if (mailInfo.isValidate()) {
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
    }
    try {
        Message mailMessage = new MimeMessage(Session.getInstance(pro, authenticator));
        mailMessage.setFrom(new InternetAddress(mailInfo.getFromAddress()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()));
        mailMessage.setSubject(mailInfo.getSubject());
        mailMessage.setSentDate(new Date());
        Multipart multi = new MimeMultipart();
        BodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        multi.addBodyPart(textBodyPart);
        for (String path : mailInfo.getAttachFileNames()) {
            DataSource fds = new FileDataSource(path);
            BodyPart fileBodyPart = new MimeBodyPart();
            fileBodyPart.setDataHandler(new DataHandler(fds));
            fileBodyPart.setFileName(path.substring(path.lastIndexOf("/") + 1));
            multi.addBodyPart(fileBodyPart);
        }
        mailMessage.setContent(multi);
        mailMessage.saveChanges();
        Transport.send(mailMessage);
        return true;
    } catch (MessagingException ex) {
        ex.printStackTrace();
        return false;
    }
}
 
開發者ID:JamesLiAndroid,項目名稱:AndroidKillerService,代碼行數:33,代碼來源:SimpleMailSender.java

示例6: addAttachment

import javax.activation.FileDataSource; //導入依賴的package包/類
/**
 * Add the specified file as an attachment
 *
 * @param fileName
 *            name of the file
 * @throws PackageException
 *             on error
 */
@PublicAtsApi
public void addAttachment(
                           String fileName ) throws PackageException {

    try {
        // add attachment to multipart content
        MimeBodyPart attPart = new MimeBodyPart();
        FileDataSource ds = new FileDataSource(fileName);
        attPart.setDataHandler(new DataHandler(ds));
        attPart.setDisposition(MimeBodyPart.ATTACHMENT);
        attPart.setFileName(ds.getName());

        addPart(attPart, PART_POSITION_LAST);
    } catch (MessagingException me) {
        throw new PackageException(me);
    }
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:26,代碼來源:MimePackage.java

示例7: saveStore

import javax.activation.FileDataSource; //導入依賴的package包/類
public StoreResult saveStore(String model, String key, DataSource dataSource)
        throws Exception {
    String path = key;
    File dir = new File(baseDir + "/" + model);
    dir.mkdirs();

    File targetFile = new File(baseDir + "/" + model + "/" + path);
    FileOutputStream fos = new FileOutputStream(targetFile);

    try {
        FileCopyUtils.copy(dataSource.getInputStream(), fos);
        fos.flush();
    } finally {
        fos.close();
    }

    StoreResult storeResult = new StoreResult();
    storeResult.setModel(model);
    storeResult.setKey(path);
    storeResult.setDataSource(new FileDataSource(targetFile));

    return storeResult;
}
 
開發者ID:zhaojunfei,項目名稱:lemon,代碼行數:24,代碼來源:FileStoreHelper.java

示例8: marshalAttachments

import javax.activation.FileDataSource; //導入依賴的package包/類
@Test
public void marshalAttachments() throws Exception {
	marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(BinaryObject.class);
	marshaller.setMtomEnabled(true);
	marshaller.afterPropertiesSet();
	MimeContainer mimeContainer = mock(MimeContainer.class);

	Resource logo = new ClassPathResource("spring-ws.png", getClass());
	DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));

	given(mimeContainer.convertToXopPackage()).willReturn(true);
	byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
	BinaryObject object = new BinaryObject(bytes, dataHandler);
	StringWriter writer = new StringWriter();
	marshaller.marshal(object, new StreamResult(writer), mimeContainer);
	assertTrue("No XML written", writer.toString().length() > 0);
	verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:Jaxb2MarshallerTests.java

示例9: addAttachment

import javax.activation.FileDataSource; //導入依賴的package包/類
public EmailMessage addAttachment(String attachmentName, File file)
    throws MessagingException {

  _totalAttachmentSizeSoFar += file.length();

  if (_totalAttachmentSizeSoFar > _totalAttachmentMaxSizeInByte) {
    throw new MessageAttachmentExceededMaximumSizeException(
        "Adding attachment '" + attachmentName
            + "' will exceed the allowed maximum size of "
            + _totalAttachmentMaxSizeInByte);
  }

  BodyPart attachmentPart = new MimeBodyPart();
  DataSource fileDataSource = new FileDataSource(file);
  attachmentPart.setDataHandler(new DataHandler(fileDataSource));
  attachmentPart.setFileName(attachmentName);
  _attachments.add(attachmentPart);
  return this;
}
 
開發者ID:JasonBian,項目名稱:azkaban,代碼行數:20,代碼來源:EmailMessage.java

示例10: createMultiPart

import javax.activation.FileDataSource; //導入依賴的package包/類
/**
 * 創建複雜的正文
 * @return
 * @throws MessagingException 
 */
private Multipart createMultiPart() throws MessagingException {
	// TODO Auto-generated method stub
	Multipart multipart=new MimeMultipart();
	
	//第一塊
	BodyPart bodyPart1=new MimeBodyPart();
	bodyPart1.setText("創建複雜的郵件,此為正文部分");
	multipart.addBodyPart(bodyPart1);
	
	//第二塊 以附件形式存在
	MimeBodyPart bodyPart2=new MimeBodyPart();
	//設置附件的處理器
	FileDataSource attachFile=new FileDataSource(ClassLoader.getSystemResource("attach.txt").getFile());
	DataHandler dh=new DataHandler(attachFile);
	bodyPart2.setDataHandler(dh);
	bodyPart2.setDisposition(Part.ATTACHMENT);
	bodyPart2.setFileName("test");
	multipart.addBodyPart(bodyPart2);
	
	return multipart;
}
 
開發者ID:xiaomin0322,項目名稱:alimama,代碼行數:27,代碼來源:SimpleSendReceiveMessage.java

示例11: sendUsingMTOM

import javax.activation.FileDataSource; //導入依賴的package包/類
public void sendUsingMTOM(String fileName, String targetEPR) throws IOException {
    final String EXPECTED = "<m0:uploadFileUsingMTOMResponse xmlns:m0=\"http://services.samples\"><m0:response>" +
            "<m0:image>PHByb3h5PkFCQzwvcHJveHk+</m0:image></m0:response></m0:uploadFileUsingMTOMResponse>";
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
    OMElement payload = factory.createOMElement("uploadFileUsingMTOM", ns);
    OMElement request = factory.createOMElement("request", ns);
    OMElement image = factory.createOMElement("image", ns);

    FileDataSource fileDataSource = new FileDataSource(new File(fileName));
    DataHandler dataHandler = new DataHandler(fileDataSource);
    OMText textData = factory.createOMText(dataHandler, true);
    image.addChild(textData);
    request.addChild(image);
    payload.addChild(request);

    ServiceClient serviceClient = new ServiceClient();
    Options options = new Options();
    options.setTo(new EndpointReference(targetEPR));
    options.setAction("urn:uploadFileUsingMTOM");
    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    options.setCallTransportCleanup(true);
    serviceClient.setOptions(options);
    OMElement response = serviceClient.sendReceive(payload);
    Assert.assertTrue(response.toString().contains(EXPECTED), "Attachment is missing in the response");
}
 
開發者ID:wso2,項目名稱:product-ei,代碼行數:27,代碼來源:ESBJAVA4909MultipartRelatedTestCase.java

示例12: sendUsingMTOM

import javax.activation.FileDataSource; //導入依賴的package包/類
public void sendUsingMTOM(String fileName, String targetEPR) throws IOException {
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMNamespace ns = factory.createOMNamespace("http://services.samples", "m0");
    OMElement payload = factory.createOMElement("uploadFileUsingMTOM", ns);
    OMElement request = factory.createOMElement("request", ns);
    OMElement image = factory.createOMElement("image", ns);

    FileDataSource fileDataSource = new FileDataSource(new File(fileName));
    DataHandler dataHandler = new DataHandler(fileDataSource);
    OMText textData = factory.createOMText(dataHandler, true);
    image.addChild(textData);
    request.addChild(image);
    payload.addChild(request);

    ServiceClient serviceClient = new ServiceClient();
    Options options = new Options();
    options.setTo(new EndpointReference(targetEPR));
    options.setAction("urn:uploadFileUsingMTOM");
    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    options.setCallTransportCleanup(true);
    serviceClient.setOptions(options);
    OMElement response = serviceClient.sendReceive(payload);
    Assert.assertTrue(response.toString().contains(
            "<m:testMTOM xmlns:m=\"http://services.samples/xsd\">" + "<m:test1>testMTOM</m:test1></m:testMTOM>"));
}
 
開發者ID:wso2,項目名稱:product-ei,代碼行數:26,代碼來源:CallOutMediatorWithMTOMTestCase.java

示例13: prepareMessage

import javax.activation.FileDataSource; //導入依賴的package包/類
/**
 * @param attachmentPaths
 *            Paths to attachment files.
 * @throws Exception
 *             Exception.
 * @return Message with attachments.
 */
private Message prepareMessage(String[] attachmentPaths) throws Exception {
    Message message = new MimeMessage(Session.getDefaultInstance(System.getProperties()));
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("Test.");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    for (String file : attachmentPaths) {
        BodyPart attachmentPart = new MimeBodyPart();
        File attachment = new File(SRC_TEST_RESOURCES_MAILING_TEST + "/" + file);
        DataSource source = new FileDataSource(attachment);
        attachmentPart.setDataHandler(new DataHandler(source));
        attachmentPart.setFileName(attachment.getName());
        multipart.addBodyPart(attachmentPart);
    }
    message.setContent(multipart);
    message.removeHeader("Content-Type");
    message.setHeader("Content-Type", "multipart/mixed");
    Assert.assertTrue(message.isMimeType("multipart/*"));
    return message;
}
 
開發者ID:Communote,項目名稱:communote-server,代碼行數:28,代碼來源:MailMessageHelperTest.java

示例14: testMessageWithAttachmentRedeliveryIssue

import javax.activation.FileDataSource; //導入依賴的package包/類
public void testMessageWithAttachmentRedeliveryIssue() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(1);

    template.send("direct:start", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
            exchange.getIn().addAttachment("message1.xml", new DataHandler(new FileDataSource(new File("src/test/data/message1.xml"))));
            exchange.getIn().addAttachment("message2.xml", new DataHandler(new FileDataSource(new File("src/test/data/message2.xml"))));
        }
    });

    assertMockEndpointsSatisfied();

    Message msg = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn();
    assertNotNull(msg);

    assertEquals("Hello World", msg.getBody());
    assertTrue(msg.hasAttachments());
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:21,代碼來源:MessageWithAttachmentRedeliveryIssueTest.java

示例15: testAttachments

import javax.activation.FileDataSource; //導入依賴的package包/類
public void testAttachments() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    mock.expectedBodiesReceivedInAnyOrder("log4j.properties", "jndi-example.properties");

    template.send("direct:begin", new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            Message m = exchange.getIn();
            m.setBody("Hello World");
            m.addAttachment("log4j", new DataHandler(new FileDataSource("src/test/resources/log4j.properties")));
            m.addAttachment("jndi-example", new DataHandler(new FileDataSource("src/test/resources/jndi-example.properties")));
        }
    });

    assertMockEndpointsSatisfied();
    Map<String, DataHandler> attachments = mock.getExchanges().get(0).getIn().getAttachments();
    assertTrue(attachments == null || attachments.size() == 0);
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:20,代碼來源:ExpressionClauseTest.java


注:本文中的javax.activation.FileDataSource類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。