`
阿尔萨斯
  • 浏览: 4398451 次
社区版块
存档分类
最新评论

赵雅智_java 网络编程(3)之上传文件和图片

 
阅读更多

上传文件和图片格式

客户端:

1.建立socket客户端,连接服务,要有目标ip和端口
2.读取本地文件的内容
3.输出到服务器端:通过socket的输出流
4.传输结束:socket的shutdownOutput()
5.接受服务端传过来的上传成功提示:通过socket的输入流
6.关闭资源。

服务器端

1.建立ServerSocket服务,监听一个端口
2.通过accept方法产生一个socket对象,与客户端建立通道。
3.通过socket的输入流读取客户端穿过来信息。
4.将获取到的信息保存到文件。
5.向客户端发出上传成功提示。
6.关闭资源

实例1:上传文件:

客户端:

package net.csdn.web;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpTextClient {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		// TODO Auto-generated method stub

		Socket s = new Socket("192.168.49.59",9009);
		BufferedReader br=new BufferedReader(new FileReader("d:\\1.txt"));
		PrintWriter pwout = new PrintWriter(s.getOutputStream(),true);
		String line = null;
		while((line = br.readLine())!=null){
			pwout.println(line);
		}
		s.shutdownOutput();
		BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
		System.out.println(brin.readLine());
		br.close();
		s.close();
	}

}


服务端:

package net.csdn.web;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpTextServer {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		ServerSocket ss = new ServerSocket(9009);
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"....connection");
		BufferedReader brin = new BufferedReader(new InputStreamReader(s.getInputStream()));
		PrintWriter pw=new PrintWriter(new FileWriter("e:\\server.txt"),true);
		
		String line=null;
		
		while((line=brin.readLine())!=null){
			pw.println(line);
		}
		PrintWriter pwout=new PrintWriter(s.getOutputStream(),true);
		pwout.println("上传成功");
		
		pw.close();
		s.close();
		ss.close();
	}
}


实例二:上传图片

客户端:

package net.csdn.web;

import java.net.*;
import java.io.*;

public class JpgClient {

	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		
		Socket s=new Socket("192.168.49.59",9009);
		
		FileInputStream fis=new FileInputStream("d:\\2.jpg");
		
		OutputStream out=s.getOutputStream();
		
		byte[] buf=new byte[1024];
		
		int len=0;
		
		while((len=fis.read(buf))!=-1){
			out.write(buf, 0, len);
		}
		
		s.shutdownOutput();
		
		InputStream in=s.getInputStream();
		byte[] b=new byte[1024];
		int num=in.read(b);
		System.out.println(new String(b,0,num));
		
		fis.close();
		
		s.close();
		
		
		
		

	}

}


服务端:

package net.csdn.web;

import java.net.*;
import java.io.*;

public class JpgServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		
		ServerSocket ss=new ServerSocket(9009);
		
		Socket s=ss.accept();
		
		InputStream in=s.getInputStream();
		
		FileOutputStream fos=new FileOutputStream("e:\\1.jpg");
		
		byte[] buf=new byte[1024];
		
		int len=0;
		
		while((len=in.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		
		OutputStream out=s.getOutputStream();
		out.write("上传成功了".getBytes());
		
		fos.close();
		
		s.close();
		ss.close();
		
	}

}


实例三:

客户端:

package net.csdn.web;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class TcpClient2 {
	public static void main(String[] args) throws Exception {
		Socket s=new Socket("192.168.49.59",9009);
		//获取键盘录入
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		//数据输出给服务器端
		OutputStream out=s.getOutputStream();
		
		BufferedWriter bwout=new BufferedWriter(new OutputStreamWriter(out));
		//获取服务器端返回的数据
		BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
		
		String line=null;
		
		while((line=br.readLine())!=null){
			if(line.equals("over"))
				break;
			
			bwout.write(line);
			bwout.newLine();
			bwout.flush();
			
			String str=brin.readLine();
			System.out.println("server:"+str);			
		}
		br.close();
		s.close();
	}
}


服务端:加入多线程内容

package net.csdn.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class JpgServer2 {

	/**
	 * @param args
	 */
public static void main(String[] args) throws Exception{
		
		ServerSocket ss=new ServerSocket(9009);
		
		while(true){
			
			Socket s=ss.accept();
			
			new Thread(new JpgThread(s)).start();
		}
		
		
		
		
		//ss.close();
		
	}

}


class JpgThread implements Runnable{
	
	private Socket s;
	
	public JpgThread(Socket s){
		this.s=s;
	}

	@Override
	public void run() {
		int count=1;
		try{
			String ip=s.getInetAddress().getHostAddress();
			
			System.out.println(ip+"---connection");
			InputStream in=s.getInputStream();
			
			File dir=new File("d:\\picture");
			
			File f=new File(dir,ip+"("+count+").jpg");
			
			while(f.exists())
				f=new  File(dir,ip+"("+(count++)+").jpg");
			
			FileOutputStream fos=new FileOutputStream(f);
			
			byte[] buf=new byte[1024];
			
			int len=0;
			
			while((len=in.read(buf))!=-1){
				fos.write(buf,0,len);
			}
			
			OutputStream out=s.getOutputStream();
			out.write("上传成功了".getBytes());
			
			fos.close();
			
			s.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics