示例
/**
* 获取资源
*
* @param request 请求
* @param response 响应
*/
@RequestMapping("/getFile")
public void getFile(HttpServletRequest request, HttpServletResponse response) {
//请求信息
String version = request.getParameter("version");
String data = request.getParameter("data");
String platform = request.getParameter("platform");
//获取版本文件
String xmlPath = "";
try {
File path = new File(ResourceUtils.getURL("classpath:").getPath());
while (true) {
File parentFile = path.getParentFile();
if (parentFile.getParent().endsWith("mapleStoryBackstage")) {
xmlPath = parentFile.getParent() + "/assetBundles/" + platform + "/version.xml";
break;
} else {
path = path.getParentFile();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//验证版本
AssetBundleVersion serverAssetBundleVersion = appService.assetBundleVersionXmlDecode(new File(xmlPath));
ConcurrentHashMap serverAssetBundleMap = serverAssetBundleVersion.getAssetBundleMap();
List assetBundleList = new ArrayList<>();
if (StringUtils.isBlank(data)) {
LogManger.info("首次更新资源");
serverAssetBundleMap.values().forEach(assetBundle -> assetBundleList.add(assetBundle.getUrl()));
} else if (version.equals(serverAssetBundleVersion.getVersion())) {
LogManger.info("已是最新版");
return;
} else {
//资源比对
LogManger.info("增量更新资源");
List assetBundleList1 = JSONArray.parseArray(data, AssetBundle.class);
Map a = new ConcurrentHashMap<>();
assetBundleList1.forEach(assetBundle -> {
AssetBundle assetBundle1 = a.get(assetBundle.getName());
if (null == assetBundle1) {
a.put(assetBundle.getName(), assetBundle);
} else {
System.out.println(assetBundle.getName());
}
});
Map clientAssetBundleMap = JSONArray.parseArray(data, AssetBundle.class).stream().collect(Collectors.toMap(AssetBundle::getName, (p) -> p));
serverAssetBundleMap.forEach((key, value) -> {
AssetBundle assetBundle = clientAssetBundleMap.get(key);
if (null == assetBundle) {
assetBundleList.add(value.getUrl());
} else {
if (!value.getMd5().equals(assetBundle.getMd5())) {
assetBundleList.add(value.getUrl());
}
}
});
}
//资源目录
String destFileName = "assetBundle";
String[] split = xmlPath.split("/");
StringBuilder basePath = new StringBuilder();
for (int i = 0; i < split.length - 1; i++) {
basePath.append(split[i]).append("/");
}
//创建压缩文件
FileOutputStream fileOutputStream;
ZipOutputStream zipOutputStream;
FileInputStream fileInputStream = null;
File zipFile = new File(platform + "_" + destFileName + ".zip");
try {
fileOutputStream = new FileOutputStream(zipFile.getName());
zipOutputStream = new ZipOutputStream(fileOutputStream);
for (String path : assetBundleList) {
addToZip(fileInputStream, basePath, zipOutputStream, path);
}
addToZip(fileInputStream, basePath, zipOutputStream, "WindowsPlayer");
zipOutputStream.closeEntry();
zipOutputStream.close();
if (fileInputStream != null) {
fileInputStream.close();
}
fileOutputStream.close();
byte[] fileBytes = FileUtil.readFileToByteArray(zipFile);
byte[] versionBytes = serverAssetBundleVersion.getVersion().getBytes();
ByteBuf byteBuf = Unpooled.copiedBuffer(ByteUtil.int2Bytes(versionBytes.length), versionBytes, fileBytes);
byte[] bytes = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(bytes);
//通过response发送数据
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + platform + "_" + destFileName);
response.setContentLength(bytes.length);
response.setContentType("multipart/form-data");
InputStream in = new ByteArrayInputStream(bytes);
OutputStream out = response.getOutputStream();
int b;
byte[] bufferOut = new byte[1024];
while ((b = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, b);
}
out.close();
in.close();
zipFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
标签:java,游戏,assetBundle,path,new,byte,response,下载,String
来源: https://www.cnblogs.com/plxz/p/10951687.html