1.下載源代碼https://github.com/shiwen1997/gxu_ar ,在AS中打開:
打開運行可能會報錯。
一般是gradle的錯誤,確保翻牆(有些google的依賴下載不來),並且把app的gradle依賴版本改成最新的
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.google.ar:core:1.4.0'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('src/main/java/build/java/Vuforia/Vuforia.jar')
2.服務器下載模型文件接口:
①新建IDEA項目file1223,新建DownloadController.java、application.yml兩個文件
package com.example.file1223;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DownloadController
{
@RequestMapping({"/downfile/{id}"})
public String downloadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws Exception {
String fileName = id;
if (fileName != null) {
String realPath = "/var/files/modelsfile/";
// String realPath = "H:/modelsfile";
File file = new File(realPath, fileName);
if (file.exists()) {
response.setContentType("application/force-download");
response.setHeader("Content-Length", file.length() + "");
response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
ServletOutputStream servletOutputStream = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
servletOutputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return null;
}
}
yml文件設置端口
②打包部署:點擊右邊Maven projects 的package生成jar文件
③.將資源文件(AR模型文件)放在服務器上:一定要放對目錄
④.運行jar包
nohup java -jar file1223-0.0.1.jar >myfile20191201.out &
file1223-0.0.1.jar是jar包的名字
檢測:瀏覽器輸入47.101.207.179:8083/downfile/dmsbtexcoords.txt測試:能下載文件就行。
⑤修改AS工程內部代碼:
在DMSBActivity.java文件、MJWActivity.java文件裏使用了兩個模型文件下載接口:只用將47.101.207.179:8084改成服務器的IP就可以了