📄 attachmentpart.java
字号:
extractFilename((ManagedMemoryDataSource)ds); } } /** * Gets the content of this <CODE>AttachmentPart</CODE> object * as a Java object. The type of the returned Java object * depends on (1) the <CODE>DataContentHandler</CODE> object * that is used to interpret the bytes and (2) the <CODE> * Content-Type</CODE> given in the header. * * <P>For the MIME content types "text/plain", "text/html" and * "text/xml", the <CODE>DataContentHandler</CODE> object does * the conversions to and from the Java types corresponding to * the MIME types. For other MIME types,the <CODE> * DataContentHandler</CODE> object can return an <CODE> * InputStream</CODE> object that contains the content data as * raw bytes.</P> * * <P>A JAXM-compliant implementation must, as a minimum, * return a <CODE>java.lang.String</CODE> object corresponding * to any content stream with a <CODE>Content-Type</CODE> * value of <CODE>text/plain</CODE> and a <CODE> * javax.xml.transform.StreamSource</CODE> object * corresponding to a content stream with a <CODE> * Content-Type</CODE> value of <CODE>text/xml</CODE>. For * those content types that an installed <CODE> * DataContentHandler</CODE> object does not understand, the * <CODE>DataContentHandler</CODE> object is required to * return a <CODE>java.io.InputStream</CODE> object with the * raw bytes.</P> * @return a Java object with the content of this <CODE> * AttachmentPart</CODE> object * @throws SOAPException if there is no content set * into this <CODE>AttachmentPart</CODE> object or if there * was a data transformation error */ public Object getContent() throws SOAPException { if(contentObject != null) { return contentObject; } if(datahandler == null) { throw new SOAPException(Messages.getMessage("noContent")); } javax.activation.DataSource ds = datahandler.getDataSource(); InputStream is = null; try { is = ds.getInputStream();; } catch (java.io.IOException io) { log.error(Messages.getMessage("javaIOException00"), io); throw new SOAPException(io); } if (ds.getContentType().equals("text/plain")) { try { byte[] bytes = new byte[is.available()]; IOUtils.readFully(is, bytes); return new String(bytes); } catch (java.io.IOException io) { log.error(Messages.getMessage("javaIOException00"), io); throw new SOAPException(io); } } else if (ds.getContentType().equals("text/xml")) { return new StreamSource(is); } else if (ds.getContentType().equals("image/gif") || ds.getContentType().equals("image/jpeg")) { try { return ImageIOFactory.getImageIO().loadImage(is); } catch (Exception ex) { log.error(Messages.getMessage("javaIOException00"), ex); throw new SOAPException(ex); } } return is; } /** * Sets the content of this attachment part to that of the * given <CODE>Object</CODE> and sets the value of the <CODE> * Content-Type</CODE> header to the given type. The type of the * <CODE>Object</CODE> should correspond to the value given for * the <CODE>Content-Type</CODE>. This depends on the particular * set of <CODE>DataContentHandler</CODE> objects in use. * @param object the Java object that makes up * the content for this attachment part * @param contentType the MIME string that * specifies the type of the content * @throws java.lang.IllegalArgumentException if * the contentType does not match the type of the content * object, or if there was no <CODE> * DataContentHandler</CODE> object for this content * object * @see #getContent() getContent() */ public void setContent(Object object, String contentType) { ManagedMemoryDataSource source = null; setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, contentType); if (object instanceof String) { try { String s = (String) object; java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(s.getBytes()); source = new ManagedMemoryDataSource(bais, ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true); extractFilename(source); datahandler = new DataHandler(source); contentObject = object; return; } catch (java.io.IOException io) { log.error(Messages.getMessage("javaIOException00"), io); throw new java.lang.IllegalArgumentException( Messages.getMessage("illegalArgumentException00")); } } else if (object instanceof java.io.InputStream) { try { source = new ManagedMemoryDataSource((java.io.InputStream) object, ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true); extractFilename(source); datahandler = new DataHandler(source); contentObject = null; // the stream has been consumed return; } catch (java.io.IOException io) { log.error(Messages.getMessage("javaIOException00"), io); throw new java.lang.IllegalArgumentException(Messages.getMessage ("illegalArgumentException00")); } } else if (object instanceof StreamSource) { try { source = new ManagedMemoryDataSource(((StreamSource)object).getInputStream(), ManagedMemoryDataSource.MAX_MEMORY_DISK_CACHED, contentType, true); extractFilename(source); datahandler = new DataHandler(source); contentObject = null; // the stream has been consumed return; } catch (java.io.IOException io) { log.error(Messages.getMessage("javaIOException00"), io); throw new java.lang.IllegalArgumentException(Messages.getMessage ("illegalArgumentException00")); } } else { throw new java.lang.IllegalArgumentException( Messages.getMessage("illegalArgumentException00")); } } /** * Clears out the content of this <CODE> * AttachmentPart</CODE> object. The MIME header portion is left * untouched. */ public void clearContent() { datahandler = null; contentObject = null; } /** * Returns the number of bytes in this <CODE> * AttachmentPart</CODE> object. * @return the size of this <CODE>AttachmentPart</CODE> object * in bytes or -1 if the size cannot be determined * @throws SOAPException if the content of this * attachment is corrupted of if there was an exception * while trying to determine the size. */ public int getSize() throws SOAPException { if (datahandler == null) { return 0; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { datahandler.writeTo(bout); } catch (java.io.IOException ex) { log.error(Messages.getMessage("javaIOException00"), ex); throw new SOAPException(Messages.getMessage("javaIOException01", ex.getMessage()), ex); } return bout.size(); } /** * Gets all the values of the header identified by the given * <CODE>String</CODE>. * @param name the name of the header; example: * "Content-Type" * @return a <CODE>String</CODE> array giving the value for the * specified header * @see #setMimeHeader(java.lang.String, java.lang.String) setMimeHeader(java.lang.String, java.lang.String) */ public String[] getMimeHeader(String name) { return mimeHeaders.getHeader(name); } /** * Content ID. * * @return the contentId reference value that should be used directly * as an href in a SOAP element to reference this attachment. * <B>Not part of JAX-RPC, JAX-M, SAAJ, etc. </B> */ public String getContentIdRef() { return Attachments.CIDprefix + getContentId(); } /** * Maybe add file name to the attachment. * * @param source the source of the data */ private void extractFilename(ManagedMemoryDataSource source) { //check for there being a file if(source.getDiskCacheFile()!=null) { String path = source.getDiskCacheFile().getAbsolutePath(); setAttachmentFile(path); } } /** * Set the filename of this attachment part. * * @param path the new file path */ protected void setAttachmentFile(String path) { attachmentFile=path; } /** * Detach the attachment file from this class, so it is not cleaned up. * This has the side-effect of making subsequent calls to * getAttachmentFile() return <code>null</code>. */ public void detachAttachmentFile() { attachmentFile=null; } /** * Get the filename of this attachment. * * @return the filename or null for an uncached file */ public String getAttachmentFile() { return attachmentFile; } /** * when an attachment part is disposed, any associated files * are deleted, and the datahandler itself nulled. The object * is no longer completely usable, at this point */ public synchronized void dispose() { if (attachmentFile != null) { javax.activation.DataSource ds = datahandler.getDataSource(); if (ds instanceof ManagedMemoryDataSource) { ((ManagedMemoryDataSource) ds).delete(); //and delete the file } else { File f = new File(attachmentFile); //no need to check for existence here. f.delete(); } //set the filename to null to stop repeated use setAttachmentFile(null); } //clean up the datahandler, as it will have been //invalidated if it was bound to a file; if it wasnt //we get to release memory anyway datahandler = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -