https://gitee.com/qinkaiyuan/Transfer-files-using-Dubbo#dubbo%E6%9C%8D%E5%8A%A1%E4%B9%8B%E9%97%B4%E4%BC%A0%E8%BE%93file-transfer-files-using-dubbo
https://github.com/qinkaiyuan/Transfer-files-between-Dubbo/blob/master/README.md#dubbo%E6%9C%8D%E5%8A%A1%E4%B9%8B%E9%97%B4%E4%BC%A0%E8%BE%93file-transfer-files-between-dubbo
# dubbo服务之间传输File Transfer files between Dubbo
#### 项目介绍
dubbo之间传输File文件,将File转成byte数组传输,附上代码
项目没有使用zokkeeper
先启动服务端代码,之后才能启动消费端,否则的话是无法访问服务端的,
启动项目后输入http://localhost:8080/1
点击页面中的小相机按钮上传图片,然后点击上传按钮
图片则会上传到电脑的 D:\file\a.jpg
### 参考
dubbo-spring-boot-starter使用的是阿里官方整合的springboot框架
https://github.com/alibaba/dubbo-spring-boot-starter/blob/master/README_zh.md
dubbo-spring-boot-starter使用方式参考了以下文章
https://blog.csdn.net/qq_36890499/article/details/80858663
Java 文件和byte数组转换 参考:
https://www.cnblogs.com/kgdxpr/p/3595518.html
### 代码部分:
前端部分有很多没有用的代码,不需要关注,
重要代码
前端:
使用了vue和mint-ui
### 首页代码
### html:
相机按钮
大加号上传图片样式
上传按钮
### js:
data: { imgs: [],//图片文件数组
talkImgs: [],//图片名数组
index: 0 //图片数量 好像没用到大写尴尬
},
methods: {
//上传图片后出发的方法
upload(obj) {
const reader = new FileReader()
reader.readAsDataURL(obj.target.files[0]);
reader.onload = function () {
app.imgs.push({img: obj.target.files[0], src: this.result})
app.talkImgs.push({imgSrc: obj.target.files[0].name})
app.index++
}
},
//点击上传按钮执行的方法
postImgs() {
let formData = new FormData();
let imgs = []
for (let img of this.imgs) {
imgs.push(img.img)
formData.append("files", img.img)
}
axios.post('/upload', formData)
.then(response => {
console.log(response.data);
})
.catch(function (error) {
alert("上传失败");
console.log(error);
});
}
### java api:
public interface DemoService {
String sayHello(String name);
String convertFile(byte[] bytes);
}
### java 消费端
@Controller
public class IndexController {
@RequestMapping("1")
public String index() {
System.out.println("123");
return "index";
}
}
@RestController
public class UploadController {
@Reference(version = "1.0.0",
application = "${dubbo.application.id}",
url = "dubbo://localhost:12345")
private DemoService demoService;
@PostMapping("upload")
public String upload(MultipartFile[] files) {
for (MultipartFile file : files) {
if (Objects.isNull(file) || file.isEmpty()) {
return "文件为空,请重新上传";
}
try {
System.out.println(file.getName());
System.out.println(file.getSize());
byte[] bytes = file.getBytes();
return demoService.convertFile(bytes);
} catch (IOException e) {
e.printStackTrace();
return "失败";
}
}
return "失败";
}
}
### java服务端:
@Service(
version = "1.0.0",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
@Override
public String convertFile(byte[] bytes) {
System.out.println(bytes);
System.out.println(123);
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
String filePath = "d:/file";
File dir = new File(filePath);
if (!dir.exists() || !dir.isDirectory()) {//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "/a.jpg");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
System.out.println(file.getName());
System.out.println(file.length());
System.out.println(file.toPath());
} catch (IOException e) {
e.printStackTrace();
return "失败";
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return "成功";
}
}
### pom文件
```
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
```