本文整理汇总了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);
}
示例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 "";
}
示例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;
}
示例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();
}
示例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;
}
}
示例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);
}
}
示例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;
}
示例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));
}
示例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;
}
示例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;
}
示例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");
}
示例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>"));
}
示例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;
}
示例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());
}
示例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);
}