FileUpload包下放了三个类:
OpenAndSave.java
TCPClient .java
TCPServer.java
1、OpenAndSave.java
package FileUpload; import java.awt.*; import javax.swing.*; import java.util.*; import java.io.*; public class OpenAndSave extends JFrame { // 选择打开文件 public String getFile() { String fileName=""; FileDialog fd = new FileDialog(this, "请选择要传给zpc的文件", FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); // 获取此文件对话框选定的文件 if (fd.getFile()!= null) { fileName=fd.getDirectory() + fd.getFile(); System.out.println("已选择打开 " +fileName ); } return fileName; } // 保存文件 public OutputStream saveFile(String fileName) { OutputStream os=null; try { FileDialog fd = new FileDialog(this, "保存文件", FileDialog.SAVE); // 对话框打开时显示默认的目录 fd.setDirectory("C:\\"); fd.setFile(fileName); fd.setVisible(true); if (fd.getFile() != null) { File myFile = new File(fd.getDirectory(), fd.getFile()); os = new FileOutputStream(myFile); } } catch (IOException ioe) { JOptionPane.showMessageDialog(this, "文件保存发生错误,已停止"); } return os; } }
2、TCPServer.java
/* * Author:zpc * Time:2014-5-1 */ package FileUpload; import java.io.*; import java.net.*; import java.util.Random; import javax.swing.*; //每接受一个客户端请求单开一个线程处理 class UploadProcess implements Runnable { private Socket s; public UploadProcess(Socket s) { this.s = s; } public void run() { OutputStream fos = null; try { DataInputStream fis = new DataInputStream(s.getInputStream());// 和复制文件不同的是网络上传文件是从Socket中获取的I/O流对象 String fileName = fis.readUTF(); DataOutputStream fos1= new DataOutputStream(s.getOutputStream());//写回信息; if ((JOptionPane.showOptionDialog(null, "您想接受文件" + fileName + "吗?", "消息", JOptionPane.YES_NO_OPTION, 0, null, null, null)) == 0) { System.out.println("我已选择接受文件!"); String message1="服务器已接收!"; fos1.writeUTF(message1); fos = new OpenAndSave().saveFile(fileName); int len = 0; byte[] bytes = new byte[1024]; if (fis != null) { while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); } } JOptionPane.showMessageDialog(null, "文件保存成功!"); if (fis != null) fis.close(); if (fos != null) fos.close(); } else { System.out.println("我选择了拒绝接受!"); String message2="提示:对方已拒绝接受!"; fos1.writeUTF(message2); } } catch (Exception e) { System.out.println("接受文件失败!"); } } } public class TCPServer { public static void main(String[] args) throws IOException { // 建立服务器端的socket对象ServerSocket ServerSocket ss = new ServerSocket(10001); while (true) { Socket s = ss.accept(); // 每个客户端的请求创建一个Socket对象,单开一个线程处理 new Thread(new UploadProcess(s)).start(); } } }
3、TCPClient .java
/* * Author:zpc * Function:上传文件给用户zpc * Time:2014-5-1 */ package FileUpload; import java.io.*; import java.net.*; public class TCPClient { public static void main(String[] args) throws UnknownHostException{ // 进行文件的读取.数据源 OpenAndSave open = new OpenAndSave(); String fileName = open.getFile(); File myFile = new File(fileName); InputStream in = null; try { in = new FileInputStream(myFile); } catch (FileNotFoundException e1) { System.out.println("未选中文件!"); } // 数据目的地是另一台主机,通过Socket获取I/O流 if (in != null) { Socket s = null; try { s = new Socket("127.0.0.1", 10001);//这里写好目标机器IP地址和任意一个开放的未被其它程序占用的端口号 DataOutputStream os = new DataOutputStream(s.getOutputStream()); os.writeUTF(fileName); byte[] b = new byte[1024]; int len = 0; while ((len = in.read(b)) != -1) { os.write(b, 0, len); } //System.out.println("message"); DataInputStream fis = new DataInputStream(s.getInputStream()); String message = fis.readUTF(); System.out.println(message); if (os != null) os.close(); if (in != null) in.close(); } catch (Exception e) { System.out.println("貌似zpc不在线,稍后再传!!"); } } else { System.out.println("文件读取失败(in = null)!"); } } }