TCP之上传文本与图片

Java文件上传与图片传输

//将代码写出来吧!!!!
/*
上传文本文件。


原理:
其实就是将本地的文件数据通过socket流,发送到了服务端。服务端对这些数据进行文件存储




*/


import java.io.*;
import java.net.*;
class  UploadClient
{
	public static void main(String[] args)throws Exception 
	{
		Socket s =new Socket("192.168.1.253",9005);


		//读取要上传的本地文本文件,为了提高效率,使用了缓冲区。
		BufferedReader bufr = new BufferedReader(new FileReader("UdpDemo.java"));


		PrintWriter out = new PrintWriter(s.getOutputStream(),true);


		String line = null;


		while((line=bufr.readLine())!=null)
		{
			out.println(line.toUpperCase());
		}


		//out.println("");
			
		s.shutdownOutput();


		//读取服务端发挥的上传成功信息。
		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String info = bufIn.readLine();
		System.out.println(info);


		bufr.close();


		s.close();




	}
}
class  UploadServer
{
	public static void main(String[] args) throws Exception
	{


		ServerSocket ss = new ServerSocket(9005);


		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"....connected");


		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));


		PrintWriter pw = new PrintWriter(new FileWriter("server.txt"),true);


		String line = null;


		while((line=bufIn.readLine())!=null)
		{
			//if("over".equals(line))
				//break;
			pw.println(line);
		}


		PrintWriter out =new PrintWriter(s.getOutputStream(),true);
		out.println("上传成功---");


		pw.close();


		s.close();


		ss.close();
	}
}


//首先写了一个发送端:
package com.csdn.File;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


public class JpgSend {


	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		if(args.length==0){
			System.out.println("请输入一个jpg图片");
			return;
		}
		File f= new File(args[0]);
		if(!(f.exists() && f.isFile() && f.getName().endsWith(".jpg"))){
			System.out.println("选择文件错误,请重输");
			return;
		}
          Socket s=new Socket("192.168.49.110",9009);
          
          FileInputStream fis = new FileInputStream(f);
          OutputStream output =s.getOutputStream();
          
         byte[] b=new byte[1024];
         int len=0;
         while((len=fis.read(b))!=-1){
        	 output.write(b,0,len);
         }
         s.shutdownOutput();
         InputStream input = s.getInputStream();
         byte[] bt= new byte[1024];
         int num=input.read(bt);
         String str=new String(bt,0,num);
         
         System.out.println(str);
         
         fis.close();
         s.close();
	}


}

//做一个线程,接收文件
package com.csdn.File;


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


public class JpgThread implements Runnable{
  private Socket s;
  public JpgThread(Socket s){
	  this.s=s;
  }
 	@Override
	public void run() {
		// TODO Auto-generated method stub
 		int count=1;
 		String ip =s.getInetAddress().getHostAddress();
 		try{
 		  InputStream input =s.getInputStream();
          File f=new File("d:\\"+"("+count+").jpg");
          while(f.exists()){
             f=new File("d:\\"+"("+(count++)+").jpg");
          }
          FileOutputStream fos=new FileOutputStream(f);
          
          byte[] b=new byte[1024];
          int len=0;
          while((len=input.read(b))!=-1){
         	 fos.write(b,0,len);
          }
          OutputStream out=s.getOutputStream();
          out.write("接收成功".getBytes());
          
          fos.close();
          s.close();
          }catch(Exception e){
        	  System.out.println(e.toString());
          }
         
	}


}


调用线程:
package com.csdn.File;


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


public class JpgReceive {


	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
         ServerSocket ss=new ServerSocket(9009);
	 while(true){
         Socket s=ss.accept();
          String ip=ss.getInetAddress().getHostAddress();
          System.out.println(ip+">>正在发送>>");
          new Thread(new JpgThread(s)).start();
    
	}
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值