
com.xr.qiniuyun.service.impl.QiniuyunFileServiceImpl Maven / Gradle / Ivy
/*
*
* Copyright (c) 2023 - future
* @author fzh
* @email [email protected]
* @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.xr.qiniuyun.service.impl;
import com.xr.common.utils.FileUtil;
import com.xr.common.utils.R;
import com.xr.qiniuyun.config.QiniuyunConfig;
import com.xr.qiniuyun.service.QiniuyunFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
/**
* @author XR FZH
*
*
* application.yml 配置
*
* ———————————————————— #qiniuyun: # accessKey: # secretKey: # domainOfBucket: #
* bucketName: # # [{'zone0':'华东'},
* {'zone1':'华北'},{'zone2':'华南'},{'zoneNa0':'北美'},{'zoneAs0':'其他'}] # zoneName: # #
* 链接过期时间,单位是秒,3600代表1小时,-1代表永不过期 # expireInSeconds: ————————————————————
*/
@Slf4j
@Component
@ComponentScan(basePackages = "com.xr.qiniuyun.config")
public class QiniuyunFileServiceImpl implements QiniuyunFileService {
@Resource
private QiniuyunConfig qiniuyunConfig;
@Resource
private HttpServletResponse response;
/**
* 上传文件
* @param file
* @return
*/
@Override
public R uploadFile(MultipartFile file) {
String fileName = file.getOriginalFilename();
boolean flag = qiniuyunConfig.uploadMultipartFile(file, fileName, true);
if (flag) {
return R.ok("文件上传成功", qiniuyunConfig.getFileUrl(fileName));
}
return R.error("文件上传失败");
}
/**
* 删除文件
* @param url
* @return
*/
@Override
public R deleteFile(String url) {
boolean flag = qiniuyunConfig.deleteFile(url);
if (flag) {
return R.ok("文件删除成功");
}
return R.error("文件删除失败");
}
/**
* 下载文件
* @param url
*/
@Override
public void downloadFile(String url) {
try {
// 从url中截取出文件名
String fileName = url.substring(url.lastIndexOf("/") + 1);
ServletOutputStream sos = response.getOutputStream();
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("application/octet-stream");
// 通过文件远程url读取文件的字节流
sos.write(Objects.requireNonNull(FileUtil.getFileStream(url)));
sos.flush();
sos.close();
}
catch (IOException e) {
log.error("文件下载失败");
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy