首页 > 解决方案 > 创建存储在本地服务器中的文件的 URI 并以 REST JSON 格式发送 URL

问题描述

我想将文件从我的应用程序保存到服务器,然后读取文件的位置并在请求 JSON 中发送 URL,但是每次运行此代码时,我都会收到此错误异常:没有当前的 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;
}

标签: javaspring-bootapirest

解决方案


推荐阅读