从目录中进行邮政编码并在春季下载而不创建新文件或目录

发布于 2025-01-26 02:39:19 字数 1681 浏览 3 评论 0原文

我有一个示例目录,称为'sample-directory'(在资源中),其中包含文件和目录。

这是我的终点:

public void downloadZipFile(@RequestBody final Parameters parameter, final HttpServletResponse response) throws FileReadingException, IOException, URISyntaxException {
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader(CONTENT_DISPOSITION, HEADER_VALUE_STR);
    response.setStatus(HttpServletResponse.SC_OK);
    this.generateZipService.generateZipFile(parameter, response);
}

在我的this.generatezipservice.generatezipfile(参数,resposne)方法中,我想从'sample-directory'中读取所有文件目录(包括其中的所有目录和文件,包括更改)。

有人可以建议我还是给出指导我该怎么做?

我想在不创建新文件或目录的情况下这样做。

这样:

for all files {
   if(currentFile is "TEST1.xml") {
            change first Line here;
   }
   if(currentFile is "TEST2.xml") {
            change second Line here;
   }
}
Then zip full directory and download.

我也不想使用/src/main/resources或类似的路径。 相反,我正在使用getClass()。getClassLoader()。getResource(“ folderDirectlyInsideresources/otherepackages/file.txt”);

我已经开始通过:

FileOutputStream fout = new FileOutputStream("src/main/resources/generate.zip");
ZipOutputStream zout = new ZipOutputStream(fout);

File folder = new File("src/main/resources/docker-compose-zip");
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
    ZipEntry ze = new ZipEntry(file.getName());
    zout.putNextEntry(ze);
    zout.closeEntry();
}
zout.close();

但是它下载它generate.zip files.zip文件,不是不是zip(也许是.txt格式)。但是我正在使用mediatype.application_octet_stream_value

I have a sample directory called 'sample-directory'(in resources) with files and directories in it.

this is my endpoint:

public void downloadZipFile(@RequestBody final Parameters parameter, final HttpServletResponse response) throws FileReadingException, IOException, URISyntaxException {
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader(CONTENT_DISPOSITION, HEADER_VALUE_STR);
    response.setStatus(HttpServletResponse.SC_OK);
    this.generateZipService.generateZipFile(parameter, response);
}

In my this.generateZipService.generateZipFile(parameter, resposne) method, I want to read all files from 'sample-directory', change some files here and download a zip file from this directory(includng all directories and files in it, including changes).

Can someone suggest me or give direction how can I do this?

I want to do it without creating new files or directories.

like this:

for all files {
   if(currentFile is "TEST1.xml") {
            change first Line here;
   }
   if(currentFile is "TEST2.xml") {
            change second Line here;
   }
}
Then zip full directory and download.

Also I don't want to use /src/main/resources or paths like that.
instead I'm using getClass().getClassLoader().getResource("folderDirectlyInsideResources/otherPackages/file.txt");

I have started implementing this by:

FileOutputStream fout = new FileOutputStream("src/main/resources/generate.zip");
ZipOutputStream zout = new ZipOutputStream(fout);

File folder = new File("src/main/resources/docker-compose-zip");
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
    ZipEntry ze = new ZipEntry(file.getName());
    zout.putNextEntry(ze);
    zout.closeEntry();
}
zout.close();

but it downloads generate.zip file which is not zip(it is .txt format maybe). But I'm using MediaType.APPLICATION_OCTET_STREAM_VALUE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文