Commit a1325de663cddab0d626040dd6eaa221ec73a329

Authored by 房远帅
1 parent 966a4040

楚江ERP:客户资信-导出完善

... ... @@ -27,7 +27,6 @@ import com.lframework.xingyun.sc.vo.customer.credit.CreateCustomerCreditVo;
27 27 import com.lframework.xingyun.sc.vo.customer.credit.QueryCorePersonnelVo;
28 28 import com.lframework.xingyun.sc.vo.customer.credit.QueryCustomerCreditVo;
29 29 import com.lframework.xingyun.sc.vo.customer.credit.UpdateCustomerCreditVo;
30   -import freemarker.template.TemplateException;
31 30 import io.swagger.annotations.ApiImplicitParam;
32 31 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
33 32 import com.lframework.starter.common.exceptions.impl.DefaultClientException;
... ... @@ -35,18 +34,15 @@ import io.swagger.annotations.ApiOperation;
35 34 import com.lframework.starter.common.utils.CollectionUtil;
36 35 import io.swagger.annotations.Api;
37 36 import org.springframework.beans.factory.annotation.Autowired;
38   -import org.springframework.core.io.FileSystemResource;
39   -import org.springframework.http.ResponseEntity;
  37 +import org.springframework.beans.factory.annotation.Value;
40 38 import org.springframework.validation.annotation.Validated;
41 39 import org.springframework.web.bind.annotation.*;
42 40 import org.springframework.web.multipart.MultipartFile;
43 41
44 42 import javax.validation.Valid;
45 43 import javax.validation.constraints.NotNull;
46   -import java.io.IOException;
47   -import java.nio.file.Files;
48   -import java.nio.file.Path;
49   -import java.nio.file.Paths;
  44 +import java.io.*;
  45 +import java.net.URLEncoder;
50 46 import java.util.List;
51 47 import java.util.stream.Collectors;
52 48
... ... @@ -70,6 +66,8 @@ public class CustomerCreditController extends DefaultBaseController {
70 66 private SysUserService sysUserService;
71 67 @Autowired
72 68 private RedisHandler redisHandler;
  69 + @Value("${customer.credit.export:/web/service/erp/xingyun/export/templates/}")
  70 + private String exportTemplate;
73 71
74 72 /**
75 73 * 查询列表
... ... @@ -267,33 +265,40 @@ public class CustomerCreditController extends DefaultBaseController {
267 265
268 266 /**
269 267 * 导出
  268 + *
270 269 * @return
271 270 */
272 271 @ApiOperation("导出")
273 272 @HasPermission({"customer-dev-manage:customer-dev-plan:export"})
274   - @PostMapping("/export")
275   - public ResponseEntity<FileSystemResource> export(@NotBlank(message = "ID不能为空") String id, HttpServletResponse response) {
  273 + @GetMapping("/export")
  274 + public void export(@NotBlank(message = "ID不能为空") String id, HttpServletResponse response) {
276 275 try {
277 276 String fileName = customerCreditService.export(id);
278   - Path filePath = Paths.get("/web/service/erp/xingyun/export/templates", fileName);
279   -
280   - if (Files.exists(filePath)) {
281   - // 创建FileSystemResource实例,用于读取文件
282   - FileSystemResource file = new FileSystemResource(filePath.toString());
283   -
284   - // 返回ResponseEntity对象,设置响应头以便浏览器知道这是一个文件下载请求
285   - return ResponseEntity.ok()
286   - .header("Content-Disposition", "attachment; filename=" + file.getFilename())
287   - .body(file);
288   - } else {
289   - response.sendError(404, "文件未找到");
  277 + String tmpLocation = exportTemplate + fileName;
  278 + // 创建文件对象
  279 + File file = new File(tmpLocation);
  280 + // 检查文件是否存在
  281 + if (!file.exists()) {
  282 + throw new DefaultClientException("文件不存在!");
  283 + }
  284 + // 设置响应头
  285 + response.setContentType("application/octet-stream");
  286 + response.setHeader("Content-Disposition", "attachment; filename=" +
  287 + URLEncoder.encode(file.getName(), "UTF-8"));
  288 + response.setContentLengthLong(file.length());
  289 + // 读取文件并写入响应输出流
  290 + try (FileInputStream fis = new FileInputStream(file);
  291 + OutputStream os = response.getOutputStream()) {
  292 + byte[] buffer = new byte[1024];
  293 + int len;
  294 + while ((len = fis.read(buffer)) > 0) {
  295 + os.write(buffer, 0, len);
  296 + }
  297 + os.flush();
290 298 }
291   - } catch (IOException e) {
292   - e.printStackTrace();
293   - } catch (TemplateException e) {
294   - e.printStackTrace();
  299 + } catch (Exception e) {
  300 + throw new RuntimeException(e);
295 301 }
296   - return null;
297 302 }
298 303
299 304
... ...
... ... @@ -47,20 +47,17 @@ import freemarker.template.TemplateExceptionHandler;
47 47 import org.apache.commons.collections.CollectionUtils;
48 48 import org.apache.commons.lang3.StringUtils;
49 49 import org.springframework.beans.factory.annotation.Autowired;
  50 +import org.springframework.beans.factory.annotation.Value;
50 51 import org.springframework.cache.annotation.CacheEvict;
51 52 import org.springframework.cache.annotation.Cacheable;
52 53 import org.springframework.stereotype.Service;
53 54 import org.springframework.transaction.annotation.Transactional;
54 55 import net.sourceforge.pinyin4j.PinyinHelper;
55 56 import javax.annotation.Resource;
56   -import java.lang.reflect.InvocationTargetException;
57   -import java.lang.reflect.Method;
58   -import java.lang.reflect.Modifier;
59 57 import java.nio.charset.StandardCharsets;
60 58 import java.nio.file.Files;
61 59 import java.nio.file.Path;
62 60 import java.nio.file.Paths;
63   -import java.time.format.DateTimeFormatter;
64 61 import java.util.*;
65 62 import java.util.stream.Collectors;
66 63 import java.util.stream.IntStream;
... ... @@ -94,6 +91,8 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM
94 91 private FlowTaskWrapperMapper flowTaskWrapperMapper;
95 92 @Resource
96 93 private SysUserDeptService sysUserDeptService;
  94 + @Value("${customer.credit.export:/web/service/erp/xingyun/export/templates/}")
  95 + private String exportTemplate;
97 96
98 97 @Override
99 98 public PageResult<CustomerCredit> query(Integer pageIndex, Integer pageSize, QueryCustomerCreditVo vo) {
... ... @@ -1333,9 +1332,7 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM
1333 1332 StringWriter stringWriter = new StringWriter();
1334 1333 template.process(data, stringWriter);
1335 1334 String processedXml = stringWriter.toString();
1336   - // 1. 定义服务器导出目录
1337   - String exportDir = "/web/service/erp/xingyun/export/templates";
1338   - Path dirPath = Paths.get(exportDir);
  1335 + Path dirPath = Paths.get(exportTemplate);
1339 1336 // 2. 创建目录(如果不存在)
1340 1337 if (!Files.exists(dirPath)) {
1341 1338 Files.createDirectories(dirPath);
... ...