zoukankan      html  css  js  c++  java
  • 读取文件转换为byte阵列

    让wdinwos Server去监控某一文件夹,是否有新文件产生,如果有的话,即把它上传至互联网上一台云服务器上。
    刚开始,我是写了一个Windows Service的,但InstallUtil.exe之后,它死活没有把文件夹新创建的文件上传。
    后来,不得不写成一个控制台应用程序。后来查找到相关资料,如果与桌面有交互的,还是写成控制台应用程序较好。

    由于上传的文件存放的服务器是,互联网云服务器。Insus.NET想过许多方案,最终是将文件转换为数据流,上传至云服务器ms sql server。

    基中,有一个方法,根据路径文件转换为数据流的方法:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace Insus.NET.Utility
    {
        public static class ImgUtil
        {
            public static byte[] ReadFileToByteArray(string filePath)
            {
                byte[] buffer;
                FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                try
                {
                    int length = (int)fileStream.Length;
                    buffer = new byte[length];
                    int count;
                    int sum = 0;
    
                    while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                        sum += count;
                }
                finally
                {
                    fileStream.Close();
                }
                return buffer;
            }
        }
    }
    Source Code
  • 相关阅读:
    第二周学习总结
    第一周学习进度总结
    淘宝网质量属性分析
    软件架构师如何工作
    寒假学习记录第十六天
    寒假学习记录第十五天
    寒假学习记录第十四天
    寒假学习记录第十三天
    三.NFS存储服务
    二.Rsync备份服务
  • 原文地址:https://www.cnblogs.com/insus/p/15226499.html
Copyright © 2011-2022 走看看