简体   繁体   中英

Create text file and add to zip file and download spring boot with out savind in local server

create and download zip file by adding list of text files. with out creating the file in local server, it should be download at client side direct, Here i added a code snippet, it was creating in local server, but i dont want that, it should create and download at client side instant. Please help me in this way..

@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();
        }


      }

Here is an example using FileSystemResource .

What has been modified is (see the numbers in the commented code ) :

1) Declare that the response will be of type application/octet-stream

2) @ResponseBody

Annotation that indicates a method return value should be bound to the web response body

3) Declare that the method returns a FileSystemResource body

4) Return the FileSystemResource entity based on your created zip file

Note that this will still create the file on the server side first, but you may want to use File.createTempFile and File.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");
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM