[英]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. 通过添加文本文件列表来创建和下载zip文件。 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 . 这是使用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 1)声明响应将为application / octet-stream类型
2) @ResponseBody
2)
@ResponseBody
Annotation that indicates a method return value should be bound to the web response body
指示方法返回值的注释应绑定到Web响应正文
3) Declare that the method returns a FileSystemResource
body 3)声明该方法返回
FileSystemResource
主体
4) Return the FileSystemResource
entity based on your created zip file 4)根据您创建的zip文件返回
FileSystemResource
实体
Note that this will still create the file on the server side first, but you may want to use File.createTempFile and File.deleteOnExit . 请注意,这仍将首先在服务器端创建文件,但是您可能要使用File.createTempFile和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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.