- 这是代码:
- 客户端:
- public class Client {
- static Socket client = null;
- static final int PORT = 1234;
- static final String IP = "127.0.0.1";
- static final String PATH = "savefile.xml";
- // static InputStream inputStream = null;
- static DataInputStream inputStream = null;
- static OutputStream outputStream = null;
- public static void main(String[] A) {
- init();
- receiveData1();
- }
- public static void init() {
- try {
- client = new Socket(IP, PORT);
- outputStream = new FileOutputStream(PATH);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- System.out.println("未知的服务器.");
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("输入输出服务器出现异常.");
- }
- }
- public static void receiveData1() {
- try {
- // inputStream = client.getInputStream();
- inputStream= new DataInputStream(new BufferedInputStream(client.getInputStream()));
- long startTime= System.currentTimeMillis();
- String fileName= inputStream.readUTF();
- long length= inputStream.readLong();
- System.out.println("文件名以及长度:"+fileName+ " :"+ length);
- StringBuffer content= new StringBuffer();
- byte[] temp= new byte[102400];
- int numFlag= inputStream.read(temp);
- System.out.println("numFlag:"+numFlag);
- while(numFlag!= -1) {
- content.append(temp);
- numFlag= inputStream.read(temp);
- System.out.println("numFlag:"+numFlag);
- }
- System.out.println("接受的总时间是:"+(System.currentTimeMillis()- startTime));
- SAVEINFILE(content);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("接收服务器返回数据时出现异常.");
- }
- }
- public static void receiveData2() {
- try {
- // inputStream = client.getInputStream();
- byte[] temp= new byte[1000000];
- StringBuffer content= new StringBuffer();
- int numFlag= inputStream.read(temp);
- while(numFlag!= -1) {
- content.append(temp);
- numFlag= inputStream.read(temp);
- }
- System.out.println(content.toString());
- SAVEINFILE(content);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("接收服务器返回数据时出现异常.");
- }
- }
- public static final void SAVEINFILE(final Object content) {
- System.out.println("content的类是:"+content.getClass());
- if(content instanceof StringBuffer) {
- try {
- System.out.println(content);
- outputStream.write(content.toString().getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("存储到文件中出现异常.");
- }
- }else{
- if(content instanceof String) {
- try {
- outputStream.write(((String) content).getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("存储到文件中出现异常.");
- }
- }else{
- System.out.println("传入的参数没有定义.");
- }
- }
- CLOSE();
- }
- public static final void CLOSE() {
- try {
- outputStream.flush();
- inputStream.close();
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("关闭输入输出流的时候出现异常.");
- }
- }
- }
- 服务端:
- public class Server {
- static ServerSocket server= null;
- // static OutputStream outputStream= null;
- static DataOutputStream outputStream= null;
- static InputStream inputStream= null;
- static String fileName= "";
- static long length= 0;
- static final int PORT= 1234;
- static Socket socket= null;
- static final String PATH= "E:/download/logs/test/SystemOut.log";
- public static void main(String[] A) {
- init();
- sendFile();
- }
- public static void init() {
- try {
- server= new ServerSocket(PORT);
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("服务器启动出现异常");
- }
- System.out.println("服务器已启动.");
- }
- public static final void saveFile(final String path, Object content) {
- /*try {
- outputStream= new FileOutputStream(path);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.out.println("读取文件时出现异常.");
- }*/
- }
- public static void readFile(final String path) {
- try {
- File file= new File(path);
- fileName= file.getName();
- length= file.length();
- inputStream= new FileInputStream(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.out.println("存储文件时出现异常.");
- }
- }
- public static void sendFile() {
- while(true) {
- try {
- socket = server.accept();
- readFile(PATH);
- // outputStream= socket.getOutputStream();
- outputStream= new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("等待客户端连接时出现错误.");
- }
- System.out.println("开始发送数据.");
- try {
- int i= 2;
- outputStream.writeUTF(fileName);
- outputStream.flush();
- outputStream.writeLong(length);
- outputStream.flush();
- byte[] temp= new byte[1024000];
- int n= inputStream.read(temp);
- System.out.println("本次发送"+n+"个字节.");
- while(n!= -1) {
- System.out.println("本次发送了"+n+"个字节;"+"第"+i);
- outputStream.write(temp);
- n= inputStream.read(temp);
- i++;
- }
- CLOSE_CONNECTION();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("发送数据时出现错误.");
- }
- }
- }
- public static void receiveFile() {
- try {
- inputStream= socket.getInputStream();
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("接收文件时出现异常.");
- }
- }
- public static final void CLOSE_CONNECTION() {
- try {
- outputStream.flush();
- outputStream.close();
- socket.close();
- // server.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/160120148538.html
来源: http://www.codesnippet.cn/detail/160120148538.html