//将代码写出来吧!!!!
/*
上传文本文件。
原理:
其实就是将本地的文件数据通过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();
}
}
}