客户端:
package ch2;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.FlowLayout;
import
import
import
import
import
import
import
import
import
import
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TCPClient extends JFrame {
JTextArea jta;
JButton jb;
public
TCPClient(){
jta=new JTextArea(20,20);
jb=new JButton("查找");
jb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
deal();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
this.setLayout(new FlowLayout());
this.add(jb);
this.add(jta);
this.setBounds(200, 100, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public File
dealaa(){
File fileFlag = new File("");
String filename="";
JFileChooser j1 = new JFileChooser();
int n = j1.showOpenDialog(null);
filename = j1.getSelectedFile().toString();
System.out.println(filename);
if (n == JFileChooser.APPROVE_OPTION) {
System.out.println(filename);
fileFlag = new File(filename);
}
return fileFlag;
}
public static void main(String[] args) throws Exception, IOException {
TCPClient tcp=new TCPClient();
}
public void deal() throws UnknownHostException, IOException, InterruptedException{
Socket socket=new Socket("127.0.0.1",887);
System.out.println("连接上服务器");
OutputStream os = socket.getOutputStream();
PrintWriter writer = new PrintWriter(os, true);
File file=dealaa();
BufferedReader br=new BufferedReader(new FileReader(file));
long fileSize = file.length();
String fileName = file.getName();
writer.println(fileSize);
writer.println(fileName);
final char[] buffer = new char[1024];
int len = 0;
while((len = br.read(buffer,0,buffer.length)) != -1) {
writer.write(buffer, 0, len);
writer.flush();
}
socket.shutdownOutput();
//
writer.close();
//
jta.append(""+socket.isClosed());
InputStreamReader reader=new InputStreamReader(socket.getInputStream());
BufferedReader bra=new BufferedReader(reader);
String response=bra.readLine();
jta.append(response+"/n");
//
bra.close();
//
jta.append(""+socket.isClosed());
br.close();
//
jta.append(""+socket.isClosed());
//
writer.close();
//
jta.append(""+socket.isClosed());
os.close();
Thread.sleep(1000);
//
jta.append(""+socket.isClosed());
//
System.out.println();
/*
InputStreamReader reader=new InputStreamReader(socket.getInputStream());
BufferedReader bra=new BufferedReader(reader);
String response=bra.readLine();
jta.append(response+"/n");
bra.close();*/
socket.close();
}
}
服务器端:
package ch2;
import java.awt.FlowLayout;
import
import
import
import
import
import
import
import
import
import javax.swing.*;
public class TCPServer extends JFrame{
public static void main(String[]args) throws Exception{
TCPServer tcp=new TCPServer();
tcp.deal();
}
public
void deal() throws Exception{
ServerSocket server=new ServerSocket(887);
Socket s=server.accept();
System.out.println("客户端连接进来了");
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
PrintWriter pw = new PrintWriter(s.getOutputStream());
System.out.println("文件开始上传");
String fileSizeStr = br.readLine();
System.out.println("文件长度"+fileSizeStr);
long fileSize = Long.valueOf(fileSizeStr).longValue();
String fileName = br.readLine();
System.out.println("文件名称"+fileName);
StringBuilder filePath = new StringBuilder();
filePath.append("D://file");
File fileCatalog = new File(filePath.toString());
if(!fileCatalog.exists())
fileCatalog.mkdirs();
filePath.append("//").append(fileName);
System.out.println(filePath);
File file = new File(filePath.toString());
FileWriter fos = new FileWriter(file);
final char[] buffer = new char[1024];
System.out.println("开始接收文件");
int len = 0;
while((len = br.read(buffer,0,buffer.length))!= -1) {
//System.out.println("hahahhahahha");
fos.write(buffer, 0, len);
fos.flush();
}
System.out.println("文件上传完毕");
pw.write("文件上传完毕");
pw.flush();
pw.close();
fos.close();
is.close();
s.close();
server.close();
}
}