帮我写一个springboot vue 文件夹打包下载本地静态资源的代码实现
时间: 2024-05-11 21:16:17 浏览: 149
好的,以下是代码实现:
前端代码:
```vue
<template>
<div>
<button @click="downloadFile">下载文件</button>
</div>
</template>
<script>
export default {
methods: {
downloadFile() {
window.open("/statics.zip");
},
},
};
</script>
```
后端代码:
```java
package com.example.demo.controller;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Controller
public class FileDownloadController {
@GetMapping("/download/statics")
@ResponseBody
public ResponseEntity<byte[]> downloadStaticResources() throws IOException {
FileSystemResource fileSystemResource =
new FileSystemResource(compressStaticResources());
byte[] fileBytes = new byte[(int) fileSystemResource.getFile().length()];
FileInputStream inputStream = new FileInputStream(fileSystemResource.getFile());
inputStream.read(fileBytes);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "statics.zip");
headers.setContentLength(fileBytes.length);
return ResponseEntity.ok().headers(headers).body(fileBytes);
}
private String compressStaticResources() {
String tempZipPath = System.getProperty("java.io.tmpdir")+"/statics.zip";
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipPath))) {
for (String staticsFolder : new String[]{"static", "public"}) {
try {
addFolderToZip(new ClassPathResource(staticsFolder).getFile(), staticsFolder, zos);
} catch (IOException e) {
e.printStackTrace();
}
}
return tempZipPath;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private void addFolderToZip(File folder, String parentFolderName, ZipOutputStream zipOutputStream) throws IOException {
if (folder.isDirectory()) {
for (File file : folder.listFiles()) {
addFolderToZip(file, parentFolderName, zipOutputStream);
}
} else {
String name = folder.getName();
String path = folder.getPath().replace("\\", "/");
zipOutputStream.putNextEntry(new ZipEntry(path.substring(path.indexOf(parentFolderName), path.length())));
FileInputStream fileInputStream = new FileInputStream(folder);
byte[] bytes = new byte[1024];
int length;
while ((length = fileInputStream.read(bytes)) >= 0) {
zipOutputStream.write(bytes, 0, length);
}
fileInputStream.close();
}
}
}
```
这段代码使用了ZipOutputStream,将指定的静态资源文件夹进行了压缩,并通过@RepositoryRestController注解的Controller接口提供了下载静态资源文件的功能。
你可以在前端页面点击下载文件按钮,将会自动下载静态资源文件。同时,该代码也适用于任意SpringBoot+Vue项目。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)