Get a URL for a Resource

Hi,
I am working on a generator of pdf with flying saucer and i need to have access to a url in order to display an image.
As file resources can not be retrieved by a regular URL from the server i tried to store the image in temporary folder and get the url of this temporary image.
I am not sure of how to achieve that. I tried to use java createTempFile but it doesn’t store the temp file online.

Here is what i tried.


						URI uriSchema = new URI(previousImageField.getSingleData(String.class));
						File schema = (localStorageService.getFile(uriSchema)
						BufferedImage img = ImageIO.read(schema);
						File schemaTemp = File.createTempFile("schema",".png");
						ImageIO.write(img,"png",schemaTemp);
						schemaTemp.deleteOnExit();
                        schemaTempPath = schemaTemp.getAbsolutePath();

Thanks a lot,
Clement

You need to create a Resource. So there are two possibilities:

  1. write the image to byte output stream:
		final ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE);
		document.write(output);
		output.flush();
		output.close();
		return new StreamResource(new StreamSource() {

			@Override
			public InputStream getStream() {
				return new ByteArrayInputStream(output.toByteArray());
			}
		}, "document.doc");
  1. or to temporary file:[code]
    File tmpFile = File.createTempFile(“document”, “.doc”);
    tmpFile.deleteOnExit();
    try {
    document.save(tmpFile);
    } catch (Docx4JException e) {
    throw new IOException(e);
    }
    return new FileResource(tmpFile);

[/code]