Unity3D 上传日志
发布日期:2021-06-30 19:37:43
浏览次数:3
分类:技术文章
本文共 2599 字,大约阅读时间需要 8 分钟。
上传日志到服务器,需要先将日志进行压缩,首先下载压缩库Ionic.Zip.dll
Ionic.Zip.dll下载:http://yunpan.cn/cj7U6E4RrRh6m 访问密码 b6df
丢到工程的Assets\Plugins目录下
然后
using Ionic.Zip;
接着就可以使用啦
上传文件需要用到WWW,所以要使用协程,所以要继承MonoBehaviour,或者继承MonoBehaviour的子类
using Ionic.Zip;using System.IO;using UnityEngine;public class UploadFile:MonoBehaviour{ protected override void Start() { string filePath="d:/test/test.text"; string savePath="d:/test/test.zip"; #if !UNITY_EDITOR filePath = "mnt/sdcard/output_log.txt"; savePath = "mnt/sdcard/output_log.zip"; #endif try { if(File.Exists(filePath)) { using(ZipFile zip = new ZipFile()) { ZipEntry e = zip.AddFile(filePath,""); zip.Save(savePath); } using(FileStream fs = File.OpenRead(savePath)) { long i=0; long length = fs.Length; byte[] data = new byte[length]; long offset = data.Length>1024?1024:data.Length; while(ioffset?offset:tem; //以时间为文件名// DateTime now = DateTime.Now; string fileName=string.Format("{0}.dat",now.Year+""+now.Month+""+now.Day+"_"+now.Hour+""+now.Minute+""+now.Second); StartCoroutine(upload(fileName,data)); } } } } } IEnumerator upload(string fileName,byte[] data) { WWWForm form = new WWWForm(); form.AddField("type","file"); form.AddBinaryData("log_file", data, fileName, "application/x-gzip"); string url="http://192.168.0.119:8080/uploadLog"; #if !UNITY_EDITOR url = "http://192.168.218.1:8080/uploadLog"; //外网的地址// #endif WWW w=new WWW(url,form); yield return w; if(w.error != null) { Debug.LogError(w.error); } else { if(w.text != null) { Debug.Log("Finished Uploading:"+w.text); } } yield return null; }}
注意,WWWForm的AddBinaryData接口的第四个参数是mimeType,
常见的MIME类型如下: 超文本标记语言文本.html, text/html 普通文本 .txt text/plain RTF文本 .rtf application/rtf GIF图形 .gif image/gif JPEG图形 .ipeg,.jpg image/jpeg au声音文件 .au audio/basic MIDI音乐文件 .mid,.midi audio/midi, audio/x-midi RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio MPEG文件 .mpg,.mpeg video/mpeg AVI文件 .avi video/x-msvideo GZIP文件 .gz application/x-gzip TAR文件 .tar application/x-tar转载地址:https://linxinfa.blog.csdn.net/article/details/45874761 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月06日 20时21分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
基于MATLAB的二进制数字调制与解调信号的仿真——2PSK
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——AM
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——DSB
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——SSB
2019-04-30
操作系统实验之生产者和消费者程序
2019-04-30
操作系统实验之猴子过桥问题的模拟程序
2019-04-30
POJ - 3067 Japan (树状数组 思维)
2019-04-30
POJ - 2352 Stars (树状数组 入门题)
2019-04-30
HDU - 1166 敌兵布阵 (树状数组模板题/线段树模板题)
2019-04-30
CodeForces - 761C Dasha and Password (思维 暴力)
2019-04-30
POJ - 2481 Cows (树状数组 入门题)
2019-04-30
ACM-ICPC 2018 焦作赛区网络预赛 I. Save the Room
2019-04-30
CodeForces - 987C Three displays (暴力/dp)
2019-04-30
计蒜客 NAIPC 2016 F. Mountain Scenes(dp)
2019-04-30
牛客国庆集训派对Day4——I 连通块计数(思维)
2019-04-30
牛客国庆集训派对Day4——G 区间权值(找规律,双重前缀和)
2019-04-30
牛客国庆集训派对Day5——L 数论之神(找规律/数论)
2019-04-30