springboot+vue打包成zip文件并且下载
ruoyi框架下开发的,使用了hutool工具包自己记录一下。前提是文件已经存放在后台服务器上。如果是其他框架或者流文件, 参照博客:vue+springboot压缩多个文件、压缩包下载、单文件下载 - 代码先锋网 (codeleading.com)前段vuedownloadExpress(row) {const uuids = row.uuid || this.ids;this.$downloa
·
ruoyi框架下开发的,使用了
hutool工具包
自己记录一下。前提是文件已经存放在后台服务器上。如果是其他框架或者流文件, 参照博客:
vue+springboot压缩多个文件、压缩包下载、单文件下载 - 代码先锋网 (codeleading.com)
前段vue
downloadExpress(row) {
const uuids = row.uuid || this.ids;
this.$download.zip("/business/hawbbill/downloadExpress?uuids=" + uuids, new Date().getTime());
}
后段:
controller:
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.json.JSONObject;
import com.lh.common.annotation.Log;
import com.lh.common.config.RuoYiConfig;
import com.lh.common.core.controller.BaseController;
import com.lh.common.core.domain.AjaxResult;
import com.lh.common.core.page.TableDataInfo;
import com.lh.common.core.text.Convert;
import com.lh.common.enums.BusinessType;
import com.lh.common.utils.DictUtils;
import com.lh.common.utils.StringUtils;
import com.lh.common.utils.poi.ExcelUtil;
import com.lh.common.utils.uuid.IdUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/business/hawbbill")
public class HouseAirWaybillController extends BaseController {
@Autowired
private IHouseAirWaybillService houseAirWaybillService;
@Log(title = "下载zip", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('business:waybill:express')")
@GetMapping("/downloadExpress")
public void downloadExpress(HttpServletResponse response, String uuids) throws Exception {
String[] fileIds = Convert.toStrArray(uuids);
int size = fileIds.length;
if (size == 0 || fileIds == null) {
throw new Exception("下载文件id数组为空!");
}
//获取包裹信息
List<HouseAirWaybill> houseAirWaybillList = this.houseAirWaybillService.selectHouseAirWaybillListByUuids(fileIds, false, false);
//1、创建一个时间戳 将每次下载的文件夹区分开
long l = System.currentTimeMillis();
// 本次文件操作的根目录,操作完成之后删除
String BASE_FILE_ROOT_PATH = RuoYiConfig.getDownloadPath() + l;
// 存放复制的文件夹临时路径
String FILE_ROOT_PATH = BASE_FILE_ROOT_PATH + File.separator + "zipTemp";
// 存放压缩之后的zip存放路径
String ZIP_PATH = BASE_FILE_ROOT_PATH + File.separator + l + ".zip";
for (int i = 0; i < houseAirWaybillList.size(); i++) {
HouseAirWaybill houseAirWaybill = houseAirWaybillList.get(i);
String fileName = houseAirWaybill.getHouseNo() + ".pdf";
// 2、复制文件到制定文件夹
FileUtil.copy(houseAirWaybill.getFileUrl(), FILE_ROOT_PATH + File.separator + fileName, true);
}
// 3、压缩已经写入文件的文件夹,并获得压缩包路径
String zipPath = downZIP(FILE_ROOT_PATH, ZIP_PATH);
//下载文件
byte[] data = Files.readAllBytes(Paths.get(zipPath));
res(response, data);
// 4、先清空要被压缩的的文件夹
FileUtil.del(new File(BASE_FILE_ROOT_PATH));
}
/**
* @param s 被压缩的文件夹路径
* @param zipPath 压缩后生成的文件路径
* @Author Mr Pan
* @Description 压缩文件
* @Date 3:47 下午 2022/3/15
* @Return java.lang.String
**/
public static String downZIP(String s, String zipPath) {
//ZipUtil.zip()是一个工具类,具体看图一的import
File file = ZipUtil.zip(s, zipPath);
return file.getAbsolutePath();
}
/**
* 生成zip文件
*/
private void res(HttpServletResponse response, byte[] data) throws IOException {
response.reset();
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Content-Disposition", "attachment;");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream; charset=UTF-8");
IOUtils.write(data, response.getOutputStream());
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)