然后你需要FTP的IP、端口、FTP账户、FTP密码
再通过FTP软件链接到服务器,然后就可以下载网站源码了
自己把这些方法写到一个类里.我分二次回答,一次...您的回答内容多于10000字,请删减! string ftpServerIPstring ftpUserID
string ftpPassword
FtpWebRequest reqFTP
private void Connect(String path)//连接ftp
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path))
// 指定数据传输类型
reqFTP.UseBinary = true
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword)
}
public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP
this.ftpUserID = ftpUserID
this.ftpPassword = ftpPassword
}
//都调用这个
//从ftp服务器上获得文件列表
private string[] GetFileList(string path, string WRMethods)
{
string[] downloadFiles
StringBuilder result = new StringBuilder()
try
{
Connect(path)
reqFTP.Method = WRMethods
WebResponse response = reqFTP.GetResponse()
StreamReader reader =
new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default)//中文文件名
string line = reader.ReadLine()
while (line != null)
{
result.Append(line)
result.Append("\n")
line = reader.ReadLine()
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1)
reader.Close()
response.Close()
return result.ToString().Split('\n')
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message)
downloadFiles = null
return downloadFiles
}
}
//从ftp服务器上获得文件列表
public string[] GetFileList(string path)
{
return GetFileList("ftp://" + ftpServerIP + "/" + path,
WebRequestMethods.Ftp.ListDirectory)
}
//从ftp服务器上获得文件列表
public string[] GetFileList()
{
return GetFileList("ftp://" + ftpServerIP + "/",
WebRequestMethods.Ftp.ListDirectory)
}
//从ftp服务器上载文件的功能
public void Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename)
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name
Connect(uri)//连接
// 默认为true,连接不会被关闭
// 在一个命令之后被执行
reqFTP.KeepAlive = false
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.UploadFile
// 上传文件时通知服务器文件的大小
reqFTP.ContentLength = fileInf.Length
// 缓冲大小设置为kb
int buffLength = 2048
byte[] buff = new byte[buffLength]
int contentLen
// 打开一个文件流(System.IO.FileStream) 去读上传的文件
FileStream fs = fileInf.OpenRead()
try
{
// 把上传的文件写入流
Stream strm = reqFTP.GetRequestStream()
// 每次读文件流的kb
contentLen = fs.Read(buff, 0, buffLength)
// 流内容没有结束
while (contentLen != 0)
{
// 把内容从file stream 写入upload stream
strm.Write(buff, 0, contentLen)
contentLen = fs.Read(buff, 0, buffLength)
}
// 关闭两个流
strm.Close()
fs.Close()
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error")
}
}
这个是可以向服务器端发送文字的程序,就是在客户端发送一句hello在服务器也可以接受到hello,这个程序可以修改一下就可以了。具体修改方法是增加一个定时器,然后把字符流改成字节流,现在有点忙,你先研究啊,近两天帮你写写看。服务器端:
import java.net.*
import java.io.*
public class DateServer {
public static void main(String[] args) {
ServerSocket server=null
try{
server=new ServerSocket(6666)
System.out.println(
"Server start on port 6666...")
while(true){
Socket socket=server.accept()
new SocketHandler(socket).start()
/*
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(new java.util.Date().toLocaleString())
out.close()
*/
}
}catch(Exception e){
e.printStackTrace()
}finally{
if(server!=null) {
try{
server.close()
}catch(Exception ex){}
}
}
}
}
class SocketHandler extends Thread {
private Socket socket
public SocketHandler(Socket socket) {
this.socket=socket
}
public void run() {
try{
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(
new java.util.Date().
toLocaleString())
out.close()
}catch(Exception e){
e.printStackTrace()
}
}
}
客户端:
package com.briup
import java.io.*
import java.net.*
public class FtpClient {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("Usage:java FtpClient file_path")
System.exit(0)
}
File file=new File(args[0])
if(!file.exists()||!file.canRead()) {
System.out.println(args[0]+" doesn't exist or can not read.")
System.exit(0)
}
Socket socket=null
try{
socket=new Socket(args[1],Integer.parseInt(args[2]))
BufferedInputStream in=new BufferedInputStream(
new FileInputStream(file)
)
BufferedOutputStream out=new BufferedOutputStream(
socket.getOutputStream()
)
byte[] buffer=new byte[1024*8]
int i=-1
while((i=in.read(buffer))!=-1) {
out.write(buffer,0,i)
}
System.out.println(socket.getInetAddress().getHostAddress()+" send file over.")
in.close()
out.close()
}catch(Exception e){
e.printStackTrace()
}finally{
if(socket!=null) {
try{
socket.close()
}catch(Exception ex){}
}
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)