java游戏开发总结_java--游戏后端--项目开发总结6--资源下载

示例

/**

* 获取资源

*

* @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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值