最近遇到一个简单需求,就是要能动态算出给出的几组数据的组合方式。
我也问了好多朋友,但都没解决,上次我一篇博文 如何下载未知扩展名的图片的文章(
)就在博客园各位大侠的帮助下顺利解决,博客园的平台真的很不错。
我把我最终整理的下载未知扩展名的图片的方法贴出来:
/// <summary>
/// 抓取远程图片 自动识别扩展名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private string getRemoteImg(string url)
{
string rtnString = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
string fileExt = string.Empty;
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssss");
string FileExtNum = string.Empty;
using (Stream stream = response.GetResponseStream())
{
byte[] retBytes = null;
using (MemoryStream ms = new MemoryStream())
{
int b;
int i = 0;
while ((b = stream.ReadByte()) != -1)
{
i++;
if (i <= 2)
{
FileExtNum += b.ToString();
}
ms.WriteByte((byte)b);
}
retBytes = ms.ToArray();
}
switch (FileExtNum)
{
case "255216":
fileExt = "jpg";
break;
case "7173":
fileExt = "gif";
break;
case "13780":
fileExt = "png";
break;
case "6677 ":
fileExt = "bmp";
break;
}
if (!string.IsNullOrEmpty(fileExt))
{
if (!Directory.Exists(Application.StartupPath + "\\UploadFiles\\"))//若文件夹不存在则新建文件夹
{
Directory.CreateDirectory(Application.StartupPath + "\\UploadFiles\\"); //新建文件夹
}
string localPath = Application.StartupPath + "\\UploadFiles\\" + fileName + "." + fileExt + "";
using (FileStream fs = new FileStream(localPath, FileMode.Create))
{
using (BinaryWriter sr = new BinaryWriter(fs))
{
sr.Write(retBytes, 0, retBytes.Length);
sr.Close(); fs.Close();
if (System.IO.File.Exists(localPath))
{
rtnString = "UploadFiles/" + fileName + "." + fileExt + "";
}
}
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return rtnString;
}
/// 抓取远程图片 自动识别扩展名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private string getRemoteImg(string url)
{
string rtnString = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
string fileExt = string.Empty;
string fileName = DateTime.Now.ToString("yyyyMMddhhmmssss");
string FileExtNum = string.Empty;
using (Stream stream = response.GetResponseStream())
{
byte[] retBytes = null;
using (MemoryStream ms = new MemoryStream())
{
int b;
int i = 0;
while ((b = stream.ReadByte()) != -1)
{
i++;
if (i <= 2)
{
FileExtNum += b.ToString();
}
ms.WriteByte((byte)b);
}
retBytes = ms.ToArray();
}
switch (FileExtNum)
{
case "255216":
fileExt = "jpg";
break;
case "7173":
fileExt = "gif";
break;
case "13780":
fileExt = "png";
break;
case "6677 ":
fileExt = "bmp";
break;
}
if (!string.IsNullOrEmpty(fileExt))
{
if (!Directory.Exists(Application.StartupPath + "\\UploadFiles\\"))//若文件夹不存在则新建文件夹
{
Directory.CreateDirectory(Application.StartupPath + "\\UploadFiles\\"); //新建文件夹
}
string localPath = Application.StartupPath + "\\UploadFiles\\" + fileName + "." + fileExt + "";
using (FileStream fs = new FileStream(localPath, FileMode.Create))
{
using (BinaryWriter sr = new BinaryWriter(fs))
{
sr.Write(retBytes, 0, retBytes.Length);
sr.Close(); fs.Close();
if (System.IO.File.Exists(localPath))
{
rtnString = "UploadFiles/" + fileName + "." + fileExt + "";
}
}
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return rtnString;
}
这次小弟又遇到一个棘手的新问题,都已经好多天了,也问了不少朋友,都没有得到解决,这次又要来麻烦园子里的朋友了。也希望大家能多多交流!
先贴一个图: 这个数据是我自己编造的 ,是为了更好的把问题解释清楚。
如上图,假如要组装一个电脑 电脑的某一个配件可以选择不同的品牌 不同的品牌的价格当然也不一样
所以 希望能知道,要配一个电脑,可以用以上不同类别的组件来配,各种组合的情况是什么样的 不考虑每一个类别之间的兼容性 只要排列方式。
所以 希望能知道,要配一个电脑,可以用以上不同类别的组件来配,各种组合的情况是什么样的 不考虑每一个类别之间的兼容性 只要排列方式。