也许有点晚了(4年后),但是对于其他正在寻找类似解决方案的人,您可以使用我创建的URL工厂:
public class InMemoryURLFactory { public static void main(String... args) throws Exception { URL url = InMemoryURLFactory.getInstance().build("/this/is/a/test.txt", "This is a test!"); byte[] data = IOUtils.toByteArray(url.openConnection().getInputStream()); // Prints out: This is a test! System.out.println(new String(data)); } private final Map<URL, byte[]> contents = new WeakHashMap<>(); private final URLStreamHandler handler = new InMemoryStreamHandler(); private static InMemoryURLFactory instance = null; public static synchronized InMemoryURLFactory getInstance() { if(instance == null) instance = new InMemoryURLFactory(); return instance; } private InMemoryURLFactory() { } public URL build(String path, String data) { try { return build(path, data.getBytes("UTF-8")); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } } public URL build(String path, byte[] data) { try { URL url = new URL("memory", "", -1, path, handler); contents.put(url, data); return url; } catch (MalformedURLException ex) { throw new RuntimeException(ex); } } private class InMemoryStreamHandler extends URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { if(!u.getProtocol().equals("memory")) { throw new IOException("Cannot handle protocol: " + u.getProtocol()); } return new URLConnection(u) { private byte[] data = null; @Override public void connect() throws IOException { initDataIfNeeded(); checkDataAvailability(); // Protected field from superclass connected = true; } @Override public long getContentLengthLong() { initDataIfNeeded(); if(data == null) return 0; return data.length; } @Override public InputStream getInputStream() throws IOException { initDataIfNeeded(); checkDataAvailability(); return new ByteArrayInputStream(data); } private void initDataIfNeeded() { if(data == null) data = contents.get(u); } private void checkDataAvailability() throws IOException { if(data == null) throw new IOException("In-memory data cannot be found for: " + u.getPath()); } }; } }}


