dubbo之間傳輸File檔案,將File轉成byte陣列傳輸,附上程式碼
碼雲:
gitHub:
# 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:
相機按鈕
<label for="img" class="icon iconfont icon-add" style="font-size: 80px;"></label>
大加號上傳圖片樣式
<input id="img" type="file" accept="image/*" style="opacity: 0;width: 0;height: 0" @change="upload">
上傳按鈕
<mt-button style="margin-left: 50px" @click="postImgs">上傳</mt-button>
### 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檔案
```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qky</groupId>
<artifactId>dubbo-starter-demo</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>demo-api</module>
<module>demo-consumer</module>
<module>demo-provider</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```