|
package com.renxing.r0043_AllUserThreader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Administrator
*
*多用户服务端
*/
class uploadingPicThread implements Runnable
{
private Socket s;
private int count;
public uploadingPicThread(Socket s,int count)
{
this.s=s;
this.count=count;
}
@Override
public void run()
{
//当运行相关的方法的时候我们就进行相关的操作
//什么操作
//上传图片 ----》 字节
try {
File file=new File("E:\\"+s.getInetAddress().getHostAddress()+"("+(count+"")+")"+".jpg");
if(!file.exists())
{
file.createNewFile();
}
BufferedInputStream bis=new BufferedInputStream(s.getInputStream());
BufferedOutputStream bos2=new BufferedOutputStream(new FileOutputStream(file));
byte[] bys1=new byte[1024*1024];
int num1;
while((num1=bis.read(bys1))!=-1){
bos2.write(bys1);
bos2.flush();
}
bis.close();
bos2.close();
System.out.println("Server end");
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class allSocketDemo
{
public static int count =0;
public static void main(String[] args) throws IOException
{
ServerSocket ss=new ServerSocket(10000);
//服务器监听 10000端口
while(true)
{ Socket s=ss.accept();
new Thread(new uploadingPicThread(s,count++)).start();
}
}
}
package com.renxing.r0043_AllUserThreader;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
//对于 单个客户端来讲
/**
* 1图形界面问题
* 2线程问题
* 3.socket 的代码问题
*/
public class SimpleSocketDemo
{
public Frame f;
public MenuBar mb;
Menu m;
MenuItem mi;
public SimpleSocketDemo(String frameName)
{
f=new Frame(frameName);
mb=new MenuBar();
m=new Menu("操作");
mi=new MenuItem("上传图片");
f.setSize(102,313);
f.setLayout(new FlowLayout());
f.setMenuBar(mb);
mb.add(m);
m.add(mi);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
mi.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==mi)
{
FileDialog fd=new FileDialog(f,"请选择图片",FileDialog.LOAD);
fd.setVisible(true);
String fileName=fd.getFile();
String dir =fd.getDirectory();
String allfileName=dir+fileName;
File file=new File(allfileName);
Socket s=null;
if(fileName.endsWith("jpg"))
{try {
s=new Socket("localhost",10000);
BufferedInputStream bis1=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos1 =new BufferedOutputStream(s.getOutputStream());
byte [] bys=new byte[1024*1024];
int num=0;
while((num=bis1.read(bys))!=-1)
{
bos1.write(bys);
bos1.flush();
}
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally
{
try {
s.shutdownOutput();
System.out.println("centil end");
System.out.println("上传成功!");
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return;
}
System.out.println("上传失败!");
}
}
});
}
public static void main(String[] args)
{
new SimpleSocketDemo ("abc");
}
}
不整理了 直接睡觉!~~~ |
|