diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java new file mode 100644 index 00000000..b7aaaa1c --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysFileController.java @@ -0,0 +1,92 @@ +package com.bonus.sgzb.system.controller; + +import com.bonus.sgzb.common.core.utils.StringHelper; +import com.bonus.sgzb.common.core.web.domain.AjaxResult; +import com.bonus.sgzb.system.domain.FileInfo; +import com.bonus.sgzb.system.service.SysFileService; +import io.swagger.annotations.ApiOperation; +import org.apache.poi.util.IOUtils; +import org.springframework.core.io.ResourceLoader; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +@RestController +@RequestMapping("/sys/file") +public class SysFileController { + + @Resource + private SysFileService service; + + @Resource + private ResourceLoader resourceLoader; + + + @PostMapping("/upload") + @ApiOperation(value = "文件上传") + public AjaxResult upload(HttpServletRequest request) { + String limitWords = request.getParameter("limitWords"); + FileInfo file = new FileInfo(); + try { + file = service.uploadFile(request); + if (limitWords != null && file.getWords() > Integer.parseInt(limitWords)){ + return AjaxResult.error("上传文件字数超出限制字数!"); + } + }catch (Exception e){ + e.printStackTrace(); + } + if (file != null && file.getId() != 0){ + return AjaxResult.success(file); + }else { + return AjaxResult.error("文件上传失败!"); + } + + } + + @ApiOperation(value = "模板", httpMethod = "GET") + @GetMapping("download") + public void download(HttpServletRequest request, HttpServletResponse response, String filename) { + InputStream inputStream = null; + ServletOutputStream servletOutputStream = null; + try { + String path = "download/" + filename; + org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:" + path); + response.setContentType("application/vnd.ms-excel"); + response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + response.addHeader("charset", "utf-8"); + response.addHeader("Pragma", "no-cache"); + String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); + response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); + + inputStream = resource.getInputStream(); + servletOutputStream = response.getOutputStream(); + IOUtils.copy(inputStream, servletOutputStream); + response.flushBuffer(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (servletOutputStream != null) { + servletOutputStream.close(); + } + if (inputStream != null) { + inputStream.close(); + } + // 召唤jvm的垃圾回收器 + System.gc(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/FileInfo.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/FileInfo.java new file mode 100644 index 00000000..aaad32f6 --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/domain/FileInfo.java @@ -0,0 +1,40 @@ +package com.bonus.sgzb.system.domain; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.Date; + +@Data +public class FileInfo { + + /** $column.columnComment */ + private Long id; + + /** 模块id */ + private Long modelId; + /** 文件名称 */ + @ApiModelProperty(value = "文件名称") + private String fileName; + + /** 文件路径 */ + @ApiModelProperty(value = "文件路径") + private String fileUrl; + + /** 数据字典 */ + @ApiModelProperty(value = "数据字典") + private Long dicId; + + /** 创建者 */ + private String createBy; + + /** 创建时间 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createTime; + private String size; + private String type; + private int words; + private String creator; + +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/FileInfoMapper.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/FileInfoMapper.java new file mode 100644 index 00000000..fc1a4522 --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/mapper/FileInfoMapper.java @@ -0,0 +1,17 @@ +package com.bonus.sgzb.system.mapper; + +import com.bonus.sgzb.system.domain.FileInfo; + +/** + *
+ * Mapper 接口 + *
+ * + * @author laker + * @since 2021-08-05 + */ +public interface FileInfoMapper { + + int insertFileInfo(FileInfo o); + +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/SysFileService.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/SysFileService.java new file mode 100644 index 00000000..0f70509d --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/SysFileService.java @@ -0,0 +1,23 @@ +package com.bonus.sgzb.system.service; + + +import com.bonus.sgzb.system.domain.FileInfo; + +import javax.servlet.http.HttpServletRequest; + +/** + * 文件上传接口 + * + * @author zys + */ +public interface SysFileService +{ + /** + * 文件上传接口 + * + * @return + * @throws Exception + */ + FileInfo uploadFile(HttpServletRequest request) throws Exception; + +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java new file mode 100644 index 00000000..62ec5e23 --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysFileServiceImpl.java @@ -0,0 +1,179 @@ +package com.bonus.sgzb.system.service.impl; + +import cn.hutool.core.util.IdUtil; +import com.bonus.sgzb.common.core.utils.DateTimeHelper; +import com.bonus.sgzb.common.security.utils.SecurityUtils; +import com.bonus.sgzb.system.api.model.LoginUser; +import com.bonus.sgzb.system.domain.FileInfo; +import com.bonus.sgzb.system.mapper.FileInfoMapper; +import com.bonus.sgzb.system.service.SysFileService; +import org.apache.poi.hwpf.extractor.WordExtractor; +import org.apache.poi.openxml4j.util.ZipSecureFile; +import org.apache.poi.xwpf.extractor.XWPFWordExtractor; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.Security; +import java.util.*; + +/** + * 本地文件存储 + * + * @author zys + */ +@Primary +@Service("SysFileService") +public class SysFileServiceImpl implements SysFileService { + + @Resource + private FileInfoMapper dao; + + /** + * 本地文件上传接口 + * + * @return 访问地址 + * @throws Exception + */ + @Override + public FileInfo uploadFile(HttpServletRequest request) throws Exception { + FileInfo file = new FileInfo(); + StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request; + String photoType = req.getParameter("fileType"); + String userId = req.getParameter("userId"); + HashMap