阿里云oss和下载中心

main
wanghb 2 years ago
parent 795e3e5461
commit 66460c199c

@ -76,6 +76,18 @@
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>

@ -17,4 +17,6 @@ public interface CourseMapper extends BaseMapper<Course> {
CourseVo queryCourseById(Integer id);
int updateCourseById(Course course);
int selectByName(String courseName);
}

@ -4,6 +4,12 @@ import lombok.Data;
@Data
public class PageParam {
/**
*
*/
private Integer currentPage;
/**
*
*/
private Integer pageSize;
}

@ -58,12 +58,18 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper,Course> implemen
*/
@Override
public int addCourse(CourseVo courseVo) {
if (courseVo == null)
{
throw new ServiceException("400","课程添加失败");
}
int sum = courseMapper.selectByName(courseVo.getCourseName());
if (sum>0)
{
throw new ServiceException("403","该课程已经存在");
}
Course course = new Course();
BeanUtils.copyProperties(courseVo,course);
@ -97,12 +103,14 @@ public class CourseServiceImpl extends ServiceImpl<CourseMapper,Course> implemen
*/
@Override
public int updateById(CourseVo courseVo) {
//将vo值赋给course
Course course = new Course();
BeanUtils.copyProperties(courseVo,course);
//查询归属目录
String dirName = courseVo.getDirName();
Integer id = directoryMapper.selectByName(dirName);
course.setDirId(id);
//根据id进行课程更新
int count = courseMapper.updateCourseById(course);
if (count<0)
{

@ -1,4 +1,100 @@
package com.zhiyun.zhiyun03.download.controller;
import com.zhiyun.zhiyun03.course.vo.PageVO;
import com.zhiyun.zhiyun03.download.service.DownloadService;
import com.zhiyun.zhiyun03.download.vo.DownloadVo;
import com.zhiyun.zhiyun03.utils.common.JsonResult;
import com.zhiyun.zhiyun03.utils.oss.AliyunOssUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
@RestController
@RequestMapping("/download")
public class DownloadController {
@Resource
private AliyunOssUtil ossUtil;
@Resource
private DownloadService downloadService;
@ApiOperation("查询下载中心")
@GetMapping("/queryDownload")
public JsonResult<PageVO<DownloadVo>> queryDownload(@ApiParam @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
@ApiParam @RequestParam(value = "limit",required = false,defaultValue = "5")Integer limit ){
PageVO<DownloadVo> lists = downloadService.queryDownload(page,limit);
return JsonResult.success(lists);
}
@ApiOperation("添加下载")
@PostMapping("/addDownload")
public JsonResult addDownload(DownloadVo vo){
int count = downloadService.addDownload(vo);
return JsonResult.success();
}
@ApiOperation("图片上传")
@PostMapping("/uploadFile")
public JsonResult upload(@RequestParam("file") @RequestPart MultipartFile file){
String upload = null;
try {
if (file != null) {
String fileName = file.getOriginalFilename();
if (!"".equals(fileName.trim())) {
File newFile = new File(fileName);
FileOutputStream os = new FileOutputStream(newFile);
os.write(file.getBytes());
os.close();
//把file里的内容复制到newFile中
file.transferTo(newFile);
upload = ossUtil.upload(newFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
String path = "https://zhiyun03.oss-cn-beijing.aliyuncs.com/"+ upload;
return JsonResult.success(path);
}
@ApiOperation("删除下载")
@PostMapping("/delDownload")
public JsonResult delDownload(Integer id){
int count = downloadService.delDownload(id);
return JsonResult.success();
}
@ApiOperation("根据Id查询")
@PostMapping("/queryById")
public JsonResult<DownloadVo> queryById(Integer id){
DownloadVo downloadAddVo =downloadService.queryById(id);
return JsonResult.success(downloadAddVo);
}
@ApiOperation("更新下载")
@PostMapping("/updateDownload")
public JsonResult<DownloadVo> updateDownload(DownloadVo vo){
int count =downloadService.updateDownload(vo);
return JsonResult.success();
}
}

@ -21,7 +21,7 @@ public class Download {
* id
*/
@TableId(value = "id",type = IdType.AUTO)
private int id;
private Integer id;
/**
*
@ -32,8 +32,8 @@ public class Download {
/**
*
*/
@TableField(value = "download_biref")
private String downloadBiref;
@TableField(value = "download_brief")
private String downloadBrief;
/**
*

@ -2,8 +2,16 @@ package com.zhiyun.zhiyun03.download.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhiyun.zhiyun03.download.entity.Download;
import com.zhiyun.zhiyun03.download.vo.DownloadVo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DownloadMapper extends BaseMapper<Download> {
List<DownloadVo> queryDownload();
int selectByName(String downloadName);
int updateDownloadById(Download download);
}

@ -1,7 +1,23 @@
package com.zhiyun.zhiyun03.download.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhiyun.zhiyun03.course.vo.PageVO;
import com.zhiyun.zhiyun03.download.entity.Download;
import com.zhiyun.zhiyun03.download.vo.DownloadVo;
public interface DownloadService extends IService<Download> {
/*
*
* */
PageVO<DownloadVo> queryDownload(Integer page, Integer limit);
/*
*
* */
int addDownload(DownloadVo vo);
int delDownload(Integer id);
DownloadVo queryById(Integer id);
int updateDownload(DownloadVo vo);
}

@ -1,11 +1,149 @@
package com.zhiyun.zhiyun03.download.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyun.zhiyun03.application.entity.Directory;
import com.zhiyun.zhiyun03.application.mapper.DirectoryMapper;
import com.zhiyun.zhiyun03.course.vo.PageVO;
import com.zhiyun.zhiyun03.download.entity.Download;
import com.zhiyun.zhiyun03.download.mapper.DownloadMapper;
import com.zhiyun.zhiyun03.download.service.DownloadService;
import com.zhiyun.zhiyun03.download.vo.DownloadVo;
import com.zhiyun.zhiyun03.utils.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class DownloadServiceImpl extends ServiceImpl<DownloadMapper, Download> implements DownloadService {
@Resource
private DownloadMapper downloadMapper;
@Resource
private DirectoryMapper directoryMapper;
/*
*
* */
@Override
public PageVO<DownloadVo> queryDownload(Integer page, Integer limit) {
//开启分页
PageHelper.startPage(page,limit);
//查询所有下载数据
List<DownloadVo> downloadVoList = downloadMapper.queryDownload();
PageInfo<DownloadVo> downloadVoPageInfo = new PageInfo<>(downloadVoList);
PageVO<DownloadVo> downloadVoPageVO = new PageVO<>();
//获取当前页
downloadVoPageVO.setCurrentPage(downloadVoPageInfo.getPageNum());
//总条数
downloadVoPageVO.setTotal(downloadVoPageInfo.getTotal());
//分页数据
downloadVoPageVO.setList(downloadVoPageInfo.getList());
return downloadVoPageVO;
}
/*
*
* */
@Override
public int addDownload(DownloadVo vo) {
//查询下载名称是否已经存在
int count = downloadMapper.selectByName(vo.getDownloadName());
if (count>0)
{
throw new ServiceException("403","下载已存在");
}
//将vo值传给download
Download download = new Download();
BeanUtils.copyProperties(vo,download);
//查询归属目录id
Integer dirId = directoryMapper.selectByName(vo.getDirName());
download.setDirId(dirId);
download.setDownloadUpdatetime(new Date());
//添加download
int insert = downloadMapper.insert(download);
if (insert>0)
{
return insert;
}else
{
throw new ServiceException("400","添加下载失败");
}
}
/*
* id
* */
@Override
public int delDownload(Integer id) {
if (id == null)
{
throw new ServiceException("400","查询id值为空");
}
//根据id删除
int count = downloadMapper.deleteById(id);
if (count>0)
{
return count;
}else
{
throw new ServiceException("400","删除失败");
}
}
/*
* id
* */
@Override
public DownloadVo queryById(Integer id) {
//根据id查询数据
Download download = downloadMapper.selectById(id);
//查询目录id
Directory directory = directoryMapper.selectById(download.getDirId());
DownloadVo downloadAddVo = new DownloadVo();
//复制给vo
BeanUtils.copyProperties(download,downloadAddVo);
downloadAddVo.setDirName(directory.getDirName());
return downloadAddVo;
}
/*
*
* */
@Override
public int updateDownload(DownloadVo vo) {
//查询更新的下载名是否存在
int sum = downloadMapper.selectByName(vo.getDownloadName());
if (sum>0)
{
throw new ServiceException("403","下载已存在");
}
//将vo值传给download
Download download = new Download();
BeanUtils.copyProperties(vo,download);
//查询归属目录id
Integer dirId = directoryMapper.selectByName(vo.getDirName());
download.setDirId(dirId);
download.setDownloadUpdatetime(new Date());
//添加download
int insert = downloadMapper.updateDownloadById(download);
if (insert>0)
{
return insert;
}else
{
throw new ServiceException("400","更新失败");
}
}
}

@ -0,0 +1,34 @@
package com.zhiyun.zhiyun03.download.vo;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class DownloadVo {
@TableId
private int id;
/**
*
*/
private String downloadName;
/**
*
*/
private String downloadBrief;
/**
*
*/
private String downloadUrl;
/**
*
*/
private String dirName;
/**
*
*/
private String downloadImg;
}

@ -0,0 +1,76 @@
package com.zhiyun.zhiyun03.utils.oss;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
*
*/
@Component
public class AliyunOssUtil {
@Autowired
private OssConfiguration config;
public String upload(File file) {
if (file == null) {
return null;
}
String endPoint = config.getEndPoint();
String keyId = config.getAccessKeyId();
String keySecret = config.getAccessKeySecret();
String bucketName = config.getBucketName();
String fileHost = config.getFileHost();
//定义子文件的格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(new Date());
//阿里云文件上传客户端
OSSClient client = new OSSClient(endPoint, keyId, keySecret);
try {
//判断桶是否存在
if (!client.doesBucketExist(bucketName)) {
//创建桶
client.createBucket(bucketName);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
//设置访问权限为公共读
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//发起创建桶的请求
client.createBucket(createBucketRequest);
}
//当桶存在时,进行文件上传
//设置文件路径和名称
String fileUrl = fileHost + "/" + (dateStr + "/" + UUID.randomUUID().toString().replace("-", "") + "-" + file.getName());
PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl, file));
client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
//文件上传成功后,返回当前文件的路径
if (result != null) {
return fileUrl;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null) {
client.shutdown();
}
}
return null;
}
}

@ -0,0 +1,32 @@
package com.zhiyun.zhiyun03.utils.oss;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* .
*/
@Data
@Configuration
public class OssConfiguration {
@Value("${endpoint}")
private String endPoint;
@Value("${accessKeyId}")
private String accessKeyId;
@Value("${accessKeySecret}")
private String accessKeySecret;
@Value("${filehost}")
private String fileHost;
@Value("${bucketName}")
private String bucketName;
}

@ -1,14 +1,17 @@
server:
port: 8080
#
#mybatis:
# mapper-locations: classpath*:mapper/**/*.xml
# configuration:
# map-underscore-to-camel-case: true
# # ???????
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#
#配置OSS的个人信息
bucketName: zhiyun03
accessKeyId: LTAI5t8zUbXsJpzPuoBQ3eGu
accessKeySecret: dIYCoLCKlKPp17wqRRPlrwLaznDYXc
#OSS对应的区域
endpoint: oss-cn-beijing.aliyuncs.com
#OSS对应的文件夹会在OSS中自动创建
filehost: images
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource

@ -47,5 +47,8 @@
on c.dir_id = d.id
where c.id =#{id}
</select>
<select id="selectByName" resultType="java.lang.Integer">
select count(1) from course where course_name =#{courseName}
</select>
</mapper>

@ -0,0 +1,45 @@
<?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.zhiyun.zhiyun03.download.mapper.DownloadMapper">
<update id="updateDownloadById">
update download
<set >
<if test="downloadName != null" >
download_name=#{downloadName},
</if>
<if test="downloadUrl != null" >
download_url=#{downloadUrl},
</if>
<if test="downloadBrief != null" >
download_brief=#{downloadBrief},
</if>
<if test="dirId != null" >
dir_id=#{dirId},
</if>
<if test="downloadImg != null" >
download_img=#{downloadImg}
</if>
</set>
<where>
<if test="id!=null">
id = #{id}
</if>
</where>
</update>
<select id="queryDownload" resultType="com.zhiyun.zhiyun03.download.vo.DownloadVo">
select do.id,do.download_name,do.download_brief,do.download_url,d.dir_name
from download as do
inner join
(select id,dir_name from directory) as d
on do.dir_id = d.id
</select>
<select id="selectByName" resultType="java.lang.Integer">
select count(1) from download where download_name =#{downloadName}
</select>
</mapper>
Loading…
Cancel
Save