MVC 返回404,返回图片,流到数组,apk信息
return HttpNotFound();
byte[] buffer0 = QRCode(); return File(buffer0, @"image/jpeg"); // //通过二进制数据的方式 直接返回图片,在线浏览
Image ss = CreateImage("000000000",false,12); MemoryStream ms = new MemoryStream(); ss.Save(ms, ImageFormat.Jpeg); byte[] buffer = new byte[ms.Length]; ms.Read(buffer, 0, buffer.Length); return File(ms.ToArray(), @"image/jpeg"); // //通过二进制数据的方式
//暂无测试 FileStream fs = new FileStream(); MemoryStream ms = new MemoryStream(); img0.Save(ms, ImageFormat.Jpeg); byte[] buffer = new byte[ms.Length]; ms.Read(buffer, 0, buffer.Length); //读取数据到字节数组 fs.Read(buffer, 0, buffer.Length); //读取字节数组到缓冲区 return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
//文件的输入流
Stream filestrem = file.InputStream; StreamReader sr = new StreamReader(filestrem, System.Text.Encoding.GetEncoding("gb2312")); string stringReader = sr.ReadToEnd();
StreamReader sr = new StreamReader(filestrem, System.Text.Encoding.GetEncoding("gb2312")); string stringReader = sr.ReadToEnd(); //流转字符串 sr.BaseStream.Seek(0, SeekOrigin.Begin); //重置流 sr.Close();sr.Dispose(); //读取完后,关闭流 MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(stringReader)); return ms;
// (1)byte[] bt=ms.ToArray(); //(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length);
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsForm_CheckApk { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Cmd c = new Cmd(); string.Format("{0} d badging {1}", "aapt.exe的path", "apk的path"); var str = c.RunCmd(@"E:\迅雷下载\小米5线刷\ext4_unpacker\ext4_unpacker\aapt.exe d badging C:\14.apk"); //byte[] UTF8byte = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, byteArray); List<Encoding> encodings = new List<Encoding>(); encodings.Add(System.Text.Encoding.Unicode); encodings.Add(System.Text.Encoding.ASCII); encodings.Add(System.Text.Encoding.Default); encodings.Add(System.Text.Encoding.GetEncoding("GB2312")); //||GetEncoding("gbk") 都是GB2132 encodings.Add(System.Text.Encoding.GetEncoding("big5")); //繁体中文 encodings.Add(System.Text.Encoding.UTF7); encodings.Add(System.Text.Encoding.UTF8); encodings.Add(System.Text.Encoding.GetEncoding("GB18030")); List<byte[]> bytes = new List<byte[]>(); for (int i = 0; i < encodings.Count; i++) { bytes.Add(encodings[i].GetBytes(str)); //以指定编码读取字符串到数组 } //Unicode 转 UTF8 byte[] UTF8byte = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, bytes[0]); //将UTF8编码byte[]变为UTF8编码的字符串,编码一致,肯定可正确显示 string str1 = Encoding.UTF8.GetString(UTF8byte); List<string> outstrs = new List<string>(); //输出的字符串 for (int i = 0; i < bytes.Count; i++) { outstrs.Add(Encoding.UTF8.GetString(bytes[i], 0, bytes[i].Length)); // } //使用GetString方法,得到的string都是unicode编码的 var gb = System.Text.Encoding.Default.GetBytes(str); //使用ASCII读取和Unicode读取,都乱码 , 使用UTF8读取也乱码,,使用ANSI读取,使用UTF8解码 var gb2132_str = System.Text.Encoding.GetEncoding("GB2312").GetString(gb); var utf8_str = System.Text.Encoding.UTF8.GetString(gb); var ascii_str = System.Text.Encoding.ASCII.GetString(gb); var default_str = System.Text.Encoding.Default.GetString(gb); var ansi_str = System.Text.Encoding.UTF7.GetString(gb); var td = ""; //byte[] byteArray = System.Text.Encoding.Default.GetBytes("00"); //以默认编码读取字符串到 字节数组 //string aaa2 = System.Text.Encoding.Default.GetString(byteArray); //以默认编码 获得字节数组, 到字符串 } /// <summary> /// 字符串转为UniCode码字符串 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string StringToUnicode(string s) { char[] charbuffers = s.ToCharArray(); byte[] buffer; StringBuilder sb = new StringBuilder(); for (int i = 0; i < charbuffers.Length; i++) { buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString()); sb.Append(String.Format("\\u{0:X2}{1:X2}", buffer[1], buffer[0])); } return sb.ToString(); } /// 字符串转Unicode /// </summary> /// <param name="source">源字符串</param> /// <returns>Unicode编码后的字符串</returns> public static string String2Unicode(string source) { var bytes = Encoding.Unicode.GetBytes(source); var stringBuilder = new StringBuilder(); for (var i = 0; i < bytes.Length; i += 2) { stringBuilder.AppendFormat("\\u{0:x2}{1:x2}", bytes[i + 1], bytes[i]); } return stringBuilder.ToString(); } //编码测试 private void button2_Click(object sender, EventArgs e) { string u16s = "忘記了啊abc"; //默认的字符编码是unicode,也就是utf16 //4种编码 Encoding utf8 = Encoding.UTF8; Encoding utf16 = Encoding.Unicode; Encoding gb = Encoding.GetEncoding("gbk"); Encoding b5 = Encoding.GetEncoding("big5"); //转换得到4种编码的字节流 byte[] u16bytes = utf16.GetBytes(u16s); byte[] u8bytes = Encoding.Convert(utf16, utf8, u16bytes); byte[] gbytes = Encoding.Convert(utf16, gb, u16bytes); byte[] bbytes = Encoding.Convert(utf16, b5, u16bytes); // unicode List<string> unicode_list = new List<string>(); unicode_list.Add("unicode: "); foreach (byte c in u16bytes) { unicode_list.Add(((int)c).ToString("x") + " "); } // UTF-8 List<string> utf8_list = new List<string>(); utf8_list.Add("utf8: "); foreach (byte c in u8bytes) { utf8_list.Add(((int)c).ToString("x") + " "); } // GBK List<string> gbk_list = new List<string>(); gbk_list.Add("gbk: "); foreach (byte c in gbytes) { gbk_list.Add(((int)c).ToString("x") + " "); } // big5 List<string> big5_list = new List<string>(); big5_list.Add("big5: "); foreach (byte c in bbytes) { big5_list.Add(((int)c).ToString("x") + " "); } //得到4种编码的string string u8s = utf8.GetString(u8bytes); string gs = gb.GetString(gbytes); string bs = b5.GetString(bbytes); Console.WriteLine("unicode: " + u16s + " " + u16s.Length.ToString()); Console.WriteLine("utf8: " + u8s + " " + u16s.Length.ToString()); Console.WriteLine("gbk: " + gs + " " + gs.Length.ToString()); Console.WriteLine("big5: " + bs + " " + bs.Length.ToString()); Console.Write("unicode: "); foreach (char c in u16s) { Console.Write(((int)c).ToString("x") + " "); } Console.WriteLine(); Console.Write("utf8: "); foreach (char c in u8s) { Console.Write(((int)c).ToString("x") + " "); } Console.WriteLine(); Console.Write("gb2312: "); foreach (char c in gs) { Console.Write(((int)c).ToString("x") + " "); } Console.WriteLine(); Console.Write("big5: "); foreach (char c in bs) { Console.Write(((int)c).ToString("x") + " "); } Console.WriteLine(); } } /// <summary> /// Cmd 的摘要说明。 /// </summary> public class Cmd: Process { private Process proc = null; /// <summary> /// 构造方法 /// </summary> public Cmd() { proc = new Process(); } /// <summary> /// 执行CMD语句 /// </summary> /// <param name="cmd">要执行的CMD命令</param> public string RunCmd(string cmd) { proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); proc.StandardInput.WriteLine(cmd); proc.StandardInput.WriteLine("exit"); #region --测试代码 //System.IO.Stream t = proc.StandardOutput.BaseStream; //byte[] bytes = new byte[t.Length]; //报错 流不支持查找 //t.Read(bytes, 0, bytes.Length); //var out_str = System.Text.Encoding.UTF8.GetString(bytes); //// (1)byte[] bt=ms.ToArray(); ////(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length); System.IO.StreamReader sOut = proc.StandardOutput;//这里是获取到一个StreamReader 流 byte[] bytes; using (MemoryStream memstream = new MemoryStream()) { int bytesRead = 0; byte[] buffer = new byte[65530]; while ((bytesRead = sOut.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } bytes = memstream.ToArray(); //FileStream keyfs = new FileStream(Server.MapPath("/key/regkey2.dat"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Write); //BinaryWriter w = null; //w = new BinaryWriter(keyfs); //for (int t = 0; t < bytes.Length; t++) //{ // w.Write((byte)bytes[t]); //} memstream.Close(); //w.Close(); //keyfs.Close(); } // t..CopyTo(ms); //var ms = StreamToMemoryStream(t); var out_str = System.Text.Encoding.Default.GetString(bytes); #endregion #region --原始代码 string outStr = proc.StandardOutput.ReadToEnd(); proc.Close(); return outStr; #endregion } System.IO.MemoryStream StreamToMemoryStream(System.IO.Stream instream) { System.IO.MemoryStream outstream = new System.IO.MemoryStream(); const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = instream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } return outstream; } /// <summary> /// 打开软件并执行命令 /// </summary> /// <param name="programName">软件路径加名称(.exe文件)</param> /// <param name="cmd">要执行的命令</param> public void RunProgram(string programName, string cmd) { Process proc = new Process(); proc.StartInfo.CreateNoWindow = true; proc.StartInfo.FileName = programName; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.Start(); if (cmd.Length != 0) { proc.StandardInput.WriteLine(cmd); } proc.Close(); } /// <summary> /// 打开软件 /// </summary> /// <param name="programName">软件路径加名称(.exe文件)</param> public void RunProgram(string programName) { this.RunProgram(programName, ""); } }
先把流转换为字节数组 System.IO.StreamReader sOut = proc.StandardOutput;//这里是获取到一个StreamReader流 using (MemoryStream memstream = new MemoryStream()) { int bytesRead = 0; byte[] buffer = new byte[65530]; while ((bytesRead = sOut.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { memstream.Write(buffer, 0, bytesRead); } bytes = memstream.ToArray(); memstream.Close(); } 再把字节数组以指定编码转为字符串 var out_str = System.Text.Encoding.UTF8.GetString(bytes);