求求高手解答下吧.

[复制链接]
查看11 | 回复9 | 2005-7-2 01:02:30 | 显示全部楼层 |阅读模式
这的人再不解答我就不学java了。.问老师老师也不解答.那不会怎么继续学啊?居然还是大学老师..问谁都一样..都不说..
看到这很多人试下吧

可以的话说给个简单例子吧

我想知道怎么用网络的什么socket传送文件啊..就是那个
File from_name=new File(from);
File to_name=new File(to);
FileInputStream fin=new FileInputStream(from_name);
FileOutputStream fout=new FileOutputStream(to_name);
byte[] temp=new byte[4096];
int byte_read;
while((byte_read=fin.read(temp))!=-1){
fout.write(temp,0,byte_read);
}
fin.close();
fout.close();
就是这个东西怎么建立在网络什么serversocket和socket上呢.怎么发送和怎么接收啊...给个例子可以吗???




回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
ServerSample:
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSample {
/**
* @param args
*/
public static void main(String[] args) {
ServerSocket server = null;
Socket clientConnection = null;
InputStream inStream = null;
FileOutputStream outStream = null;

try {

// listen on local port 10000

server = new ServerSocket(10000);

// wait for client connection

clientConnection = server.accept();



// get the data from client socket

inStream = clientConnection.getInputStream();



// init the out put file

outStream = new FileOutputStream("D:\\Server.txt&quot

;



// read the data from client and write to the output file

byte[] dataBuf = new byte[1024];

int len = inStream.read(dataBuf);

while(len != -1) {

outStream.write(dataBuf, 0, len);

len = inStream.read(dataBuf);

}

// close stream and socket

outStream.close();

inStream.close();

clientConnection.close();

server.close();
} catch (Exception e) {

e.printStackTrace();



if (outStream != null) {

try {

outStream.close();

} catch (Exception ee) {



}

}



if (inStream != null) {

try {

inStream.close();

} catch (Exception ee) {



}

}



if (clientConnection != null) {

try {

clientConnection.close();

} catch (Exception ee) {



}

}



if (server != null) {

try {

server.close();

} catch (Exception ee) {



}

}


}
}
}
=================================
client sample
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientSample {
/**
* @param args
*/
public static void main(String[] args) {
OutputStream outStream = null;
FileInputStream inStream = null;
Socket socket = null;

try {

// init client socket and connect to the localhost by port 10000

InetAddress address = InetAddress.getLocalHost();

socket = new Socket(address, 10000);



// get the client output stream

outStream = socket.getOutputStream();



// read d:\\client.txt and write its content to server

inStream = new FileInputStream("D:\\client.txt&quot

;

byte[] dataBuf = new byte[1024];

int len = inStream.read(dataBuf);

while (len != -1) {

outStream.write(dataBuf, 0, len);

len = inStream.read(dataBuf);

}



inStream.close();

outStream.close();

socket.close();




} catch (Exception e) {

e.printStackTrace();

if (inStream != null) {

try {

inStream.close();

} catch (Exception ee) {



}

}

if (outStream != null) {

try {

outStream.close();

} catch (Exception ee) {



}

}

if (socket != null) {

try {

socket.close();

} catch (Exception ee) {



}

}
}
}
}
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
不明白
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
爱死这个论坛了..我先复制来看先.成功了的话我就谢谢你...
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
不成功 人家能發嘛?
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
嘿嘿,学习
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
能用多线程再写一遍吗,,,,谢谢!!!
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
shadows_79 這個人很牛 。佩服·!
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
改为:
InetAddress address = InetAddress.getByName("A.B.C.D&quot

;A.B.C.D为服务器IP或域名。
回复

使用道具 举报

千问 | 2005-7-2 01:02:30 | 显示全部楼层
哈哈。楼主真年轻啊。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行