阿里云oss和下载中心
parent
795e3e5461
commit
66460c199c
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
}
|
@ -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…
Reference in New Issue