本帖最后由 刘克方 于 2012-5-18 20:44 编辑
//客户端
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream.GetField;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
//要上传的文件
if(args.length==0){
System.out.println("请输入您要上传的文件路径");
return;
}
File file=new File(args[0]);
uploadFile(file);
}
protected static void uploadFile(File filepath) {
BufferedInputStream buffer=null;
FileInputStream filein=null;
OutputStream out=null;
Socket socket=null;
try {
socket=new Socket("127.0.0.1",7789);
out=socket.getOutputStream();
filein=new FileInputStream(filepath);
buffer=new BufferedInputStream(filein);
//发送文件名
out.write(filepath.getName().getBytes());
byte[] byt=new byte[1024];
int len=0;
//让线程睡眠一秒,等文件名发送以后,再继续发送文件内容
Thread.sleep(1000);
//发送文件内容
while((len=buffer.read(byt))!=-1){
out.write(byt,0,len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(buffer!=null){
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(filein!=null){
try {
filein.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//服务器端
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
final ServerSocket server=new ServerSocket(7789);
while(true){
System.out.println("等待连接");
Socket socket=server.accept();
System.out.println("某客户端已连接连接");
//文件保存地址
File savepath=new File("D://");
startUpload(socket,savepath);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void startUpload(final Socket socket,final File savePath) throws IOException {
//开启一个新线程进行上传
new Thread(new Runnable() {
public void run() {
InputStream in=null;
FileOutputStream out=null;
BufferedOutputStream buffer=null;
try {
in=socket.getInputStream();
//获取文件名
byte[] namebuffer=new byte[100];
int namelen=in.read(namebuffer);
String fileName=new String(namebuffer,0,namelen);
System.out.println(fileName);
//文件保存路径
out=new FileOutputStream(savePath+"/"+fileName);
buffer=new BufferedOutputStream(out);
//获取文件内容
byte[] byt=new byte[1024];
int len=0;
while((len=in.read(byt))!=-1){
System.out.println(new String(byt,0,len));
buffer.write(byt,0,len);
}
System.out.println("文件上传成功,请到"+savePath+"目录下查看");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(buffer!=null){
try {
buffer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}).start();
}
}
|