init project
This commit is contained in:
34
6.program/wms-common/pom.xml
Normal file
34
6.program/wms-common/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>wms</artifactId>
|
||||
<groupId>com.mt</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>wms-common</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mt</groupId>
|
||||
<artifactId>wms-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>${aliyun-java-sdk-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
|
||||
<version>${aliyun-java-sdk-dysmsapi.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.coobird</groupId>
|
||||
<artifactId>thumbnailator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.mt.wms.common.controller;
|
||||
|
||||
import com.mt.wms.common.params.DownloadFileParam;
|
||||
import com.mt.wms.common.params.UploadFileBase64Param;
|
||||
import com.mt.wms.common.service.AttachmentService;
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.base.BaseController;
|
||||
import com.mt.wms.core.constants.CommonConstant;
|
||||
import com.mt.wms.core.vo.R;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jiff
|
||||
* @date 2021/01/12
|
||||
* @since 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(CommonConstant.API_MODULE_COMMON + "attachment")
|
||||
@Slf4j
|
||||
@Api(value = "附件管理", tags = "附件管理", hidden = false)
|
||||
public class AttachmentController extends BaseController {
|
||||
@Autowired
|
||||
private AttachmentService attachmentService;
|
||||
|
||||
|
||||
@PostMapping(value = "uploadFile")
|
||||
@ApiOperation(value = "上传文件", notes = "上传文件")
|
||||
public R<List<SysFileVo>> uploadFile(@ApiParam(value = "文件类型", required = true) @NotEmpty(message = "文件类型编码不得为空") @RequestParam String typeCode) {
|
||||
return attachmentService.uploadFile(typeCode);
|
||||
}
|
||||
|
||||
@PostMapping("uploadFileBase64")
|
||||
@ApiOperation(value = "文件上传(base64字符串方式)", notes = "文件上传")
|
||||
public R<SysFileVo> uploadFileBase64(@Validated @RequestBody UploadFileBase64Param param) {
|
||||
return attachmentService.uploadFileBase64(param);
|
||||
}
|
||||
|
||||
@PostMapping("uploadFileFormData")
|
||||
@ApiOperation(value = "文件上传(form表单形式)", notes = "文件上传")
|
||||
public R<List<SysFileVo>> uploadFileFormData(@ApiParam(value = "文件类型", required = true) @NotEmpty(message = "文件类型编码不得为空") @RequestParam String typeCode, @ApiParam(value = "文件附件", required = true) @NotNull(message = "文件附件不能为空") @RequestParam("files") MultipartFile[] files) {
|
||||
return attachmentService.uploadFileFormData(typeCode, files);
|
||||
}
|
||||
|
||||
@GetMapping(value = "downloadFile")
|
||||
@ApiOperation(value = "下载文件", notes = "下载文件")
|
||||
public R<Object> downloadFile(@Validated DownloadFileParam param) {
|
||||
return attachmentService.downloadFile(param);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.mt.wms.common.controller;
|
||||
|
||||
import com.mt.wms.common.service.CommonService;
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.base.BaseController;
|
||||
import com.mt.wms.core.constants.CommonConstant;
|
||||
import com.mt.wms.core.params.IdParam;
|
||||
import com.mt.wms.core.vo.R;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </P>
|
||||
*
|
||||
* @author FanYi
|
||||
* @date 2020/7/6
|
||||
* @since 1.0
|
||||
*/
|
||||
@Api(value = "公共模块", tags = "公共模块")
|
||||
@RestController
|
||||
@RequestMapping(CommonConstant.API_MODULE_COMMON)
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CommonController extends BaseController {
|
||||
private final CommonService commonService;
|
||||
|
||||
|
||||
@PostMapping(value = "downloadFile")
|
||||
@ApiOperation(value = "获取附件下载地址", notes = "获取附件下载地址")
|
||||
public R<SysFileVo> downloadFile(@Validated @RequestBody IdParam idParam) {
|
||||
return successful(commonService.downloadFile(idParam.getId()));
|
||||
}
|
||||
|
||||
@GetMapping(value = "downLoadFileUrl")
|
||||
@ApiOperation(value = "文件的预览", notes = "文件的预览")
|
||||
public R<Object> downloadFile(@Validated IdParam idParam, HttpServletResponse response) {
|
||||
return commonService.downLoadFileUrl(idParam, response);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.mt.wms.common.mapper;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </P>
|
||||
*
|
||||
* @author FanYi
|
||||
* @date 2020/7/6
|
||||
* @since 1.0
|
||||
*/
|
||||
@Repository
|
||||
public interface CommonMapper {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2020.
|
||||
* http://www.ulabcare.com
|
||||
*/
|
||||
|
||||
package com.mt.wms.common.params;
|
||||
|
||||
import com.mt.wms.core.base.BaseParam;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 下载文件对象入参
|
||||
*
|
||||
* @author jiff
|
||||
* @date 2021/01/12
|
||||
* @since 1.0
|
||||
**/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Accessors(chain = true)
|
||||
@Data
|
||||
@ApiModel(value = "下载文件信息对象", description = "下载文件信息对象")
|
||||
public class DownloadFileParam extends BaseParam {
|
||||
|
||||
@ApiModelProperty(value = "附件ID", example = "1")
|
||||
@NotNull(message = "附件ID不能为空")
|
||||
private Long attachmentId;
|
||||
|
||||
@ApiModelProperty(value = "下载方式: 0、预览,1、下载", example = "0")
|
||||
@NotNull(message = "下载方式不能为空")
|
||||
private String type;
|
||||
|
||||
@ApiModelProperty(value = "下载文件自定义文件名", example = "报销发票")
|
||||
private String fileName;
|
||||
|
||||
@ApiModelProperty(value = "压缩尺寸,取值范围(0.00--1.00),图片文件有效", example = "1")
|
||||
private float scale = 1f;
|
||||
|
||||
@ApiModelProperty(value = "压缩质量,取值范围(0.00--1.00),图片文件有效", example = "1")
|
||||
private float outputQuality = 1f;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.mt.wms.common.params;
|
||||
|
||||
import com.mt.wms.core.base.BaseParam;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 文件上传base64字符串方式
|
||||
* </p>
|
||||
*
|
||||
* @author jiff
|
||||
* @date 2021/01/12
|
||||
* @since 1.0
|
||||
**/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Accessors(chain = true)
|
||||
@Data
|
||||
@ApiModel("文件上传base64字符串方式")
|
||||
public class UploadFileBase64Param extends BaseParam {
|
||||
|
||||
@ApiModelProperty("文件类型编码")
|
||||
@NotEmpty(message = "文件类型编码不能为空")
|
||||
private String typeCode;
|
||||
|
||||
@ApiModelProperty("文件名")
|
||||
@NotEmpty(message = "文件名不能为空")
|
||||
private String fileName;
|
||||
|
||||
@ApiModelProperty("base64字符串")
|
||||
@NotEmpty(message = "文件base64字符串不能为空")
|
||||
private String fileBase64Str;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.mt.wms.common.service;
|
||||
|
||||
import com.mt.wms.common.params.DownloadFileParam;
|
||||
import com.mt.wms.common.params.UploadFileBase64Param;
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.params.IdParam;
|
||||
import com.mt.wms.core.vo.R;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jiff
|
||||
* @date 2021/01/12
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface AttachmentService {
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传(支持多文件上传)
|
||||
*
|
||||
* @param typeCode
|
||||
* @return
|
||||
*/
|
||||
R<List<SysFileVo>> uploadFile(String typeCode);
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param uploadFileBase64Param 上传文件对象
|
||||
* @return
|
||||
*/
|
||||
R<SysFileVo> uploadFileBase64(UploadFileBase64Param uploadFileBase64Param);
|
||||
|
||||
/**
|
||||
* 文件上传(form表单形式)
|
||||
*
|
||||
* @param typeCode
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
R<List<SysFileVo>> uploadFileFormData(String typeCode, MultipartFile[] files);
|
||||
|
||||
/**
|
||||
* 文件下载(下载方式: 0、预览,1、下载)
|
||||
*
|
||||
* @param downloadFileParam
|
||||
* @return
|
||||
*/
|
||||
R<Object> downloadFile(DownloadFileParam downloadFileParam);
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.mt.wms.common.service;
|
||||
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.params.IdParam;
|
||||
import com.mt.wms.core.vo.R;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </P>
|
||||
*
|
||||
* @author FanYi
|
||||
* @date 2020/7/6
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface CommonService {
|
||||
|
||||
|
||||
/**
|
||||
* 获取附件下载地址
|
||||
*
|
||||
* @param fileId 附件ID
|
||||
* @return 附件下载地址
|
||||
*/
|
||||
SysFileVo downloadFile(Long fileId);
|
||||
|
||||
/**
|
||||
* 附件预览
|
||||
*
|
||||
* @param idParam 附件ID
|
||||
* @return 附件下载地址
|
||||
*/
|
||||
R<Object> downLoadFileUrl(IdParam idParam, HttpServletResponse response);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package com.mt.wms.common.service.impl;
|
||||
|
||||
import com.mt.wms.common.params.DownloadFileParam;
|
||||
import com.mt.wms.common.params.UploadFileBase64Param;
|
||||
import com.mt.wms.common.service.AttachmentService;
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.base.BaseService;
|
||||
import com.mt.wms.core.config.CommonConfig;
|
||||
import com.mt.wms.core.dal.entity.SysFile;
|
||||
import com.mt.wms.core.dal.service.SysFileServiceBiz;
|
||||
import com.mt.wms.core.utils.*;
|
||||
import com.mt.wms.core.vo.R;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jiff
|
||||
* @date 2021/1/12
|
||||
* @since 1.0
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class AttachmentServiceImpl extends BaseService implements AttachmentService {
|
||||
|
||||
@Autowired
|
||||
private SysFileServiceBiz sysFileServiceBiz;
|
||||
@Autowired
|
||||
private CommonConfig commonConfig;
|
||||
|
||||
/**
|
||||
* 文件上传(支持多文件上传)
|
||||
*
|
||||
* @param typeCode
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R<List<SysFileVo>> uploadFile(String typeCode) {
|
||||
//获取上传文件路径
|
||||
String uploadPath = commonConfig.getUploadPath() + typeCode + "/";
|
||||
log.debug("文件的上传路径:" + uploadPath);
|
||||
File file = new File(uploadPath);
|
||||
//判断文件是否存在,不存在则创建
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
List<SysFile> fileList = new ArrayList<>();
|
||||
//多文件上传
|
||||
CommonsMultipartResolver crm = new CommonsMultipartResolver(getHttpServletRequest().getSession().getServletContext());
|
||||
if (crm.isMultipart(getHttpServletRequest())) {
|
||||
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (getHttpServletRequest());
|
||||
Iterator<String> files = mRequest.getFileNames();
|
||||
log.debug("多文件" + files.toString());
|
||||
while (files.hasNext()) {
|
||||
MultipartFile mFile = mRequest.getFile(files.next());
|
||||
log.debug("文件" + mFile.toString());
|
||||
// 文件名
|
||||
String fileName = mFile.getOriginalFilename();
|
||||
//新文件名
|
||||
String newFileName = DateUtil.getCurrentDateTime() + RandomCodeGenerator.numericCode(4);
|
||||
// 文件扩展名
|
||||
String fileExtensionName = FileUtil.getExtensionName(fileName);
|
||||
//上传文件的真实路径
|
||||
String filePath = uploadPath + newFileName + "." + fileExtensionName;
|
||||
File localPath = new File(filePath);
|
||||
try {
|
||||
mFile.transferTo(localPath);
|
||||
//保存文件信息
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setTypeCode(typeCode);
|
||||
sysFile.setFileUrl(filePath);
|
||||
sysFile.setFileName(newFileName);
|
||||
if (getLoginUser() != null) {
|
||||
setCommonField(sysFile);
|
||||
} else {
|
||||
sysFile.setCreateTime(LocalDateTime.now());
|
||||
sysFile.setCreator(1L);
|
||||
sysFile.setCreatorName("system");
|
||||
}
|
||||
fileList.add(sysFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("文件上传失败:" + e.getMessage());
|
||||
return R.failed("文件上传失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
sysFileServiceBiz.saveBatch(fileList);
|
||||
return R.ok(BeanUtils.copyList(fileList, SysFileVo.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param uploadFileBase64Param 上传文件对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R<SysFileVo> uploadFileBase64(UploadFileBase64Param uploadFileBase64Param) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传(form表单形式)
|
||||
*
|
||||
* @param typeCode
|
||||
* @param files
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R<List<SysFileVo>> uploadFileFormData(String typeCode, MultipartFile[] files) {
|
||||
//获取上传文件路径
|
||||
String uploadPath = commonConfig.getUploadPath() + typeCode + "/";
|
||||
log.debug("文件的上传路径:" + uploadPath);
|
||||
File file = new File(uploadPath);
|
||||
//判断文件是否存在,不存在则创建
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
List<SysFile> fileList = new ArrayList<>();
|
||||
for (MultipartFile multipartFile : files) {
|
||||
|
||||
log.debug("上传文件" + multipartFile.toString());
|
||||
// 文件名
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
//新文件名
|
||||
String newFileName = DateUtil.getCurrentDateTime() + RandomCodeGenerator.numericCode(4);
|
||||
// 文件扩展名
|
||||
String fileExtensionName = FileUtil.getExtensionName(fileName);
|
||||
//上传文件的真实路径
|
||||
String filePath = uploadPath + newFileName + "." + fileExtensionName;
|
||||
File localPath = new File(filePath);
|
||||
try {
|
||||
multipartFile.transferTo(localPath);
|
||||
//保存文件信息
|
||||
SysFile sysFile = new SysFile();
|
||||
sysFile.setTypeCode(typeCode);
|
||||
sysFile.setFileUrl(filePath);
|
||||
sysFile.setFileName(newFileName);
|
||||
if (getLoginUser() != null) {
|
||||
setCommonField(sysFile);
|
||||
} else {
|
||||
sysFile.setCreateTime(LocalDateTime.now());
|
||||
sysFile.setCreator(1L);
|
||||
sysFile.setCreatorName("system");
|
||||
}
|
||||
fileList.add(sysFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("文件上传失败:" + e.getMessage());
|
||||
return R.failed("文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
sysFileServiceBiz.saveBatch(fileList);
|
||||
return R.ok(BeanUtils.copyList(fileList, SysFileVo.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件下载(下载方式: 0、预览,1、下载)
|
||||
*
|
||||
* @param downloadFileParam
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public R<Object> downloadFile(DownloadFileParam downloadFileParam) {
|
||||
//根据id获取附件信息
|
||||
SysFile sysFile = sysFileServiceBiz.getById(downloadFileParam.getAttachmentId());
|
||||
if (sysFile == null) {
|
||||
return R.failed("文件不存在");
|
||||
}
|
||||
HttpServletResponse response = getHttpServletResponse();
|
||||
// 文件路径
|
||||
String path = sysFile.getFileUrl();
|
||||
File file = new File(path);
|
||||
String fileName = file.getName();
|
||||
// 文件扩展名
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
try {
|
||||
//判断下载方式是下载本地还是预览文件
|
||||
if (downloadFileParam.getType().equals("1")) {
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + new String((StringUtils.isEmpty(downloadFileParam.getFileName()) ? sysFile.getFileName() + "." + suffix : downloadFileParam.getFileName() + "." + suffix).getBytes(Charset.forName("GBK")), StandardCharsets.ISO_8859_1));
|
||||
} else {
|
||||
URL u = new URL("file:///" + path);
|
||||
String contentType = u.openConnection().getContentType();
|
||||
response.setContentType(contentType);
|
||||
response.setHeader("Content-Disposition", "inline; filename=" + new String((StringUtils.isEmpty(downloadFileParam.getFileName()) ? sysFile.getFileName() + "." + suffix : downloadFileParam.getFileName() + "." + suffix).getBytes(Charset.forName("GBK")), StandardCharsets.ISO_8859_1));
|
||||
}
|
||||
//判断是否是图片格式
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
if ("PNGpngJPGjpg".contains(suffix)) {
|
||||
Thumbnails.of(file)
|
||||
.scale(downloadFileParam.getScale())
|
||||
.outputQuality(downloadFileParam.getOutputQuality())
|
||||
.toOutputStream(outputStream);
|
||||
} else {
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
byte[] buffer = new byte[1024];
|
||||
int i = -1;
|
||||
while ((i = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, i);
|
||||
}
|
||||
inputStream.close();
|
||||
}
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("下载文件失败:{}", e.getMessage());
|
||||
return R.failed("下载文件失败");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.mt.wms.common.service.impl;
|
||||
|
||||
import com.mt.wms.common.mapper.CommonMapper;
|
||||
import com.mt.wms.common.service.CommonService;
|
||||
import com.mt.wms.common.vo.SysFileVo;
|
||||
import com.mt.wms.core.config.AliyunSmsConfig;
|
||||
import com.mt.wms.core.config.CommonConfig;
|
||||
import com.mt.wms.core.config.TencentCloudConfig;
|
||||
import com.mt.wms.core.config.TencentCloudSmsConfig;
|
||||
import com.mt.wms.core.params.IdParam;
|
||||
import com.mt.wms.core.base.BaseService;
|
||||
import com.mt.wms.core.vo.R;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </P>
|
||||
*
|
||||
* @author FanYi
|
||||
* @date 2020/7/6
|
||||
* @since 1.0
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CommonServiceImpl extends BaseService implements CommonService {
|
||||
|
||||
private final CommonMapper mCommonMapper;
|
||||
|
||||
private final CommonConfig mCommonConfig;
|
||||
|
||||
private final AliyunSmsConfig mAliyunSmsConfig;
|
||||
|
||||
private final TencentCloudSmsConfig tencentCloudSmsConfig;
|
||||
|
||||
private final TencentCloudConfig tencentCloudConfig;
|
||||
|
||||
private final CommonConfig commonConfig;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取附件下载地址
|
||||
*
|
||||
* @param fileId 附件ID
|
||||
* @return 附件下载地址
|
||||
*/
|
||||
@Override
|
||||
public SysFileVo downloadFile(Long fileId) {
|
||||
|
||||
return new SysFileVo().setFileId(null).setFileName(null).setFileUrl(mCommonConfig.getApiHost() + "/common/" + "/home".replace(mCommonConfig.getUploadPath(), ""));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public R<Object> downLoadFileUrl(IdParam idParam, HttpServletResponse response) {
|
||||
|
||||
// 文件扩展名
|
||||
String path = null;
|
||||
File picFile = new File(path);
|
||||
String fileName = picFile.getName();
|
||||
try {
|
||||
// 文件扩展名
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
URL u = new URL("file:///" + path);
|
||||
String contentType = null;
|
||||
|
||||
contentType = u.openConnection().getContentType();
|
||||
|
||||
response.setContentType(contentType);
|
||||
response.setHeader("Content-Disposition", "inline;filename=" + new String(("dw." + suffix).getBytes("GBK"), "ISO8859_1"));
|
||||
ServletOutputStream outputStream = response.getOutputStream();
|
||||
FileInputStream inputStream = new FileInputStream(picFile);
|
||||
byte[] buffer = new byte[1024];
|
||||
int i = -1;
|
||||
while ((i = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, i);
|
||||
}
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
e.printStackTrace();
|
||||
log.error("文件预览失败:" + e.getMessage());
|
||||
return R.failed("文件预览失败");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.mt.wms.common.vo;
|
||||
|
||||
import com.mt.wms.core.base.BaseVo;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </P>
|
||||
*
|
||||
* @author FanYi
|
||||
* @date 2020/7/8
|
||||
* @since 1.0
|
||||
*/
|
||||
@ApiModel("文件对象")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Accessors(chain = true)
|
||||
@Data
|
||||
public class SysFileVo extends BaseVo {
|
||||
@ApiModelProperty("文件ID")
|
||||
private Long fileId;
|
||||
|
||||
@ApiModelProperty("文件名称")
|
||||
private String fileName;
|
||||
|
||||
@ApiModelProperty("类型 1图片 2PDF")
|
||||
private Integer fileType;
|
||||
|
||||
@ApiModelProperty("下载路径")
|
||||
private String fileUrl;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.mt.wms.common.mapper.CommonMapper">
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user