资源下载链路打通

This commit is contained in:
limqhz
2022-05-03 17:15:54 +08:00
parent f44f36356d
commit f271221d8b
39 changed files with 888 additions and 312 deletions

View File

@@ -4,7 +4,6 @@ package com.quinn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.pojo.About;
import com.quinn.pojo.Say;
import com.quinn.service.AboutService;
import com.quinn.utils.QuinnUtils;
import org.springframework.stereotype.Controller;

View File

@@ -3,11 +3,13 @@ package com.quinn.controller;
import com.quinn.mapper.DownloadMapper;
import com.quinn.pojo.Download;
import com.quinn.service.DownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;
import java.util.List;
/**
@@ -21,12 +23,12 @@ import java.util.List;
@Controller
public class DownloadController {
@Autowired
DownloadMapper downloadMapper;
@Resource
DownloadService downloadService;
@GetMapping({"/download"})
public String download(Model model){
List<Download> downloadList = downloadMapper.selectList(null);
List<Download> downloadList = downloadService.list(null);
model.addAttribute("downloadList",downloadList);
return "page/download";
}

View File

@@ -31,11 +31,6 @@ public class LoginController {
return "index";
}
@GetMapping({"/auto"})
public String auto(){
return "index";
}
@GetMapping("/toLogin")
public String toLogin(){
return "login";

View File

@@ -1,53 +0,0 @@
package com.quinn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.pojo.Say;
import com.quinn.service.SayService;
import com.quinn.utils.QuinnUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class SayController {
@Autowired
SayService sayService;
@GetMapping("/say")
public String userIndexBlog(Model model){
Page<Say> pageParam = new Page<>(1, 50);
sayService.page(pageParam,new QueryWrapper<Say>().orderByDesc("gmt_create"));
// 结果
List<Say> sayList = pageParam.getRecords();
model.addAttribute("sayList",sayList);
model.addAttribute("pageParam",pageParam);
return "page/say";
}
@PostMapping("/say/{role}")
public String saveSay(@PathVariable("role") int role, Say say){
// 防止请求提交
if (role!=1){
return "redirect:/say";
}
say.setId(QuinnUtils.getUuid());
say.setGmtCreate(QuinnUtils.getTime());
// 结果
sayService.save(say);
return "redirect:/say";
}
}

View File

@@ -0,0 +1,21 @@
package com.quinn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Controller
public class SourceCategoryController {
}

View File

@@ -0,0 +1,17 @@
package com.quinn.controller;
import org.springframework.stereotype.Controller;
/**
* <p>
* 前端控制器
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Controller
public class SourceCommentController {
}

View File

@@ -0,0 +1,83 @@
package com.quinn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.pojo.*;
import com.quinn.service.SourceCategoryService;
import com.quinn.service.SourceCommentService;
import com.quinn.service.SourceService;
import com.quinn.utils.UUIDGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Controller
public class SourceController {
@Resource
SourceService sourceService;
@Resource
SourceCategoryService sourceCategoryService;
@Resource
SourceCommentService sourceCommentService;
// 列表展示
@GetMapping("/source")
public String blogList(@RequestParam String name,@RequestParam int category,Model model){
System.out.println(name + category);
Page<Source> pageParam = new Page<>(1, 10);
QueryWrapper<Source> sourceQuery = new QueryWrapper<>();
sourceQuery.orderByDesc("gmt_create");
sourceService.page(pageParam,sourceQuery);
// 结果
List<Source> sourceList = pageParam.getRecords();
model.addAttribute("sourceList",sourceList);
model.addAttribute("pageParam",pageParam);
// 分类信息
List<SourceCategory> categoryList = sourceCategoryService.list(null);
model.addAttribute("categoryList",categoryList);
return "source/list";
}
// 查看文件详情
@GetMapping("/source/view/{sid}")
public String read(@PathVariable("sid") String sid, Model model){
Source source = sourceService.getOne(new QueryWrapper<Source>().eq("id", sid));
source.setSourceLink("no network link");
model.addAttribute("source",source);
List<SourceComment> commentList = sourceCommentService.list(new QueryWrapper<SourceComment>().eq("topic_id", sid).orderByDesc("gmt_create"));
model.addAttribute("commentList",commentList);
return "source/view";
}
// 下载文件
@GetMapping("/source/download/{sid}")
public void read(HttpServletResponse response, @PathVariable("sid") String sid) throws IOException {
//通知浏览器以附件形式下载
response.setHeader("Content-Disposition",
"attachment;filename=" + UUIDGenerator.randomUUID());
this.sourceService.downloadSource(response.getOutputStream(),sid);
}
}

View File

@@ -22,7 +22,7 @@ public class CodeGenerator {
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("limqsh");
gc.setOpen(false); //生成后是否打开资源管理器
gc.setFileOverride(false); //重新生成时文件是否覆盖
gc.setFileOverride(true); //重新生成时文件是否覆盖
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setIdType(IdType.ID_WORKER_STR); //主键策略
gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
@@ -51,7 +51,7 @@ public class CodeGenerator {
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("qn_about");//设置要映射的表名
strategy.setInclude("qn_source_comment");//设置要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
strategy.setTablePrefix("qn_");//设置表前缀不生成

View File

@@ -0,0 +1,16 @@
package com.quinn.mapper;
import com.quinn.pojo.SourceCategory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
public interface SourceCategoryMapper extends BaseMapper<SourceCategory> {
}

View File

@@ -0,0 +1,16 @@
package com.quinn.mapper;
import com.quinn.pojo.SourceComment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
public interface SourceCommentMapper extends BaseMapper<SourceComment> {
}

View File

@@ -1,6 +1,6 @@
package com.quinn.mapper;
import com.quinn.pojo.Say;
import com.quinn.pojo.Source;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
@@ -9,8 +9,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* </p>
*
* @author limqsh
* @since 2020-07-01
* @since 2022-05-03
*/
public interface SayMapper extends BaseMapper<Say> {
public interface SourceMapper extends BaseMapper<Source> {
}

View File

@@ -0,0 +1,5 @@
<?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.quinn.mapper.SourceCategoryMapper">
</mapper>

View File

@@ -0,0 +1,5 @@
<?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.quinn.mapper.SourceCommentMapper">
</mapper>

View File

@@ -0,0 +1,5 @@
<?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.quinn.mapper.SourceMapper">
</mapper>

View File

@@ -0,0 +1,68 @@
package com.quinn.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("qn_source")
@ApiModel(value="Source对象", description="")
public class Source implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "资源名")
private String sourceName;
@ApiModelProperty(value = "资源内容")
private String sourceContent;
@ApiModelProperty(value = "关键字1")
private String keyWord1;
@ApiModelProperty(value = "关键字2")
private String keyWord2;
@ApiModelProperty(value = "关键字3")
private String keyWord3;
@ApiModelProperty(value = "资源类型")
private String sourceType;
@ApiModelProperty(value = "资源链接")
private String sourceLink;
@ApiModelProperty(value = "类别ID")
private Integer categoryId;
@ApiModelProperty(value = "类别名")
private String categoryName;
@ApiModelProperty(value = "创建时间")
private Date gmtCreate;
@ApiModelProperty(value = "修改时间")
private Date gmtUpdate;
}

View File

@@ -2,7 +2,6 @@ package com.quinn.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
@@ -13,33 +12,27 @@ import lombok.experimental.Accessors;
/**
* <p>
*
*
* </p>
*
* @author limqsh
* @since 2020-07-01
* @since 2022-05-03
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("qn_say")
@ApiModel(value="Say对象", description="")
public class Say implements Serializable {
@TableName("qn_source_category")
@ApiModel(value="SourceCategory对象", description="")
public class SourceCategory implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "唯一id")
@TableId(value = "id", type = IdType.ID_WORKER_STR)
private String id;
@ApiModelProperty(value = "自增id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "标题")
private String title;
@ApiModelProperty(value = "内容")
private String content;
@ApiModelProperty(value = "时间")
private Date gmtCreate;
@ApiModelProperty(value = "博客分类")
private String category;
}

View File

@@ -0,0 +1,60 @@
package com.quinn.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("qn_source_comment")
@ApiModel(value="SourceComment对象", description="")
public class SourceComment implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "自增id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "评论唯一id")
private String commentId;
@ApiModelProperty(value = "1博客 2问答")
private Integer topicCategory;
@ApiModelProperty(value = "评论主题id")
private String topicId;
@ApiModelProperty(value = "评论者id")
private String userId;
@ApiModelProperty(value = "评论者昵称")
private String userName;
@ApiModelProperty(value = "评论者头像")
private String userAvatar;
@ApiModelProperty(value = "评论内容")
private String content;
@ApiModelProperty(value = "评论创建时间")
private Date gmtCreate;
}

View File

@@ -0,0 +1,16 @@
package com.quinn.service;
import com.quinn.pojo.SourceCategory;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 服务类
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
public interface SourceCategoryService extends IService<SourceCategory> {
}

View File

@@ -1,6 +1,6 @@
package com.quinn.service;
import com.quinn.pojo.Say;
import com.quinn.pojo.SourceComment;
import com.baomidou.mybatisplus.extension.service.IService;
/**
@@ -9,8 +9,8 @@ import com.baomidou.mybatisplus.extension.service.IService;
* </p>
*
* @author limqsh
* @since 2020-07-01
* @since 2022-05-03
*/
public interface SayService extends IService<Say> {
public interface SourceCommentService extends IService<SourceComment> {
}

View File

@@ -0,0 +1,22 @@
package com.quinn.service;
import com.quinn.pojo.Source;
import com.baomidou.mybatisplus.extension.service.IService;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* <p>
* 服务类
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
public interface SourceService extends IService<Source> {
void downloadSource(ServletOutputStream outputStream, String sid) throws IOException;
}

View File

@@ -1,20 +0,0 @@
package com.quinn.service.impl;
import com.quinn.pojo.Say;
import com.quinn.mapper.SayMapper;
import com.quinn.service.SayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author limqsh
* @since 2020-07-01
*/
@Service
public class SayServiceImpl extends ServiceImpl<SayMapper, Say> implements SayService {
}

View File

@@ -0,0 +1,20 @@
package com.quinn.service.impl;
import com.quinn.pojo.SourceCategory;
import com.quinn.mapper.SourceCategoryMapper;
import com.quinn.service.SourceCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Service
public class SourceCategoryServiceImpl extends ServiceImpl<SourceCategoryMapper, SourceCategory> implements SourceCategoryService {
}

View File

@@ -0,0 +1,20 @@
package com.quinn.service.impl;
import com.quinn.pojo.SourceComment;
import com.quinn.mapper.SourceCommentMapper;
import com.quinn.service.SourceCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Service
public class SourceCommentServiceImpl extends ServiceImpl<SourceCommentMapper, SourceComment> implements SourceCommentService {
}

View File

@@ -0,0 +1,52 @@
package com.quinn.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quinn.pojo.Source;
import com.quinn.mapper.SourceMapper;
import com.quinn.service.SourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quinn.utils.OSSClientUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* <p>
* 服务实现类
* </p>
*
* @author limqsh
* @since 2022-05-03
*/
@Service
public class SourceServiceImpl extends ServiceImpl<SourceMapper, Source> implements SourceService {
@Resource
OSSClientUtil ossClientUtil;
@Override
public void downloadSource(ServletOutputStream outputStream, String sid) throws IOException {
Source source = getOne(new QueryWrapper<Source>().eq("id", sid));
String sourceLink = source.getSourceLink();
// 读取文件内容。
BufferedInputStream in = new BufferedInputStream(ossClientUtil.downloadFile(sourceLink));
BufferedOutputStream out = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[1024];
int lenght = 0;
while ((lenght = in.read(buffer)) != -1) {
out.write(buffer, 0, lenght);
}
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
}
}

View File

@@ -0,0 +1,115 @@
package com.quinn.utils;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
/**
* 阿里云 OSS文件类
*
* @author zuojie.liang
*/
@Component
public class OSSClientUtil {
Log log = LogFactory.getLog(OSSClientUtil.class);
/**
* 协议
*/
@Value("${oss.protocol}")
private String protocol;
/**
* 仓库名称
*/
@Value("${oss.name}")
private String bucketName;
/**
* 公网节点
*/
@Value("${oss.endPoint}")
private String endpoint;
@Value("${oss.accessKeyId}")
private String accessKeyId;
/**
* API访问值
*/
@Value("${oss.accessKeySecret}")
private String accessKeySecret;
@Value("${oss.url}")
private String url;
private OSSClient ossClient;
public String upload(String filename, InputStream file) {
return uploadImg(file, filename);
}
/**
* 上传图片
*
* @param file
* @return
*/
public String uploadImg(MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
return uploadImg(file.getInputStream(), originalFilename);
}
public String uploadImg(InputStream inputStream, String originalFilename) {
String backUrl = "";
try {
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
Random random = new Random();
String name = random.nextInt(10000) + System.currentTimeMillis() + substring;
backUrl = "imageDir/" + name;
// 上传文件
ossClient.putObject(bucketName, backUrl, inputStream);
ossClient.setObjectAcl(bucketName, backUrl, CannedAccessControlList.PublicRead);
// 判断是否上传成功
boolean uploadResult = ossClient.doesObjectExist(bucketName, backUrl);
if (uploadResult) {
backUrl = getImgUrl(name);
} else {
backUrl = "";
}
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return backUrl;
}
/**
* 获得图片路径
*
* @param fileUrl
* @return
*/
public String getImgUrl(String fileUrl) {
if (!StringUtils.isEmpty(fileUrl)) {
String[] split = fileUrl.split("/");
return url + "imageDir/" + split[split.length - 1];
}
return null;
}
public InputStream downloadFile(String objName){
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
OSSObject ossObject = ossClient.getObject(bucketName, objName);
return ossObject.getObjectContent();
}
}

View File

@@ -0,0 +1,28 @@
package com.quinn.utils;
import java.util.StringTokenizer;
import java.util.UUID;
/**
* @title UUIDGenerator
* @description 随机生成32位UUID格式如a11686c39a154cf2a5238fb14cf3d097
* <br>
* <br>
*/
public class UUIDGenerator {
/**
* 主键生成机制
* @return
*/
public static String randomUUID(){
String result="";
UUID uuid = UUID.randomUUID();
String temp=uuid.toString();
StringTokenizer token=new StringTokenizer(temp,"-");
while(token.hasMoreTokens()){
result+=token.nextToken();
}
return result;
}
}