提交 5f8c569a authored 作者: YunaiV's avatar YunaiV

!313 修复上传七牛云无mime type问题

上级 583cb938
...@@ -22,7 +22,7 @@ public interface FileClient { ...@@ -22,7 +22,7 @@ public interface FileClient {
* @return 完整路径,即 HTTP 访问地址 * @return 完整路径,即 HTTP 访问地址
* @throws Exception 上传文件时,抛出 Exception 异常 * @throws Exception 上传文件时,抛出 Exception 异常
*/ */
String upload(byte[] content, String path) throws Exception; String upload(byte[] content, String path, String type) throws Exception;
/** /**
* 删除文件 * 删除文件
......
...@@ -21,7 +21,7 @@ public class DBFileClient extends AbstractFileClient<DBFileClientConfig> { ...@@ -21,7 +21,7 @@ public class DBFileClient extends AbstractFileClient<DBFileClientConfig> {
} }
@Override @Override
public String upload(byte[] content, String path) { public String upload(byte[] content, String path, String type) {
getDao().insert(getId(), path, content); getDao().insert(getId(), path, content);
// 拼接返回路径 // 拼接返回路径
return super.formatFileUrl(config.getDomain(), path); return super.formatFileUrl(config.getDomain(), path);
......
...@@ -10,7 +10,6 @@ import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient; ...@@ -10,7 +10,6 @@ import cn.iocoder.yudao.framework.file.core.client.AbstractFileClient;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
/** /**
* Ftp 文件客户端 * Ftp 文件客户端
...@@ -39,7 +38,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> { ...@@ -39,7 +38,7 @@ public class FtpFileClient extends AbstractFileClient<FtpFileClientConfig> {
} }
@Override @Override
public String upload(byte[] content, String path) { public String upload(byte[] content, String path, String type) {
// 执行写入 // 执行写入
String filePath = getFilePath(path); String filePath = getFilePath(path);
String fileName = FileUtil.getName(filePath); String fileName = FileUtil.getName(filePath);
......
...@@ -25,7 +25,7 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> { ...@@ -25,7 +25,7 @@ public class LocalFileClient extends AbstractFileClient<LocalFileClientConfig> {
} }
@Override @Override
public String upload(byte[] content, String path) { public String upload(byte[] content, String path, String type) {
// 执行写入 // 执行写入
String filePath = getFilePath(path); String filePath = getFilePath(path);
FileUtil.writeBytes(content, filePath); FileUtil.writeBytes(content, filePath);
......
...@@ -82,10 +82,11 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> { ...@@ -82,10 +82,11 @@ public class S3FileClient extends AbstractFileClient<S3FileClientConfig> {
} }
@Override @Override
public String upload(byte[] content, String path) throws Exception { public String upload(byte[] content, String path, String type) throws Exception {
// 执行上传 // 执行上传
client.putObject(PutObjectArgs.builder() client.putObject(PutObjectArgs.builder()
.bucket(config.getBucket()) // bucket 必须传递 .bucket(config.getBucket()) // bucket 必须传递
.contentType(type)
.object(path) // 相对路径作为 key .object(path) // 相对路径作为 key
.stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容 .stream(new ByteArrayInputStream(content), content.length, -1) // 文件内容
.build()); .build());
......
...@@ -31,7 +31,7 @@ public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> { ...@@ -31,7 +31,7 @@ public class SftpFileClient extends AbstractFileClient<SftpFileClientConfig> {
} }
@Override @Override
public String upload(byte[] content, String path) { public String upload(byte[] content, String path, String type) {
// 执行写入 // 执行写入
String filePath = getFilePath(path); String filePath = getFilePath(path);
File file = FileUtils.createTempFile(content); File file = FileUtils.createTempFile(content);
......
...@@ -16,7 +16,7 @@ public class FileTypeUtils { ...@@ -16,7 +16,7 @@ public class FileTypeUtils {
/** /**
* 获得文件的 mineType,对于doc,jar等文件会有误差 * 获得文件的 mineType,对于doc,jar等文件会有误差
* *
* @param data 包含文件开头几千个字节的字节数组 * @param data 文件内容
* @return mineType 无法识别时会返回“application/octet-stream” * @return mineType 无法识别时会返回“application/octet-stream”
*/ */
@SneakyThrows @SneakyThrows
...@@ -37,7 +37,7 @@ public class FileTypeUtils { ...@@ -37,7 +37,7 @@ public class FileTypeUtils {
/** /**
* 在拥有文件和数据的情况下,最好使用此方法,最为准确 * 在拥有文件和数据的情况下,最好使用此方法,最为准确
* *
* @param data 包含文件开头几千个字节的字节数组 * @param data 文件内容
* @param name 文件名 * @param name 文件名
* @return mineType 无法识别时会返回“application/octet-stream” * @return mineType 无法识别时会返回“application/octet-stream”
*/ */
......
...@@ -25,7 +25,7 @@ public class FtpFileClientTest { ...@@ -25,7 +25,7 @@ public class FtpFileClientTest {
// 上传文件 // 上传文件
String path = IdUtil.fastSimpleUUID() + ".jpg"; String path = IdUtil.fastSimpleUUID() + ".jpg";
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
String fullPath = client.upload(content, path); String fullPath = client.upload(content, path, "image/jpeg");
System.out.println("访问地址:" + fullPath); System.out.println("访问地址:" + fullPath);
if (false) { if (false) {
byte[] bytes = client.getContent(path); byte[] bytes = client.getContent(path);
......
...@@ -19,7 +19,7 @@ public class LocalFileClientTest { ...@@ -19,7 +19,7 @@ public class LocalFileClientTest {
// 上传文件 // 上传文件
String path = IdUtil.fastSimpleUUID() + ".jpg"; String path = IdUtil.fastSimpleUUID() + ".jpg";
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
String fullPath = client.upload(content, path); String fullPath = client.upload(content, path, "image/jpeg");
System.out.println("访问地址:" + fullPath); System.out.println("访问地址:" + fullPath);
client.delete(path); client.delete(path);
} }
......
...@@ -101,7 +101,7 @@ public class S3FileClientTest { ...@@ -101,7 +101,7 @@ public class S3FileClientTest {
// 上传文件 // 上传文件
String path = IdUtil.fastSimpleUUID() + ".jpg"; String path = IdUtil.fastSimpleUUID() + ".jpg";
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
String fullPath = client.upload(content, path); String fullPath = client.upload(content, path, "image/jpeg");
System.out.println("访问地址:" + fullPath); System.out.println("访问地址:" + fullPath);
// 读取文件 // 读取文件
if (true) { if (true) {
......
...@@ -23,7 +23,7 @@ public class SftpFileClientTest { ...@@ -23,7 +23,7 @@ public class SftpFileClientTest {
// 上传文件 // 上传文件
String path = IdUtil.fastSimpleUUID() + ".jpg"; String path = IdUtil.fastSimpleUUID() + ".jpg";
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
String fullPath = client.upload(content, path); String fullPath = client.upload(content, path, "image/jpeg");
System.out.println("访问地址:" + fullPath); System.out.println("访问地址:" + fullPath);
if (false) { if (false) {
byte[] bytes = client.getContent(path); byte[] bytes = client.getContent(path);
......
...@@ -31,8 +31,8 @@ import org.springframework.validation.annotation.Validated; ...@@ -31,8 +31,8 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Validator; import javax.validation.Validator;
import java.util.Collection;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -193,7 +193,7 @@ public class FileConfigServiceImpl implements FileConfigService { ...@@ -193,7 +193,7 @@ public class FileConfigServiceImpl implements FileConfigService {
// 校验存在 // 校验存在
FileConfigDO config = this.validateFileConfigExists(id); FileConfigDO config = this.validateFileConfigExists(id);
if (Boolean.TRUE.equals(config.getMaster())) { if (Boolean.TRUE.equals(config.getMaster())) {
throw exception(FILE_CONFIG_DELETE_FAIL_MASTER); throw exception(FILE_CONFIG_DELETE_FAIL_MASTER);
} }
// 删除 // 删除
fileConfigMapper.deleteById(id); fileConfigMapper.deleteById(id);
...@@ -230,7 +230,7 @@ public class FileConfigServiceImpl implements FileConfigService { ...@@ -230,7 +230,7 @@ public class FileConfigServiceImpl implements FileConfigService {
this.validateFileConfigExists(id); this.validateFileConfigExists(id);
// 上传文件 // 上传文件
byte[] content = ResourceUtil.readBytes("file/erweima.jpg"); byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg"); return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
} }
@Override @Override
......
...@@ -52,7 +52,7 @@ public class FileServiceImpl implements FileService { ...@@ -52,7 +52,7 @@ public class FileServiceImpl implements FileService {
// 上传到文件存储器 // 上传到文件存储器
FileClient client = fileConfigService.getMasterFileClient(); FileClient client = fileConfigService.getMasterFileClient();
Assert.notNull(client, "客户端(master) 不能为空"); Assert.notNull(client, "客户端(master) 不能为空");
String url = client.upload(content, path); String url = client.upload(content, path, type);
// 保存到数据库 // 保存到数据库
FileDO file = new FileDO(); FileDO file = new FileDO();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论