fork(1) download
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Client {
  5. public static void main(String[] args) {
  6. try {
  7. System.out.println("簡易檔案傳送...");
  8.  
  9. String remoteHost = args[0];
  10. int port = Integer.parseInt(args[1]);
  11. File file = new File(args[2]);
  12.  
  13. System.out.printf("遠端主機: %s%n", remoteHost);
  14. System.out.printf("遠端主機連接埠: %d%n", port);
  15. System.out.printf("傳送檔案: %s%n", file.getName());
  16.  
  17. Socket skt = new Socket(remoteHost, port);
  18.  
  19. System.out.println("連線成功!嘗試傳送檔案....");
  20.  
  21. PrintStream printStream =
  22. new PrintStream(skt.getOutputStream());
  23. printStream.println(file.getName());
  24.  
  25. System.out.print("OK! 傳送檔案....");
  26.  
  27. BufferedInputStream inputStream =
  28. new FileInputStream(file));
  29.  
  30. int readin;
  31. while((readin = inputStream.read()) != -1) {
  32. printStream.write(readin);
  33. Thread.yield();
  34. }
  35.  
  36. printStream.flush();
  37. printStream.close();
  38. inputStream.close();
  39.  
  40. skt.close();
  41.  
  42. System.out.println("\n檔案傳送完畢!");
  43. }
  44. catch(Exception e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class Client is public, should be declared in a file named Client.java
public class Client {
       ^
1 error
stdout
Standard output is empty