import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class IPServer{
/**
* @param args
*/
JButton save=new JButton("save");
ServerSocket ss;
Socket s;
FileOutputStream fos;
FileInputStream fis;
OutputStream os;
DataInputStream ip;
String filename;
byte[] buf=new byte[1024];
public static void main(String[] args) {
// TODO Auto-generated method stub
new IPServer().start();
}
public IPServer() {
}
public void start()
{
try{
byte b[]=new byte[1024];
System.out.print("等待连接......\n");
ss=new ServerSocket(8888);
s=ss.accept();
System.out.print("以链接........\n");
ip=new DataInputStream(s.getInputStream());
fos=new FileOutputStream("D:/1.jpg");
int c=0;
int i=0;
while((c=ip.read(b,0,b.length))>0)
{
i=c+i;
fos.write(b,0,c);
fos.flush();
}
System.out.print("finish\n"+i);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class IPServer{
/**
* @param args
*/
JButton save=new JButton("save");
ServerSocket ss;
Socket s;
FileOutputStream fos;
FileInputStream fis;
OutputStream os;
DataInputStream ip;
String filename;
byte[] buf=new byte[1024];
public static void main(String[] args) {
// TODO Auto-generated method stub
new IPServer().start();
}
public IPServer() {
}
public void start()
{
try{
byte b[]=new byte[1024];
System.out.print("等待连接......\n");
ss=new ServerSocket(8888);
s=ss.accept();
System.out.print("以链接........\n");
ip=new DataInputStream(s.getInputStream());
fos=new FileOutputStream("D:/1.jpg");
int c=0;
int i=0;
while((c=ip.read(b,0,b.length))>0)
{
i=c+i;
fos.write(b,0,c);
fos.flush();
}
System.out.print("finish\n"+i);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
本文介绍了一个简单的Java服务器程序示例,该程序通过TCP连接接收客户端发送的文件,并将其保存到指定路径。服务器使用ServerSocket监听端口8888,并通过DataInputStream读取来自客户端的数据。
38万+

被折叠的 条评论
为什么被折叠?



