我现在有个需求,cocos客户端要上传图片到java服务端,以下是我客户端和服务端的代码 但是服务端写出来的文件无法打开 现在不知如何处理 请求帮助
cocos creator客户端代码:
var audio_path = jsb.fileUtils.getWritablePath()+“testUpload/”;
var file_name = audio_path + “test.png”;
console.log(“文件地址”+file_name);
var file_data = jsb.fileUtils.getDataFromFile(file_name);
var xhr = new XMLHttpRequest();
xhr.open(“post”, “http://localhost:7081/wgame-api/api/sys/uploadFile”);
xhr.send(file_data);
java服务端代码:
@PostMapping("uploadFile")
@ApiOperation(value = "二进制上传图片")
public Result<String> uploadFile(@RequestBody byte[] bytes) {
System.out.println("长度" + bytes.length);
//生成uuid
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//文件路径
String path = "/Users/mac/Desktop/upload/dz/" + DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid + ".png";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
File file = new File(path);
try {
FileUtils.copyToFile(byteArrayInputStream, file);
} catch (IOException e) {
e.printStackTrace();
throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e, "");
}
return new Result<String>().ok(path);
}
打开文件时报错:
