将sitemap.xml写入java webapp根目录的权限被拒绝

将sitemap.xml写入java webapp根目录的权限被拒绝,java,tapestry,Java,Tapestry,我正在尝试使用sitemapgen4j库来构建我的站点地图。我在尝试写入根目录时遇到权限问题 根上下文文件夹(/src/main/webapp) 例外情况 Problem writing sitemap file /sitemap.xml java.io.FileNotFoundException /sitemap.xml (Permission denied) 代码 有人知道怎么做吗?我创建一个临时目录来存储站点地图文件,然后在第一次请求时生成它,并为所有后续请求提供相同的版本,因为我的

我正在尝试使用sitemapgen4j库来构建我的站点地图。我在尝试写入根目录时遇到权限问题

根上下文文件夹(/src/main/webapp)

例外情况

Problem writing sitemap file /sitemap.xml 
java.io.FileNotFoundException
/sitemap.xml (Permission denied)
代码


有人知道怎么做吗?

我创建一个临时目录来存储站点地图文件,然后在第一次请求时生成它,并为所有后续请求提供相同的版本,因为我的应用程序中的数据在启动后不会改变

使用临时目录的好处是,您肯定能够写入它(至少我这样认为)

此解决方案使用Spring请求映射。它还希望站点地图文件被写成
sitemap.xml
,除非您有>50k个条目,然后您需要阅读sitemapgen4j文档中关于处理索引文件的内容,并修改此示例

File directory = new File("/");
WebSitemapGenerator wsg = new WebSitemapGenerator("http://localhost:8080/app", directory);
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;
import java.io.BufferedReader;

private Path sitemapDirectory;

@RequestMapping("/sitemap.xml")
public void sitemap(HttpServletResponse response) throws IOException {
    PrintWriter w = response.getWriter();
    boolean isSitemapAlreadyCreated = sitemapDirectory != null;
    if (isSitemapAlreadyCreated) {
        pipeSitemapToResponse(w);
        return;
    }
    sitemapDirectory = Files.createTempDirectory("mySitemap");
    WebSitemapGenerator wsg = new WebSitemapGenerator("http://localhost:8080/app", sitemapDirectory.toFile());
    wsg.addUrl("http://localhost:8080/app/home");
    wsg.write();
    pipeSitemapToResponse(w);
}

private void pipeSitemapToResponse(PrintWriter w) {
    Path sitemap = Paths.get(sitemapDir.toString(), "sitemap.xml");
    Charset charset = Charset.forName("UTF-8");
    try (BufferedReader reader = Files.newBufferedReader(sitemap, charset)) {
        String line = null;
        while ((line = reader.readLine()) != null) {
            w.write(line);
        }
    } catch (IOException e) {
        logger.error("Failed to read the sitemap file.", e);
    }
}