package org.jeecg.modules.system.service.impl;
|
|
import org.jeecg.common.util.DateUtils;
|
import org.jeecg.common.util.oss.OssBootUtil;
|
import org.jeecg.modules.system.service.IAliUploadService;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
import java.net.URLEncoder;
|
|
/**
|
*
|
* @ClassName: AliUploadServiceImpl</br>
|
* @Description: TODO</br>
|
* @author yk</br>
|
* @date 2021年4月6日</br>
|
* </br>
|
*/
|
@Service
|
public class AliUploadServiceImpl implements IAliUploadService {
|
|
@Value(value = "${jeecg.oss.endpoint}")
|
private String endPoint;
|
|
@Value(value = "${jeecg.oss.accessKey}")
|
private String accessKeyId;
|
|
@Value(value = "${jeecg.oss.secretKey}")
|
private String accessKeySecret;
|
|
@Value(value = "${jeecg.oss.bucketName}")
|
private String bucketName;
|
|
@Override
|
@Transactional(rollbackFor = { Exception.class })
|
public String uploadFile(MultipartFile file) throws Exception {
|
String path = "mes/" + DateUtils.date2Str(DateUtils.yyyyMMdd.get());
|
OssBootUtil.setEndPoint(endPoint);
|
OssBootUtil.setAccessKeyId(accessKeyId);
|
OssBootUtil.setAccessKeySecret(accessKeySecret);
|
OssBootUtil.setBucketName(bucketName);
|
return OssBootUtil.upload(file, path);
|
}
|
|
@Override
|
public void downloadFile(HttpServletResponse response, String url, String fileName) {
|
OssBootUtil.setEndPoint(endPoint);
|
OssBootUtil.setAccessKeyId(accessKeyId);
|
OssBootUtil.setAccessKeySecret(accessKeySecret);
|
OssBootUtil.setBucketName(bucketName);
|
InputStream inputStream = OssBootUtil.download(url);
|
|
byte[] buffer = new byte[1024];
|
FileInputStream fis = null;
|
BufferedInputStream bis = null;
|
try {
|
response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 告诉浏览器输出内容为流
|
// response.setHeader("Content-Disposition", "attachment;fileName="+ new
|
// String(toFileName.getBytes("UTF-8"),"ISO-8859-1"));
|
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
|
// fis = new FileInputStream(file);
|
bis = new BufferedInputStream(inputStream);
|
OutputStream os = response.getOutputStream();
|
int i = bis.read(buffer);
|
while (i != -1) {
|
os.write(buffer, 0, i);
|
i = bis.read(buffer);
|
}
|
os.flush();
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if (bis != null) {
|
try {
|
bis.close();
|
} catch (IOException e) {
|
}
|
}
|
if (fis != null) {
|
try {
|
fis.close();
|
} catch (IOException e) {
|
}
|
}
|
}
|
}
|
|
}
|