fix 修复 dubbo 传输文件问题 临时更改 后续对接oss

2.X
疯狂的狮子li 3 years ago
parent 337ef9761b
commit 4b5d0fac02

@ -1,7 +1,6 @@
package com.ruoyi.file.api; package com.ruoyi.file.api;
import com.ruoyi.file.api.domain.SysFile; import com.ruoyi.file.api.domain.SysFile;
import org.springframework.web.multipart.MultipartFile;
/** /**
* *
@ -16,5 +15,5 @@ public interface RemoteFileService {
* @param file * @param file
* @return * @return
*/ */
SysFile upload(MultipartFile file); SysFile upload(String name, String originalFilename, String contentType, byte[] file);
} }

@ -4,6 +4,8 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.io.Serializable;
/** /**
* *
* *
@ -12,7 +14,10 @@ import lombok.experimental.Accessors;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Accessors(chain = true) @Accessors(chain = true)
public class SysFile { public class SysFile implements Serializable {
private static final long serialVersionUID = 1L;
/** /**
* *
*/ */

@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/** /**
* *
* *
@ -28,10 +30,11 @@ public class RemoteFileServiceImpl implements RemoteFileService {
* *
*/ */
@Override @Override
public SysFile upload(MultipartFile file) { public SysFile upload(String name, String originalFilename, String contentType, byte[] file) {
MultipartFile multipartFile = getMultipartFile(name, originalFilename, contentType, file);
try { try {
// 上传并返回访问地址 // 上传并返回访问地址
String url = sysFileService.uploadFile(file); String url = sysFileService.uploadFile(multipartFile);
SysFile sysFile = new SysFile(); SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(url)); sysFile.setName(FileUtils.getName(url));
sysFile.setUrl(url); sysFile.setUrl(url);
@ -41,4 +44,56 @@ public class RemoteFileServiceImpl implements RemoteFileService {
throw new ServiceException("上传文件失败"); throw new ServiceException("上传文件失败");
} }
} }
private MultipartFile getMultipartFile(String name, String originalFilename, String contentType, byte[] file) {
return new MultipartFile() {
@Override
public String getName() {
return name;
}
@Override
public String getOriginalFilename() {
return originalFilename;
}
@Override
public String getContentType() {
return contentType;
}
@Override
public boolean isEmpty() {
return getSize() == 0;
}
@Override
public long getSize() {
return file.length;
}
@Override
public byte[] getBytes() throws IOException {
return file;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(file);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(dest);
outputStream.write(file);
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
};
}
} }

@ -113,7 +113,7 @@ public class SysProfileController extends BaseController {
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException { public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
if (!file.isEmpty()) { if (!file.isEmpty()) {
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser loginUser = SecurityUtils.getLoginUser();
SysFile sysFile = remoteFileService.upload(file); SysFile sysFile = remoteFileService.upload(file.getName(), file.getOriginalFilename(), file.getContentType(), file.getBytes());
if (StringUtils.isNull(sysFile)) { if (StringUtils.isNull(sysFile)) {
return AjaxResult.error("文件服务异常,请联系管理员"); return AjaxResult.error("文件服务异常,请联系管理员");
} }

Loading…
Cancel
Save