开发环境:VS2003、VS2005// 上传到本地服务器,或者理解为程序运行的服务器,一切正常,上传的是一些产品的图片,都比较小
myPath = Server.MapPath("UploadFiles/");
FileUpload1.PostedFile.SaveAs(myPath + myFileName);问题:用户要求将所有的图片上传到一台指定的服务器,例如:211.134.34.26/UploadFiles,同程序运行不在一台服务器上
访问该服务器的帐号和密码暂定为:abc / 123
这种情况下的上传代码该如何修改呢?
myPath = Server.MapPath("UploadFiles/");
FileUpload1.PostedFile.SaveAs(myPath + myFileName);问题:用户要求将所有的图片上传到一台指定的服务器,例如:211.134.34.26/UploadFiles,同程序运行不在一台服务器上
访问该服务器的帐号和密码暂定为:abc / 123
这种情况下的上传代码该如何修改呢?
#region 创建文件夹 MakeFolder()
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="dirName">例如:/newDir</param>
/// <param name="url">ftp地址(可带目录)</param>
/// <param name="userid">ftp用户名</param>
/// <param name="password">ftp密码</param>
public void MakeFolder(string FolderName, string url, string userid, string password)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + url + FolderName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(userid, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
throw new Exception("此处是创建文件夹:"+ex.Message + "创建文件夹:ftp://" + url + FolderName + "不成功!");
}
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="stream">数据流</param>
/// <param name="thumbnailPath">缩略图路径(ftp相对路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public void MakeThumbnail(Stream stream, string thumbnailPath, int width, int height, string mode)
{ System.Drawing.Image originalImage = System.Drawing.Image.FromStream(stream); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel); try
{
MemoryStream temp = new MemoryStream(); bitmap.Save(temp, System.Drawing.Imaging.ImageFormat.Jpeg);
temp.Position = 0;
UploadFileByFTP(this.ftpHost, this.ftpUserId, this.ftpPwd, temp, thumbnailPath);
}
catch (System.Exception e)
{
throw new Exception("此处是生成缩略图:"+e.Message+"生成缩略图失败!"); }
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
#region FTP上传文件方法,设定ftp下的保存路径,文件名 UploadFileByFTP()
/// <summary>
/// FTP上传文件方法,设定ftp下的保存路径,文件名
/// </summary>
/// <param name="ftpServerIP">ftp地址</param>
/// <param name="ftpUserID">ftp用户名</param>
/// <param name="ftpPassword">ftp密码</param>
/// <param name="streamRead">上传文件流</param>
/// <param name="savePath">文件名(可带相对路径)</param>
/// <exception >throw</exception>
protected void UploadFileByFTP(string ftpServerIP, string ftpUserID, string ftpPassword, Stream streamRead, string filename)
{
string l_notice = "";//上传提示
string uri = string.Empty;
//如果filename为空提示选择文件
if (filename == null || filename == "")
{
l_notice = "Please select File !";
}
else
{
try
{
if (filename.StartsWith("/"))
{
filename = filename.Remove(0, 1);
}
uri = "ftp://" + ftpServerIP + "/" + filename; FtpWebRequest reqFTP;
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false; // 指定执行命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型
reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小
reqFTP.ContentLength = streamRead.Length; // 缓冲大小设置为5kb
int buffLength = 5048; byte[] buff = new byte[buffLength];
int contentLen;
Stream streamWrite = reqFTP.GetRequestStream();
streamRead.Flush();
contentLen = streamRead.Read(buff, 0, buffLength);
while (contentLen != 0)
{
streamWrite.Write(buff, 0, contentLen);
contentLen = streamRead.Read(buff, 0, buffLength);
}
// 关闭两个流
streamWrite.Close();
}
catch (Exception ex)
{
throw new Exception("此处是上传方法:"+ex.Message + "ftp路径:" + uri);
}
}
Response.Write(l_notice);
}
TCP协议连接,但是要牵扯到分包组包的问题,还有Qos==.