zoukankan      html  css  js  c++  java
  • ASP.NET 上传图片到FTP

    目录:

      2.代码

      3.参考资料

      4.IIS环境FTP配置

      5.使用虚拟目录注意Server.MapPath()

    1. 项目介绍

       建立FTP文件服务器与应用程序分开.

       下面方法中的参数为Stream因为使用的是fineUI,已经将流上传,如果是其他控件,后面有FileStream,Bitmap,byte[]之间的参考资料.

    2.测试代码

    /// <summary>
            /// Bitmap:封装 GDI+ 包含图形图像和其属性的像素数据的位图。 一个 Bitmap 是用来处理图像像素数据所定义的对象。
            /// 难点:
            ///         1.Stream转Bitmap,压缩图片
            ///         2.Bitmap 与 byte[] 转换 (Bitmap转MemoryStream,再通过ms.ToArray()转byte[])
            ///         3.创建FTP上载数据流,写入字节数(FTP服务器分IIS级别配置,和应用程序级别配置,两个要一致.安全级别高的使用指定用户,安全低的可以所以用户)
            ///         
            /// </summary>
            /// <param name="stream">继承抽象类的实例(一般是FileStream,MemoryStream)</param>
            /// <param name="url">FTP地址</param>
            /// <param name="filename">服务器中的文件名(ftp://192.168.1.127/190_140/636288137130851325admin_Penguins.jpg)</param>
            public void SaveStream(Stream stream,string url,string filename)
            {
                MemoryStream ms = null;
                Stream strm = null;
                try
                {
                    /// 1.Stream 转成 Bitmap 并压缩图片
                    Bitmap pimage = new Bitmap(stream);
                    System.Drawing.Imaging.ImageFormat fromat = pimage.RawFormat;
                    Bitmap bitNewPic = new Bitmap(pimage, 190, 140);
                    /// 2.Bitmap 转成MemoryStream(继承Stream抽象类)
                    ms = new MemoryStream();
                    bitNewPic.Save(ms, fromat);
                    /// 3.MemoryStream转成byte[]数组 "imagebyte"
                    byte[] imagebyte = new Byte[ms.Length];
                    imagebyte = ms.ToArray();
                    /// 4.创建FTP服务器连接 "reqFTP"
                    string uri = filename;
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                    reqFTP.Credentials = new NetworkCredential("administrator", "vtlongxing");
                    reqFTP.KeepAlive = false;
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.ContentLength = imagebyte.Length;
                    /// 5.创建FTP服务器上载数据的流 "strm",并向"strm"写入字节序列
                    strm = reqFTP.GetRequestStream();
                    strm.Write(imagebyte, 0, imagebyte.Length);
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    /// 6.关闭各"流"
                    strm.Close(); 
                    ms.Close();
                    stream.Close();
                }
            }

    3.filestream,bety,Bitmap操作参考

    http://blog.csdn.net/wangyue4/article/details/6819102

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace AppBox.CRM.Gift
    {
        public class ImageHelper
        {
            //byte[] 转图片  
            public static Bitmap BytesToBitmap(byte[] Bytes)
            {
                MemoryStream stream = null;
                try
                {
                    stream = new MemoryStream(Bytes);
                    return new Bitmap((Image)new Bitmap(stream));
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                catch (ArgumentException ex)
                {
                    throw ex;
                }
                finally
                {
                    stream.Close();
                }
            }
    
            //图片转byte[]   
            public static byte[] BitmapToBytes(Bitmap Bitmap)
            {
                MemoryStream ms = null;
                try
                {
                    ms = new MemoryStream();
                    Bitmap.Save(ms, Bitmap.RawFormat);
                    byte[] byteImage = new Byte[ms.Length];
                    byteImage = ms.ToArray();
                    return byteImage;
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
                finally
                {
                    ms.Close();
                }
            }
    
    
    
            /// <summary>  
            /// 将 Stream 转成 byte[]  
            /// </summary>  
            public byte[] StreamToBytes(Stream stream)
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
    
                // 设置当前流的位置为流的开始  
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
    
            /// <summary>  
            /// 将 byte[] 转成 Stream  
            /// </summary>  
            public Stream BytesToStream(byte[] bytes)
            {
                Stream stream = new MemoryStream(bytes);
                return stream;
            }
    
    
            /* - - - - - - - - - - - - - - - - - - - - - - - -  
             * Stream 和 文件之间的转换 
             * - - - - - - - - - - - - - - - - - - - - - - - */
            /// <summary>  
            /// 将 Stream 写入文件  
            /// </summary>  
            public void StreamToFile(Stream stream, string fileName)
            {
                // 把 Stream 转换成 byte[]  
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始  
                stream.Seek(0, SeekOrigin.Begin);
    
                // 把 byte[] 写入文件  
                FileStream fs = new FileStream(fileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(bytes);
                bw.Close();
                fs.Close();
            }
    
            /// <summary>  
            /// 从文件读取 Stream  
            /// </summary>  
            public Stream FileToStream(string fileName)
            {
                // 打开文件  
                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                // 读取文件的 byte[]  
                byte[] bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);
                fileStream.Close();
                // 把 byte[] 转换成 Stream  
                Stream stream = new MemoryStream(bytes);
                return stream;
            }
        }
    }
    View Code

    4.IIS下FTP配置

    (FTP服务器分IIS级别配置,和应用程序级别配置,两个要一致.安全级别高的使用指定用户,安全低的可以所以用户)

    http://www.juheweb.com/Tutorials/fwq/windows/335.html

    5.虚拟目录   (使用虚拟目录和相对路径要用Server.MapPath())

    // <virtualDirectory path="/VImage/" physicalPath="D:UploadfileCRMImage" /> iis express 配置虚拟目录
            string strImagePathV = "~/upimage/";//虚拟目录
            string strImagePath190_140V = "~/upimage/190_140/";//虚拟目录
    string strPath = Server.MapPath(strImagePathV + fileName);
                    string strPathchange = Server.MapPath(strImagePath190_140V + fileName);
    UploadImage img = new UploadImage();
                    img.Save(tab1UploadImage.PostedFile.InputStream, strPathchange);
    
    public class UploadImage
        {
            public bool Save(Stream stream, string imagename)
            {
                try
                {
                    Bitmap pimage = new Bitmap(stream);
                    Bitmap bitNewPic = new Bitmap(pimage, 190, 140);
                    bitNewPic.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    
        }
  • 相关阅读:
    SQL优化值-exists
    Oracle执行计划详细讲解
    SpringBoot配置加解密工具之Jasypt
    升级Spring Boot 2.0后RelaxedPropertyResolver不可用的解决方案
    Spring, SpringBoot之占位符源码解析
    springboot启动原理深度解析
    Java SPI技术深度解读
    [VCSA7]添加共享磁盘
    [VCSA7]MacOS部署VCSA7
    [Oracle]查看数据库磁盘空间
  • 原文地址:https://www.cnblogs.com/chirs888888/p/6769019.html
Copyright © 2011-2022 走看看