本文整理匯總了Java中javax.activation.DataHandler類的典型用法代碼示例。如果您正苦於以下問題:Java DataHandler類的具體用法?Java DataHandler怎麽用?Java DataHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DataHandler類屬於javax.activation包,在下文中一共展示了DataHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addAttachment
import javax.activation.DataHandler; //導入依賴的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 "";
}
示例2: addAttachment
import javax.activation.DataHandler; //導入依賴的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);
}
}
示例3: echo
import javax.activation.DataHandler; //導入依賴的package包/類
/**
* This method implements a web service that sends back
* any attachment it receives.
*/
public DataHandler echo( DataHandler dh) {
System.err.println("In echo");
//Attachments are sent by default back as a MIME stream if no attachments were
// received. If attachments are received the same format that was received will
// be the default stream type for any attachments sent.
//The following two commented lines would force any attachments sent back.
// to be in DIME format.
//Message rspmsg=AxisEngine.getCurrentMessageContext().getResponseMessage();
//rspmsg.getAttachmentsImpl().setSendType(org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
if (dh == null ) System.err.println("dh is null");
else System.err.println("Received \""+dh.getClass().getName()+"\".");
return dh;
}
示例4: addAttachment
import javax.activation.DataHandler; //導入依賴的package包/類
@Override
protected void addAttachment(String name, DataHandler data) throws MessagingException {
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(data);
attachment.setFileName(name);
attachment.setHeader("Content-ID", "<" + name + ">");
iBody.addBodyPart(attachment);
}
示例5: echoDir
import javax.activation.DataHandler; //導入依賴的package包/類
/**
* This method implements a web service that sends back
* an array of attachment it receives.
*/
public DataHandler[] echoDir( DataHandler[] attachments) {
System.err.println("In echoDir");
//Attachments are sent by default back as a MIME stream if no attachments were
// received. If attachments are received the same format that was received will
// be the default stream type for any attachments sent.
//The following two commented lines would force any attachments sent back.
// to be in DIME format.
//Message rspmsg=AxisEngine.getCurrentMessageContext().getResponseMessage();
//rspmsg.getAttachmentsImpl().setSendType(org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
if (attachments == null ) System.err.println("attachments is null!");
else System.err.println("Got " + attachments.length + " attachments!");
return attachments;
}
示例6: callHandlersOnResponse
import javax.activation.DataHandler; //導入依賴的package包/類
void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) {
//Lets copy all the MessageContext.OUTBOUND_ATTACHMENT_PROPERTY to the message
Map<String, DataHandler> atts = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
AttachmentSet attSet = context.packet.getMessage().getAttachments();
for (Map.Entry<String, DataHandler> entry : atts.entrySet()) {
String cid = entry.getKey();
if (attSet.get(cid) == null) { // Otherwise we would be adding attachments twice
Attachment att = new DataHandlerAttachment(cid, atts.get(cid));
attSet.add(att);
}
}
try {
//SERVER-SIDE
processor.callHandlersResponse(HandlerProcessor.Direction.OUTBOUND, context, handleFault);
} catch (WebServiceException wse) {
//no rewrapping
throw wse;
} catch (RuntimeException re) {
throw re;
}
}
示例7: callHandlersOnResponse
import javax.activation.DataHandler; //導入依賴的package包/類
void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault) {
//Lets copy all the MessageContext.OUTBOUND_ATTACHMENT_PROPERTY to the message
Map<String, DataHandler> atts = (Map<String, DataHandler>) context.get(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
AttachmentSet attSet = context.packet.getMessage().getAttachments();
for (Entry<String, DataHandler> entry : atts.entrySet()) {
String cid = entry.getKey();
Attachment att = new DataHandlerAttachment(cid, atts.get(cid));
attSet.add(att);
}
try {
//SERVER-SIDE
processor.callHandlersResponse(HandlerProcessor.Direction.OUTBOUND, context, handleFault);
} catch (WebServiceException wse) {
//no rewrapping
throw wse;
} catch (RuntimeException re) {
throw re;
}
}
示例8: testCreateTemplate
import javax.activation.DataHandler; //導入依賴的package包/類
@Test
public void testCreateTemplate() throws IOException {
List<Attachment> attachmentList = new ArrayList<>();
Attachment json = mock(Attachment.class);
Attachment tpl = mock(Attachment.class);
attachmentList.add(json);
attachmentList.add(tpl);
DataHandler jsonDataHandler = mock(DataHandler.class);
DataHandler tplDataHandler = mock(DataHandler.class);
when(json.getDataHandler()).thenReturn(jsonDataHandler);
when(tpl.getDataHandler()).thenReturn(tplDataHandler);
when(jsonDataHandler.getName()).thenReturn("test-target.json");
when(tplDataHandler.getName()).thenReturn("test-target.tpl");
String jsonContent = "{}";
when(jsonDataHandler.getInputStream()).thenReturn(new ByteArrayInputStream(jsonContent.getBytes()));
String tplContent = "template content";
when(tplDataHandler.getInputStream()).thenReturn(new ByteArrayInputStream(tplContent.getBytes()));
when(impl.createTemplate(any(InputStream.class), any(InputStream.class), anyString(), any(User.class))).thenReturn(new CreateResourceResponseWrapper(new ConfigTemplate()));
Response response = cut.createTemplate(attachmentList, "test-target-name", authenticatedUser);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
}
示例9: get
import javax.activation.DataHandler; //導入依賴的package包/類
public Object get(Object key) {
if(packet.supports(key))
return packet.get(key); // strongly typed
if(packet.getHandlerScopePropertyNames(true).contains(key))
return null; // no such application-scope property
Object value = packet.invocationProperties.get(key);
//add the attachments from the Message to the corresponding attachment property
if(key.equals(MessageContext.INBOUND_MESSAGE_ATTACHMENTS)){
Map<String, DataHandler> atts = (Map<String, DataHandler>) value;
if(atts == null)
atts = new HashMap<String, DataHandler>();
AttachmentSet attSet = packet.getMessage().getAttachments();
for(Attachment att : attSet){
atts.put(att.getContentId(), att.asDataHandler());
}
return atts;
}
return value;
}
示例10: addBinaryText
import javax.activation.DataHandler; //導入依賴的package包/類
private String addBinaryText(DataHandler data) {
String hrefOrCid = null;
if (data instanceof StreamingDataHandler) {
hrefOrCid = ((StreamingDataHandler) data).getHrefCid();
}
if (hrefOrCid == null) hrefOrCid = encodeCid();
String prefixedCid = (hrefOrCid.startsWith("cid:")) ? hrefOrCid : "cid:" + hrefOrCid;
// Should we do the threshold processing on DataHandler ? But that would be
// expensive as DataHolder need to read the data again from its source
//binaryText = BinaryTextImpl.createBinaryTextFromDataHandler((MessageImpl) soap, prefixedCid, currentElement.getOwnerDocument(), data);
//currentElement.appendChild(binaryText);
if (currentElement instanceof MtomEnabled) {
binaryText = ((MtomEnabled) currentElement).addBinaryText(prefixedCid, data);
} else {
throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement);
}
return hrefOrCid;
}
示例11: writeNonMtomAttachments
import javax.activation.DataHandler; //導入依賴的package包/類
@SuppressWarnings("resource")
private void writeNonMtomAttachments(AttachmentSet attachments,
OutputStream out, String boundary) throws IOException {
for (Attachment att : attachments) {
DataHandler dh = att.asDataHandler();
if (dh instanceof StreamingDataHandler) {
StreamingDataHandler sdh = (StreamingDataHandler) dh;
// If DataHandler has href Content-ID, it is MTOM, so skip.
if (sdh.getHrefCid() != null)
continue;
}
// build attachment frame
writeln("--" + boundary, out);
writeMimeHeaders(att.getContentType(), att.getContentId(), out);
att.writeTo(out);
writeln(out); // write \r\n
}
}
示例12: createAttachmentBuilder
import javax.activation.DataHandler; //導入依賴的package包/類
/**
* Creates an AttachmentBuilder based on the parameter type
*
* @param param
* runtime Parameter that abstracts the annotated java parameter
* @param setter
* specifies how the obtained value is set into the argument. Takes
* care of Holder arguments.
*/
public static ResponseBuilder createAttachmentBuilder(ParameterImpl param, ValueSetter setter) {
Class type = (Class)param.getTypeInfo().type;
if (DataHandler.class.isAssignableFrom(type)) {
return new DataHandlerBuilder(param, setter);
} else if (byte[].class==type) {
return new ByteArrayBuilder(param, setter);
} else if(Source.class.isAssignableFrom(type)) {
return new SourceBuilder(param, setter);
} else if(Image.class.isAssignableFrom(type)) {
return new ImageBuilder(param, setter);
} else if(InputStream.class==type) {
return new InputStreamBuilder(param, setter);
} else if(isXMLMimeType(param.getBinding().getMimeType())) {
return new JAXBBuilder(param, setter);
} else if(String.class.isAssignableFrom(type)) {
return new StringBuilder(param, setter);
} else {
throw new UnsupportedOperationException("Unexpected Attachment type ="+type);
}
}
示例13: addBodyPart
import javax.activation.DataHandler; //導入依賴的package包/類
/**
* Method addBodyPart
*
* @param mp
* @param dh
*/
private static void addBodyPart(MimeMultipart mp, DataHandler dh) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
try {
messageBodyPart.setDataHandler(dh);
String contentType = dh.getContentType();
if ((contentType == null) || (contentType.trim().length() == 0)) {
contentType = "application/octet-stream";
}
System.out.println("Content type: " + contentType);
messageBodyPart.setHeader(HEADER_CONTENT_TYPE, contentType);
messageBodyPart.setHeader(
HEADER_CONTENT_TRANSFER_ENCODING,
"binary"); // Safe and fastest for anything other than mail
mp.addBodyPart(messageBodyPart);
} catch (javax.mail.MessagingException e) {
}
}
示例14: addFileAffix
import javax.activation.DataHandler; //導入依賴的package包/類
/**
* 增加發送附件
*
* @param filename
* 郵件附件的地址,隻能是本機地址而不能是網絡地址,否則拋出異常
* @return
*/
public static boolean addFileAffix(String filename) {
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeText(fileds.getName(), "utf-8",
null)); // 解決附件名稱亂碼
mp.addBodyPart(bp);// 添加附件
attachment.add(fileds);
} catch (Exception e) {
return false;
}
return true;
}
示例15: makeIteratorTriggerNative
import javax.activation.DataHandler; //導入依賴的package包/類
@SuppressWarnings ( "resource" )
public static Object makeIteratorTriggerNative ( UtilFactory uf, Object it ) throws Exception, ClassNotFoundException, NoSuchMethodException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Cipher m = Reflections.createWithoutConstructor(NullCipher.class);
Reflections.setFieldValue(m, "serviceIterator", it);
Reflections.setFieldValue(m, "lock", new Object());
InputStream cos = new CipherInputStream(null, m);
Class<?> niCl = Class.forName("java.lang.ProcessBuilder$NullInputStream"); //$NON-NLS-1$
Constructor<?> niCons = niCl.getDeclaredConstructor();
niCons.setAccessible(true);
Reflections.setFieldValue(cos, "input", niCons.newInstance());
Reflections.setFieldValue(cos, "ibuffer", new byte[0]);
Object b64Data = Class.forName("com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data").newInstance();
DataSource ds = (DataSource) Reflections
.createWithoutConstructor(Class.forName("com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource")); //$NON-NLS-1$
Reflections.setFieldValue(ds, "is", cos);
Reflections.setFieldValue(b64Data, "dataHandler", new DataHandler(ds));
Reflections.setFieldValue(b64Data, "data", null);
Object nativeString = Reflections.createWithoutConstructor(Class.forName("jdk.nashorn.internal.objects.NativeString"));
Reflections.setFieldValue(nativeString, "value", b64Data);
return uf.makeHashCodeTrigger(nativeString);
}