//服务端
package Socket;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class Server2User extends Thread{
private Socket socket;
private InputStream is;
private BufferedInputStream bis;
private FileOutputStream fos;
private File file;
private byte name[] = new byte[1024];
private byte transport[] = new byte[1024];
private File path;
public Server2User(Socket socket)
{
this.socket= socket;
}
public void run()
{
try {
is = socket.getInputStream();
bis= new BufferedInputStream(is);
bis.read(name);
String filename = new String(name);
filename = filename.trim();
System.out.println("得到文件名"+filename);
path = this.createFile(filename);
System.out.println(path.getName());
int length;
fos = new FileOutputStream(path);
int i = 0;
while((length=bis.read(transport))!=-1)
{
this.write2file(transport);
i++;
}
this.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void write2file(byte[] transport)
{
try {
fos.write(transport);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private File createFile(String filename)
{
try {
file = new File("f:/test/"+filename);
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
private void close()
{
try {
if(is!=null)
is.close();
if(bis!=null)
bis.close();
if(fos!=null)
fos.close();
if(is!=null)
is.close();
if(socket!=null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//客户端
package Socket;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientSocket_upload {
private Socket socket;
private OutputStream os;
private BufferedOutputStream bos;
private String path;
private FileInputStream fis;
private File file;
public ClientSocket_upload(String path)
{
this.path = path;
}
public void uploadfile(){
try {
//1.创建客户端Socket,指定服务器地址和端口
socket= new Socket("10.104.77.108", 1080);
//2.获取输出流,向服务器端发送信息
os = socket.getOutputStream();//字节输出流
bos = new BufferedOutputStream(os);
file = new File(path);
fis= new FileInputStream(file);
String name = file.getName();
byte[] temp = name.getBytes();
byte[] bname = new byte[1024];
byte[] by = new byte[1024];
for(int i = 0 ;i<=bname.length-1;i++)
{
if(i<=temp.length-1)
{
bname[i] = temp[i];
}
else
{
bname[i]=0;
}
}
bos.write(bname);
while((fis.read(by))!=-1)
{
bos.write(by);
}
fis.close();
bos.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}