Refine search
@Override protected void render(Part mailPart, String fileName) { DataSource ds = new URLDataSource(url); if (fileName == null) { fileName = url.getPath(); } render(mailPart, fileName, ds); } }
private void addAttachedFilePart( FileObject file ) throws Exception { // create a data source MimeBodyPart files = new MimeBodyPart(); // create a data source URLDataSource fds = new URLDataSource( file.getURL() ); // get a data Handler to manipulate this file type; files.setDataHandler( new DataHandler( fds ) ); // include the file in the data source files.setFileName( file.getName().getBaseName() ); // insist on base64 to preserve line endings files.addHeader( "Content-Transfer-Encoding", "base64" ); // add the part with the file in the BodyPart(); data.parts.addBodyPart( files ); if ( isDetailed() ) { logDetailed( BaseMessages.getString( PKG, "Mail.Log.AttachedFile", fds.getName() ) ); } }
URLDataSource url; //Code url = new URLDataSource(new URL("localhost:8080/test/attachment/simpldoc.docx"));
url = new URL(urlString); } catch (MalformedURLException e1) { throw new UnexpectedException("Invalid URL '" + urlString + "'", e1); String[] parts = url.getPath().split("/"); name = parts[parts.length - 1]; dataSource = url.getProtocol().equals("file") ? new VirtualFileDataSource(url.getFile()) : new URLDataSource(url); } else { dataSource = new VirtualFileDataSource(img); if (url == null || urlDataSource == null || !url.toExternalForm().equals(urlDataSource.getURL().toExternalForm())) { throw new UnexpectedException("embedded name '" + name + "' is already bound to URL " + urlDataSource.getURL() + "; existing names cannot be rebound");
/** * 读取文件 * @param filename * @return * @throws IOException */ public static byte[] readFile(String filename) throws IOException { // ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream in = null; if (filename.startsWith("http://")) { URL url = new URL(filename); URLDataSource uds = new URLDataSource(url); in = uds.getInputStream(); } else { in = new FileInputStream(filename); } BufferedInputStream bufferedInputStream = new BufferedInputStream(in); int len = bufferedInputStream.available(); byte[] bytes = new byte[len]; int r = bufferedInputStream.read(bytes); if (len != r) { bytes = null; throw new IOException("读取文件不正确"); } bufferedInputStream.close(); return bytes; }
if (url.toExternalForm().equals(urlDataSource.getURL().toExternalForm())) + "' is already bound to URL " + urlDataSource.getURL() + "; existing names cannot be rebound"); try is = url.openStream(); return embed(new URLDataSource(url), name);
public URLAttachment(String url, String fileName, ContentDisposition contentDisposition) { super(); URLDataSource uds; try { uds = new URLDataSource(new URL(url)); super.setFileName(fileName); super.setMimeType(uds.getContentType()); super.setContentDisposition(contentDisposition); super.setBytes(Streams.toByteArray(uds.getInputStream())); } catch (MalformedURLException e) { throw new AttachmentException("Wasn't able to create email attachment from URL: " + url, e); } catch (IOException e) { throw new AttachmentException("Wasn't able to create email attachment from URL: " + url, e); } }
final InputStream is = url.openStream(); is.close(); return attach(new URLDataSource(url), name, description, disposition);
DataSource fds = url.getProtocol().startsWith("file:") ? new FileDataSource(url.getFile()) : new URLDataSource(url);
@Override public PatchId addArchive(PatchMetadata metadata, DataHandler dataHandler, boolean force) throws IOException { PatchId result = super.addArchive(metadata, dataHandler, force); // Remove the source file when it was placed in the repository if (dataHandler.getDataSource() instanceof URLDataSource) { URL sourceURL = ((URLDataSource)dataHandler.getDataSource()).getURL(); Path sourcePath = new File(sourceURL.getPath()).toPath(); if (sourcePath.startsWith(rootPath)) { File sourceFile = sourcePath.toFile(); File targetFile = new File (sourceFile.getPath().concat(".delete")); sourceFile.renameTo(targetFile); targetFile.delete(); } } return result; }
String fileName = "WEB-INF/resources/images/logomailtemplate.png"; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } DataSource ds = new FileDataSource(new File(classLoader.getResource(fileName).toURI())); //OR DataSource ds = new URLDataSource(classLoader.getResource(fileName));
protected void runTest() throws Throwable { InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource( XMLSample.SIMPLE.getUrl())); DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(registration.getURL().toExternalForm()); assertThat(document.getDocumentElement().getLocalName()).isEqualTo("root"); assertThat(ds.getOpenStreamCount()).isEqualTo(0); } finally { registration.unregister(); } } }
/** * Create a <code>DataHandler</code> instance referencing a URL. * The DataHandler internally creates a <code>URLDataSource</code> * instance to represent the URL. * * @param url a URL object */ public DataHandler(URL url) { dataSource = new URLDataSource(url); oldFactory = factory; // keep track of the factory }
protected String getFromUrl(final URL url) throws Exception { final URLDataSource dataSource = new URLDataSource(url); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(dataSource.getInputStream(), baos); return new String(baos.toByteArray(), "UTF-8"); }
BodyPart imagen = new MimeBodyPart(); URL url = new URL("http://server.com/mybestpicture.jpg"); URLDataSource ds = new URLDataSource(url); image.setDataHandler(new DataHandler(ds)); image.setHeader("Content-ID", "img");
if (url.toExternalForm().equals(urlDataSource.getURL().toExternalForm())) + "' is already bound to URL " + urlDataSource.getURL() + "; existing names cannot be rebound"); try is = url.openStream(); return embed(new URLDataSource(url), name);
/** * * @param u * @throws IOException */ public void index ( URL u ) throws IOException { try ( InputStream openStream = u.openStream() ) { index(new URLDataSource(u)); } }
public JAXBElement<DataHandler> standardClassDataHandler() { DataSource dataSource = new URLDataSource(getClass().getResource("spring-ws.png")); DataHandler dataHandler = new DataHandler(dataSource); return new JAXBElement<>(NAME, DataHandler.class, dataHandler); }
URLDataSource fds = new URLDataSource( file.getURL() ); logBasic( "Added file '" + fds.getName() + "' to the mail message." ); URLDataSource fds = new URLDataSource( imageFile.getURL() ); messageBodyPart.setDataHandler( new DataHandler( fds ) ); log.logBasic( "Image '" + fds.getName() + "' was embedded in message." );