1. 添加 远程获取 文件数据
This commit is contained in:
parent
bae601c048
commit
d528e0fe89
|
|
@ -6,9 +6,7 @@ import com.chushang.common.core.web.Result;
|
|||
import com.chushang.oss.constants.OssConstants;
|
||||
import com.chushang.oss.entity.vo.FileSourceVo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -47,5 +45,8 @@ public interface RemoteOssService {
|
|||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
@GetMapping("/file/{fid}")
|
||||
Result<byte[]> getFile(@PathVariable(name = "fid") String fid,@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,7 @@ import com.chushang.oss.entity.vo.FileSourceVo;
|
|||
import com.chushang.oss.feign.RemoteOssService;
|
||||
import com.chushang.oss.service.FileSourceService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
|
@ -56,5 +53,13 @@ public class RemoteFileController implements RemoteOssService {
|
|||
return Result.ok(fileSourceService.uploadFile(bytes, fileName, sealFlag, formats, fileType));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SysLog(value = "feign文件", businessType = BusinessType.DOWNLOAD)
|
||||
@GetMapping("/getFile/{fid}")
|
||||
public Result<byte[]> getFile(@PathVariable String fid,String source)
|
||||
{
|
||||
return Result.ok(fileSourceService.getFile(fid));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -258,7 +258,6 @@ public class FileSourceService
|
|||
String ip = IPUtils.clientIp(ServletUtils.getRequest());
|
||||
String fid = generateFid();
|
||||
String fName = file.getOriginalFilename();
|
||||
long length = file.getSize();
|
||||
String md5 = FileUtils.getMd5(file.getInputStream());
|
||||
// image/jpeg mimeType
|
||||
FileSourceInfo fileSourceInfo = new FileSourceInfo();
|
||||
|
|
@ -345,4 +344,16 @@ public class FileSourceService
|
|||
{
|
||||
return Collections.singletonList(addFile(bytes, fileName, sealFlag, formats, fileType));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件id获取二进制文件
|
||||
* @param fid 文件id
|
||||
*/
|
||||
public byte[] getFile(String fid) {
|
||||
FileSourceInfo info = getOne(new LambdaQueryWrapper<FileSourceInfo>()
|
||||
.eq(FileSourceInfo::getFid, fid)
|
||||
.last(Operator.LIMIT_ONE.getCharacter()));
|
||||
if (ObjUtil.isEmpty(info)) return null;
|
||||
return ossService.getFileBytes(info);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,8 +55,6 @@ public interface OssService {
|
|||
*/
|
||||
List<String> delFileBatch(List<String> keys);
|
||||
|
||||
InputStream getFileStream(FileSourceInfo info);
|
||||
|
||||
byte[] getFileBytes(FileSourceInfo info);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
|
@ -114,16 +117,28 @@ public class AliServiceImpl implements OssService {
|
|||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getFileStream(FileSourceInfo info) {
|
||||
OSSObject object = oss.getObject(config.getBucketName(), info.getPath());
|
||||
return object.getObjectContent();
|
||||
}
|
||||
//
|
||||
// @Override
|
||||
// public InputStream getFileStream(FileSourceInfo info) {
|
||||
// OSSObject object = oss.getObject(config.getBucketName(), info.getPath());
|
||||
// return object.getObjectContent();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public byte[] getFileBytes(FileSourceInfo info) {
|
||||
return new byte[0];
|
||||
OSSObject object = oss.getObject(config.getBucketName(), info.getPath());
|
||||
InputStream is = object.getObjectContent();
|
||||
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()){
|
||||
int nRead;
|
||||
byte[] data = new byte[1024];
|
||||
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||
buffer.write(data, 0, nRead);
|
||||
}
|
||||
buffer.flush();
|
||||
return buffer.toByteArray();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -204,19 +204,19 @@ public class MinioServiceImpl implements OssService {
|
|||
return resultKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getFileStream(FileSourceInfo info) {
|
||||
try {
|
||||
GetObjectResponse object = minio.getObject(GetObjectArgs.builder()
|
||||
.bucket(config.getBucketName())
|
||||
.object(info.getPath())
|
||||
.build());
|
||||
return new ByteArrayInputStream(object.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
log.error("获取minio object error", e);
|
||||
throw new ResultException("获取文件失败");
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// public InputStream getFileStream(FileSourceInfo info) {
|
||||
// try {
|
||||
// GetObjectResponse object = minio.getObject(GetObjectArgs.builder()
|
||||
// .bucket(config.getBucketName())
|
||||
// .object(info.getPath())
|
||||
// .build());
|
||||
// return new ByteArrayInputStream(object.readAllBytes());
|
||||
// } catch (Exception e) {
|
||||
// log.error("获取minio object error", e);
|
||||
// throw new ResultException("获取文件失败");
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public byte[] getFileBytes(FileSourceInfo info) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue