本帖最后由 xiaochen33520 于 2014-4-16 10:59 编辑
客户端:package cn.itcast.demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
public class Test9_Client {
private final static int SIZE=1024;
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//创建tcp发送端
Socket socket = new Socket("127.0.0.1",10011);
//要上传的文件
File file = new File("1.mp3");
FileOutputStream fos = new FileOutputStream("config.properties");
String pathName = file.getAbsolutePath();
String suffixName = pathName.substring(pathName.lastIndexOf("."));
Properties prop = new Properties();
prop.setProperty("suffixName", suffixName);
prop.store(fos, "上传数据的后缀名");
fos.close();
//创建一个输入流关联一个文件
FileInputStream fis = new FileInputStream(file);
//获取客户端的输出流
OutputStream out = socket.getOutputStream();
int len = -1;
byte [] buf = new byte[SIZE];
//向客户端发送数据
while((len = fis.read(buf))!=-1){
out.write(buf, 0, len);
}
//提醒服务端数据发送完毕
socket.shutdownOutput();
//关闭流
fis.close();
//获取一个输入流
InputStream input = socket.getInputStream();
int lenIn = -1;
byte [] bufIn = new byte[SIZE];
//读取服务端发回来的数据
while((lenIn = input.read(bufIn))!=-1){
System.out.print(new String(bufIn,0,lenIn));
}
//关闭客户端
socket.close();
}
}
服务端:
package cn.itcast.demo;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Test9_Server {
public static void main(String[] args) throws IOException{//为方便阅读异常直接抛出
// TODO Auto-generated method stub
ServerSocket serverSocket = new ServerSocket(10011);
Socket socket = null;
//服务端,通过线程的方式对各个客户端连接服务端进行单独管理与服务
while(true){
socket = serverSocket.accept();
new Thread(new Test9_ServerTask(socket)).start();
}
}
}
服务端任务
package cn.itcast.demo;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Properties;
public class Test9_ServerTask implements Runnable{
private static final int SIZE = 1024;
private Socket socket = null;
public Test9_ServerTask(Socket socket){
this.socket=socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
//获取连接服务端的客户端Ip
String ip = socket.getInetAddress().getHostAddress();
InputStream input = null;
FileOutputStream fos = null;
int len = -1;
byte [] buf = new byte[SIZE];
OutputStream out = null;
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("config.properties");
prop.load(fis);
String suffixName = prop.getProperty("suffixName"); //数据为什么读不出来?:dizzy:
fis.close();
input = socket.getInputStream();
fos = new FileOutputStream(ip+suffixName );
//读取客户端发送过来的数据,并向指定位置输出数据存储
while((len = input.read(buf))!=-1){
fos.write(buf, 0, len);
}
//数据发送完毕后提醒客户端上传成功
out = socket.getOutputStream();
out.write("上传成功".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关资源
fos.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|