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) {
        ex.getStackTrace();
    }
    
    String URL = getFileURL(filename);
}

public String getFileURL(String fileName){
    
    String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/downloadFile/")
            .path(fileName)
            .toUriString();
    
    return fileDownloadUri;
}


Read more here: https://stackoverflow.com/questions/67931891/create-uri-of-file-stored-in-local-server-and-send-the-url-in-rest-json

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: