Create URI of file stored in local server and send the URL in REST JSON

I wants to save a file from my application to server and then read the location of the file and send the URL in request JSON, but every time I run this code I got this error Exception :No current ServletRequestAttributes.

public String createFile(){
    try {
        String filename = "output.log";
        Path path = Paths.get(filename);
        byte[] bytes = contents.getBytes();
        Files.write(path, bytes);
    } catch (IOException ex) {
    String URL = getFileURL(filename);

public String getFileURL(String fileName){
    String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
    return fileDownloadUri;

Read more here:

Content Attribution

This content was originally published by Arjun Gupta at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: