提交 4ca04b33 编写于 作者: 寒風冷度夜雨's avatar 寒風冷度夜雨 🈴

message:宠物商城项目

desc:文件上传下载,商品类型模块新增
author:王荣力
time:20230919
上级 29a5281b
package cn.youle.pet.shop.controller;
import cn.youle.pet.shop.common.JsonResponse;
import cn.youle.pet.shop.enums.ENUM_HTTP_CONTENT_TYPE;
import cn.youle.pet.shop.exception.PetShopException;
import cn.youle.pet.shop.pojo.dto.UploadDTO;
import cn.youle.pet.shop.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 9:04
*/
@RestController
@RequestMapping("/common")
public class CommonController {
@Autowired
private ICommonService commonService;
/**
* 上传文件
*
* @param file
* @return
*/
@PostMapping("/upload")
public JsonResponse upload(@RequestParam("file") MultipartFile file) {
String upload = commonService.upload(file);
return JsonResponse.ok(upload);
}
/**
* 下载文件
*
* @param filePath
* @return
*/
@GetMapping("/download")
public ResponseEntity<InputStreamResource> download(@RequestParam("filePath") String filePath) {
if(filePath == null){
return null;
}
String suffix = filePath.substring(filePath.lastIndexOf("."));
ENUM_HTTP_CONTENT_TYPE enumHttpContentType = ENUM_HTTP_CONTENT_TYPE.matchFileType(suffix);
if(enumHttpContentType == null){
return null;
}
MediaType mediaType = enumHttpContentType.getMediaType();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(mediaType);
httpHeaders.add("Accept","*/*");
try {
return new ResponseEntity(
commonService.download(filePath),
httpHeaders,
HttpStatus.OK
);
}catch (Exception e){
return new ResponseEntity<>(null,httpHeaders,HttpStatus.OK);
}
}
}
package cn.youle.pet.shop.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 商品种类表 前端控制器
* </p>
*
* @author wangrongli
* @since 2023-09-20
*/
@RestController
@RequestMapping("/pet-product-category")
public class PetProductCategoryController {
}
package cn.youle.pet.shop.enums;
/**
* @projectName: pet-shop
* @description: 商品种类枚举
* @author: Administrator
* @createTime: 2023/9/20 8:40
*/
public enum ENUM_CATEGORY_TYPE {
;
String code;
String name;
ENUM_CATEGORY_TYPE(String code,String name){
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public static String getNameByCode(String code){
for (ENUM_CATEGORY_TYPE enumCategoryType : ENUM_CATEGORY_TYPE.values()){
if(enumCategoryType.getCode().equals(code)){
return enumCategoryType.getName();
}
}
return null;
}
}
package cn.youle.pet.shop.enums;
import org.springframework.http.MediaType;
import sun.net.www.content.image.gif;
import sun.net.www.content.image.jpeg;
import sun.net.www.content.image.png;
import sun.net.www.content.text.plain;
/**
* @author huangfeng
* @version 1.0
* @date 2021-03-17 15:05
*/
public enum ENUM_HTTP_CONTENT_TYPE {
// text/html : HTML格式
// text/plain :纯文本格式
// text/xml : XML格式
// image/gif :gif图片格式
// image/jpeg :jpg图片格式
// image/png:png图片格式
// application/xhtml+xml :XHTML格式
// application/xml : XML数据格式
// application/atom+xml :Atom XML聚合格式
// application/json : JSON数据格式
// application/pdf :pdf格式
// application/msword : Word文档格式
// application/octet-stream : 二进制流数据(如常见的文件下载)
// application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)
//
// 另外一种常见的媒体格式是上传文件之时使用的:
//
// multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
PDF(".pdf", MediaType.APPLICATION_PDF),
JPEG(".jpeg",MediaType.IMAGE_JPEG),
JPG(".jpg",MediaType.IMAGE_JPEG),
BMP(".bmp",MediaType.IMAGE_JPEG),
HTML(".html",MediaType.TEXT_HTML),
TEXT(".txt",MediaType.TEXT_PLAIN),
XML(".xml",MediaType.TEXT_XML),
XHTML(".xhtml",MediaType.APPLICATION_XHTML_XML),
ATOM(".atom",MediaType.APPLICATION_ATOM_XML),
JSON(".json",MediaType.APPLICATION_JSON),
WORD(".doc",MediaType.APPLICATION_OCTET_STREAM),
WORDX(".docx",MediaType.APPLICATION_OCTET_STREAM),
EXCEL(".xls",MediaType.APPLICATION_OCTET_STREAM),
EXCELX(".xlsx",MediaType.APPLICATION_OCTET_STREAM),
PNG(".png",MediaType.IMAGE_PNG),
GIF(".gif",MediaType.IMAGE_GIF),
;
private String fileType;
private MediaType mediaType;
public String getFileType() {
return fileType;
}
public MediaType getMediaType() {
return mediaType;
}
ENUM_HTTP_CONTENT_TYPE(String fileType, MediaType mediaType) {
this.fileType = fileType;
this.mediaType = mediaType;
}
ENUM_HTTP_CONTENT_TYPE() { }
public static ENUM_HTTP_CONTENT_TYPE matchFileType(String fileType){
if(fileType == null || "".equals(fileType)){
return null;
}
for (ENUM_HTTP_CONTENT_TYPE enumHttpContentType : ENUM_HTTP_CONTENT_TYPE.values()) {
if(fileType.equalsIgnoreCase(enumHttpContentType.fileType)){
return enumHttpContentType;
}
}
return null;
}
}
package cn.youle.pet.shop.enums;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 8:43
*/
public enum ENUM_ORDER_STATUS {
COMPLETE("COMPLETE","已完成","PAYING"),
PAYING("PAYING","待付款","PAYED"),
PAYED("PAYED","已付款","SHIPPING"),
SHIPPING("SHIPPING","待发货","SHIPPED"),
SHIPPED("SHIPPED","已发货","RECEIVING"),
RECEIVING("RECEIVING","待收货","RECEIVED"),
RECEIVED("RECEIVED","已收货","EVALUATING"),
EVALUATING("EVALUATING","待评价","EVALUATED"),
EVALUATED("EVALUATED","已评价",""),
CANCELING("CANCELING","待取消","CANCELED"),
CANCELED("CANCELED","已取消",""),
REFUNDING("REFUNDING","待退款","REFUNDED"),
REFUNDED("REFUNDED","已退款","");
String value;
String name;
String nextStatus;
ENUM_ORDER_STATUS(String value,String name,String nextStatus){
this.value = value;
this.name = name;
this.nextStatus = nextStatus;
}
public static String getNextStatus(String status){
for (ENUM_ORDER_STATUS enumOrderStatus : ENUM_ORDER_STATUS.values()){
if(enumOrderStatus.getValue().equals(status)){
return enumOrderStatus.getNextStatus();
}
}
return null;
}
public String getValue() {
return value;
}
public String getName() {
return name;
}
public String getNextStatus() {
return nextStatus;
}
}
package cn.youle.pet.shop.mapper;
import cn.youle.pet.shop.pojo.entity.PetProductCategory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 商品种类表 Mapper 接口
* </p>
*
* @author wangrongli
* @since 2023-09-20
*/
public interface PetProductCategoryMapper extends BaseMapper<PetProductCategory> {
}
package cn.youle.pet.shop.pojo.dto;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 14:00
*/
@Component
@ConfigurationProperties(prefix = "uploadfile")
@Data
public class CommonConfigDTO {
private String path;
}
package cn.youle.pet.shop.pojo.dto;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 9:06
*/
@Data
public class UploadDTO implements Serializable {
private static final long serialVersionUID = 980999559022731172L;
private MultipartFile file;
}
package cn.youle.pet.shop.pojo.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
* 商品种类表
* </p>
*
* @author wangrongli
* @since 2023-09-20
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("pet_product_category")
public class PetProductCategory implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID
*/
private Long id;
/**
* 商品种类等级
*/
private String level;
/**
* 商品种类名称
*/
private String name;
/**
* 父级商品种类ID
*/
private Long parentId;
/**
* 创建人
*/
private String createdBy;
/**
* 创建时间
*/
private LocalDateTime createdTime;
/**
* 更新人
*/
private String updatedBy;
/**
* 更新时间
*/
private LocalDateTime updatedTime;
/**
* 是否删除
*/
private Integer isDeleted;
}
package cn.youle.pet.shop.service;
import cn.youle.pet.shop.pojo.dto.UploadDTO;
import org.springframework.core.io.InputStreamResource;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 9:07
*/
public interface ICommonService {
/**
* 文件上传
*
* @param uploadDTO
* @return
*/
String upload(MultipartFile file);
/**
* 下载文件
*
* @param filePath
*/
InputStreamResource download(String filePath);
}
package cn.youle.pet.shop.service;
import cn.youle.pet.shop.pojo.entity.PetProductCategory;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 商品种类表 服务类
* </p>
*
* @author wangrongli
* @since 2023-09-20
*/
public interface IPetProductCategoryService extends IService<PetProductCategory> {
}
package cn.youle.pet.shop.service.impl;
import cn.youle.pet.shop.enums.ENUM_HTTP_CONTENT_TYPE;
import cn.youle.pet.shop.exception.PetShopException;
import cn.youle.pet.shop.pojo.dto.CommonConfigDTO;
import cn.youle.pet.shop.pojo.dto.UploadDTO;
import cn.youle.pet.shop.service.ICommonService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;
/**
* @projectName: pet-shop
* @description:
* @author: Administrator
* @createTime: 2023/9/20 9:07
*/
@Service
@Slf4j
public class CommonServiceImpl implements ICommonService {
@Resource
private CommonConfigDTO commonConfigDTO;
/**
* 文件上传
*
* @param multipartFile
* @return
*/
@Override
public String upload(MultipartFile multipartFile) {
log.info("****************文件上传开始****************");
String originalFilename = multipartFile.getOriginalFilename();
if (originalFilename == null) {
throw new PetShopException("没有要上传的文件");
}
// 获取后缀名
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
// 重新定义文件名
UUID uuid = UUID.randomUUID();
String fileName = uuid.toString();
fileName = fileName.replace("-", "") + suffix;
String filePath = commonConfigDTO.getPath();
filePath = filePath + fileName;
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
try {
multipartFile.transferTo(file);
log.info("文件上传成功");
} catch (Exception e) {
log.error("文件上传失败,原因是:{}", e.getMessage());
throw new PetShopException("文件上传失败,原因是:" + e.getMessage());
}
log.info("****************文件上传结束****************");
return filePath;
}
/**
* 下载文件
*
* @param filePath
*/
@Override
public InputStreamResource download(String filePath) {
log.info("======================开始获取文件,文件路径:{}======================",filePath);
// 根据文件名查找对应文件
File file = new File(filePath);
if (!file.exists()) {
throw new PetShopException("该文件不存在");
}
FileInputStream fis = null;
InputStream inputStream = null;
try {
// 读取文件流
inputStream = new FileInputStream(file);
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
log.info("======================文件获取成功,准备返回======================");
return inputStreamResource;
} catch (Exception e) {
log.error("下载接口出现问题,具体原因:{}", e.getMessage());
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
log.error("文件下载错误,原因是:{}", e.getMessage());
e.printStackTrace();
}
}
return null;
}
}
package cn.youle.pet.shop.service.impl;
import cn.youle.pet.shop.pojo.entity.PetProductCategory;
import cn.youle.pet.shop.mapper.PetProductCategoryMapper;
import cn.youle.pet.shop.service.IPetProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 商品种类表 服务实现类
* </p>
*
* @author wangrongli
* @since 2023-09-20
*/
@Service
public class PetProductCategoryServiceImpl extends ServiceImpl<PetProductCategoryMapper, PetProductCategory> implements IPetProductCategoryService {
}
......@@ -33,6 +33,8 @@ public class ShiroConfig {
map.put("/api/pet-admins/login","anon");
map.put("/api/pet-admins/register","anon");
map.put("/api/pet-admins/checkLogin","anon");
map.put("/common/upload","anon");
map.put("/common/download","anon");
// 对所有用户认证
map.put("/**","authc");
......
......@@ -10,3 +10,6 @@ login.redis.key=knd0244fwg5e45d2a242d4g
login.redis.timeout=5
spring.servlet.multipart.max-file-size=10MB
uploadfile.path=D:\\files\\petshop\\
<?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="cn.youle.pet.shop.mapper.PetProductCategoryMapper">
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册