当前位置: 首页>>代码示例>>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;未经允许,请勿转载。