文件分布式存储的代码实现

Posted 善良的小赵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件分布式存储的代码实现相关的知识,希望对你有一定的参考价值。

1.web服务器端

 1   public ActionResult DealFile()
 2         {
 3             HttpPostedFileBase file = Request.Files["imageFile"];
 4             if (file != null)
 5             {
 6                 string fileName = file.FileName;
 7                 string ext = Path.GetExtension(fileName);
 8                 if (ext == ".jpg")
 9                 { 
10                    //选择服务器
11                     MyImageServerEntities dbContext = new MyImageServerEntities();
12                    IList<ImageServerInfo> serverList= dbContext.ImageServerInfo.Where(server => server.FlgUsable == true).ToList();
13                    int count = serverList.Count;
14                    Random random = new Random();
15                    int data = random.Next();
16                    int index = data % count;
17                    ImageServerInfo serverInfo = serverList[index];
18                    string serverIP = serverInfo.ServerUrl;
19                    string address = "http://" + serverIP + "/FileUp.ashx?serverId=" + serverInfo.ServerId + "&extention=" + ext;
20                    WebClient client = new WebClient();
21                     client.UploadData(address ,StreamToByte(file.InputStream));
22                 }
23                 else
24                 {
25                     return Content("文件的格式不对");
26                 }
27             }
28             else
29             {
30                 return Content("接收文件失败");
31             }
32             return Content("OK");
33         }

2.图片服务器端

 1   public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "text/plain";
 4             // context.Response.Write("Hello World");
 5             string dir = "/Image/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
 6             string serverId = context.Request["serverId"];
 7             string ext = context.Request["extention"];
 8             string fileName = Guid.NewGuid().ToString();
 9             Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(dir)));
10             string fullName = context.Server.MapPath(dir) + fileName + ext;
11             using (FileStream fileStream =File.OpenWrite(fullName))
12             {
13                 Stream stream = context.Request.InputStream;
14                 stream.CopyTo(fileStream);
15                 //需要将文件信息写到数据库中去,同时可以写到缓存中(Session)
16                 Model.ImageInfo imageInfo = new Model.ImageInfo();
17                 imageInfo.ImageServerId = int.Parse(serverId);
18                 imageInfo.ImageName = dir + fileName + ext;
19                 Model.MyImageServerEntities dbContext = new Model.MyImageServerEntities();
20                 dbContext.ImageInfo.Add(imageInfo);
21                 dbContext.SaveChanges();
22 
23             }
24 
25         }

 

以上是关于文件分布式存储的代码实现的主要内容,如果未能解决你的问题,请参考以下文章

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

Go语言实现FastDFS分布式存储系统WebAPI网关

ASP.net MVC 代码片段问题中的 Jqgrid 实现

如何将代码片段存储在 mongodb 中?

是否可以动态编译和执行 C# 代码片段?

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途