下载模板

This commit is contained in:
sxu 2025-03-28 15:54:12 +08:00
parent afd057e868
commit 67696c8894
3 changed files with 42 additions and 0 deletions

View File

@ -4,14 +4,19 @@ import com.bonus.canteen.core.common.export.dto.ExportRecordQueryDTO;
import com.bonus.canteen.core.common.export.service.ExportRecordService;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping({"/api/v4/export"})
@ -32,4 +37,11 @@ public class ExportRecordController {
this.exportRecordService.stopAndDelete(dto);
return AjaxResult.success();
}
@ApiOperation(value = "模版下载")
@PostMapping("/downLoad")
public void downLoadExcelFile(@RequestBody ExportRecordQueryDTO dto) {
HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
exportRecordService.downLoadTemplate(resp, dto.getFileName());
}
}

View File

@ -18,6 +18,7 @@ import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import com.github.pagehelper.page.PageMethod;
import org.apache.commons.io.IOUtils;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -25,6 +26,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
@ -186,4 +191,29 @@ public class ExportRecordService {
.set(ExportRecord::getExportFileState, state)
.eq(ExportRecord::getExportId, exportId));
}
public void downLoadTemplate(HttpServletResponse response, String fileName) {
OutputStream out = null;
InputStream input = null;
try {
input = this.getClass().getClassLoader().getResourceAsStream("template/" + fileName);
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName).getBytes(), "iso-8859-1"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
out = response.getOutputStream();
byte[] buffer = new byte[1024]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入内容输出到浏览器中
while ((bytesToRead = input.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(out);
}
}
}