【问题标题】:Create text file and add to zip file and download spring boot with out savind in local server创建文本文件并添加到zip文件并下载spring boot而不保存在本地服务器中
【发布时间】:2018-10-05 07:33:29
【问题描述】:

通过添加文本文件列表来创建和下载 zip 文件。无需在本地服务器中创建文件,应直接在客户端下载, 在这里我添加了一个代码 sn-p,它是在本地服务器中创建的,但我不希望这样,它应该在客户端即时创建和下载。请以这种方式帮助我..

@GetMapping("/download/rawdata")
    public void downloadRawdata(@RequestParam("date") String date){

        log.info("date : "+date);
        List<Rawdata> rawdatas = rawdataRepoisotry.findRawdataByDate(date);
        log.info("size of rawdata : "+rawdatas.size());
        List<File> files = new ArrayList<File>();
        int i = 1;
        for(Rawdata rawdata : rawdatas){
        log.info("rawdata : "+ rawdata.getRawdata());
            File file = new File(i+".txt");

            try (Writer writer = new BufferedWriter(new FileWriter(file))) {
                String contents = rawdata.getRawdata(); 
                writer.write(contents);
                files.add(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            i++;
        }

        try {
            zipFile(files, new File(date+".zip"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed while creating Zip file");
        }

    }


    public FileOutputStream zipFile(final List<File> files, final File targetZipFile) throws IOException {
        try {
          FileOutputStream   fos = new FileOutputStream(targetZipFile);
          ZipOutputStream zos = new ZipOutputStream(fos);
          byte[] buffer = new byte[128];
          for(File currentFile : files){
                if (!currentFile.isDirectory()) {
                  ZipEntry entry = new ZipEntry(currentFile.getName());
                  FileInputStream fis = new FileInputStream(currentFile);
                  zos.putNextEntry(entry);
                  int read = 0;
                  while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                  }
                  zos.closeEntry();
                  fis.close();
                }
          }
          zos.close();
          fos.close();
          return fos;
        } catch (FileNotFoundException e) {
          System.out.println("File not found : " + e);
          throw new FileNotFoundException();
        }


      }

【问题讨论】:

    标签: java spring-boot download zipfile


    【解决方案1】:

    这是一个使用FileSystemResource的示例。

    修改的是(见注释代码中的数字):

    1) 声明响应的类型为 application/octet-stream

    2)@ResponseBody

    表示方法返回值的注解应该绑定到 网络响应正文

    3) 声明该方法返回一个FileSystemResource body

    4) 根据您创建的 zip 文件返回 FileSystemResource 实体

    请注意,这仍会首先在服务器端创建文件,但您可能需要使用File.createTempFileFile.deleteOnExit

        @GetMapping("/download/rawdata", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)//1
        @ResponseBody //2
        public ResponseEntity<FileSystemResource> downloadRawdata(@RequestParam("date") String date){ //3
    
            log.info("date : "+date);
            List<Rawdata> rawdatas = rawdataRepoisotry.findRawdataByDate(date);
            log.info("size of rawdata : "+rawdatas.size());
            List<File> files = new ArrayList<File>();
            int i = 1;
            for(Rawdata rawdata : rawdatas){
            log.info("rawdata : "+ rawdata.getRawdata());
                File file = new File(i+".txt");
    
                try (Writer writer = new BufferedWriter(new FileWriter(file))) {
                    String contents = rawdata.getRawdata(); 
                    writer.write(contents);
                    files.add(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                i++;
            }
    
            try {
    
                File resultFile = new File(date+".zip");
                zipFile(files, resultFile);
    
                return new ResponseEntity<>(new FileSystemResource(resultFile), HttpStatus.OK); //4
    
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Failed while creating Zip file");
            }
    
        }
    

    【讨论】:

    • 感谢您的快速回复.. 但我想直接在客户端下载而不在服务器端创建文件夹,请帮助我
    • 有没有其他方法可以不用在服务器端创建直接下载
    • 好吧,如果你想在内存中做所有事情,你可能想使用 ByteArrayOutputStream 作为 ZipOutputStream 的参数,然后返回一个从填充的字节数组中读取的 ResponseEntity&lt;ByteArrayResource&gt; (检索自ByteArrayOutputStream.toByteArray())。
    • 其实我也不想在服务器端创建文本文件
    • 有人知道答案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多