java實現文件傳輸
1
import java.awt.*;
2
import java.awt.event.*;
3
import javax.swing.*;
4
import javax.swing.event.*;
5
import java.io.*;
6
import java.net.*;
7
import javax.swing.filechooser.FileFilter;
8
9
public class Chooser extends JLabel
10

{
11
private JButton openButton,saveButton;
12
JFileChooser fc;
13
String fileName;
14
int result;
15
Chooser()
16

{
17
setLayout(new GridLayout());
18
JButton openButton=new JButton("Open");
19
openButton.addActionListener(new openFile());
20
JButton saveButton=new JButton("Save");
21
saveButton.addActionListener(new saveFile());
22
add(openButton);
23
add(saveButton);
24
}
25
26
class openFile implements ActionListener
27

{
28
public void actionPerformed(ActionEvent e)
29

{
30
fc = new JFileChooser();
31
result = fc.showOpenDialog(Chooser.this);
32
File file = fc.getSelectedFile();
33
if(file!=null && result==JFileChooser.APPROVE_OPTION)
34

{
35
fileName = file.getAbsolutePath();
36
System.out.println("You chose to open this file: " +fileName);
37
try
38

{
39
File file1=new File(fileName);
40
FileInputStream fos=new FileInputStream(file1);
41
//創建網絡服務器接受客戶請求
42
ServerSocket ss=new ServerSocket(3108);
43
Socket client=ss.accept();
44
//創建網絡輸出流并提供數據包裝器
45
OutputStream netOut=client.getOutputStream();
46
OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut));
47
//創建文件讀取緩沖區
48
byte[] buf=new byte[2048];
49
int num=fos.read(buf);
50
while(num!=(-1))
51

{ //是否讀完文件
52
doc.write(buf,0,num);//把文件數據寫出網絡緩沖區
53
doc.flush();//刷新緩沖區把數據寫往客戶端
54
num=fos.read(buf);//繼續從文件中讀取數據
55
}
56
fos.close();
57
doc.close();
58
}
59
catch(Exception ex)
60

{
61
System.out.println(ex);
62
}
63
}
64
if(result == JFileChooser.CANCEL_OPTION)
65

{
66
}
67
68
}
69
}
70
class saveFile implements ActionListener
71

{
72
public void actionPerformed(ActionEvent e)
73

{
74
fc = new JFileChooser();
75
result = fc.showSaveDialog(Chooser.this);
76
File file1 = fc.getSelectedFile();
77
fileName = file1.getAbsolutePath();
78
System.out.println("fileName:"+fileName);
79
if (result == JFileChooser.APPROVE_OPTION)
80

{
81
try
82

{
83
File file2=new File(fileName);
84
file2.createNewFile();
85
RandomAccessFile raf=new RandomAccessFile(file2,"rw");
86
// 通過Socket連接文件服務器
87
Socket server=new Socket(InetAddress.getLocalHost(),3108);
88
//創建網絡接受流接受服務器文件數據
89
InputStream netIn=server.getInputStream();
90
InputStream in=new DataInputStream(new BufferedInputStream(netIn));
91
//創建緩沖區緩沖網絡數據
92
byte[] buf=new byte[2048];
93
int num=in.read(buf);
94
System.out.println("in.read(buf)′length="+num);
95
while(num!=(-1))
96

{//是否讀完所有數據
97
raf.write(buf,0,num);//將數據寫往文件
98
raf.skipBytes(num);//順序寫文件字節
99
num=in.read(buf);//繼續從網絡中讀取文件
100
}
101
in.close();
102
raf.close();
103
}
104
catch(Exception ex)
105

{
106
System.out.println(ex);
107
}
108
109
}
110
if(result == JFileChooser.CANCEL_OPTION)
111

{
112
}
113
}
114
}
115
public static void main(String args[])
116

{
117
JFrame f=new JFrame();
118
f.getContentPane().add(new Chooser());
119
f.setSize(250, 110);
120
f.setResizable(false);
121
f.setDefaultCloseOperation(3);
122
f.setVisible(true);
123
}
124
}

2

3

4

5

6

7

8

9

10



11

12

13

14

15

16



17

18

19

20

21

22

23

24

25

26

27



28

29



30

31

32

33

34



35

36

37

38



39

40

41

42

43

44

45

46

47

48

49

50

51



52

53

54

55

56

57

58

59

60



61

62

63

64

65



66

67

68

69

70

71



72

73



74

75

76

77

78

79

80



81

82



83

84

85

86

87

88

89

90

91

92

93

94

95

96



97

98

99

100

101

102

103

104

105



106

107

108

109

110

111



112

113

114

115

116



117

118

119

120

121

122

123

124

posted on 2005-12-20 11:22 船夫 閱讀(3055) 評論(5) 編輯 收藏 所屬分類: java技術