java工程上传文件_[Java] 文件上传下载项目(详细注释)

1 /*

2 FTClient.java3 */

4

5 import java.net.*;6 import java.io.*;7 import java.util.*;8

9 public classFTClient {10

11 Socket s = null;12 DataInputStream dis = null;13 DataOutputStream dos = null;14

15 String[] args = null;16

17 public void start(String server, intport) throws Exception {18 s = establish(server, port);//new一个socket

19 dis = newDataInputStream(s.getInputStream());20 dos = newDataOutputStream(s.getOutputStream());21

22 if (args[1].equals("get")) {//获取文件

23 dos.writeInt(3);//执行command为3的操作,对应FTProtocol中的case 3

24 dos.flush();//清空缓冲区数据

25 int files = dis.readInt();//得到目录中文件数量

26 if (files == 0) {//目录中文件为空

27 System.out.println("no files available on the FTServer");28 s.close();//关闭套接字

29 System.exit(-1);//退出程序

30 }31 String[] filenames = newString[files];32 for (int i = 0; i < files; i++) {33 filenames[i] = dis.readUTF();//读取FTProtocol中通过writeUTF(files[i])发来的文件名

34 System.out.println(i + 1 + "\t\t" + filenames[i]);//输出序号和文件名

35 }36

37 System.out.print("please input your choice:");38

39 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));40 String c = br.readLine();//读取输入的命令选项

41

42 if (c.equalsIgnoreCase("q")) {//若输入'Q'或'q'则退出程序

43 s.close();//关闭套接字

44 System.exit(0);//退出程序

45 }46

47 if (c.equalsIgnoreCase("a")) {//若输入'A'或'a'则下载全部文件

48 for (int i = 0; i < filenames.length; i++) {49 System.out.println("filenames[i] =" +filenames[i]);50 download(filenames[i]);//循环下载每一个文件

51 }52 s.close();53 System.exit(0);54 }55 int choice = 0;56 try{57 choice = Integer.parseInt(c);//将字符串转换成整形

58 } catch(NumberFormatException e) {59 System.out.println("your input is wrong");60 s.close();61 System.exit(-2);62 }63

64 //输入文件对应序号则下载该文件

65 if (choice >= 1 && choice <=filenames.length) {66 download(filenames[choice - 1]);67 } else{68 System.out.println("your input is wrong");69 s.close();70 System.exit(-5);71 }72 s.close();73 System.exit(0);74 } else if (args[1].equals("put")) {//上传文件

75 File f = new File("C:/_Client/" + args[2]);//args[2]为文件名

76 if(f.isFile()) {77 upload("C:/_Client/" + args[2]);78 } else if (f.isDirectory()) {//如果上传的是一个目录

79 String[] filenames = f.list();//将目录中所有文件存入filenames数组

80 if (filenames.length == 0) {81 s.close();82 System.out.println("no files available in the directory");83 System.exit(-8);84 }85 for (int i = 0; i < filenames.length; i++) {//将目录中的文件列表显示

86 System.out.println(i + 1 + "\t\t" +filenames[i]);87 }88 System.out.print("please input your choice:");89 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));90 String c =br.readLine();91

92 if (c.equalsIgnoreCase("q")) {//若输入'Q'或'q'则退出程序

93 s.close();//关闭套接字

94 System.exit(0);//退出程序

95 }96

97 if (c.equalsIgnoreCase("a")) {//若输入'A'或'a'则上传全部文件

98 for (int i = 0; i < filenames.length; i++) {99 String dir = f.getCanonicalPath();//获取文件路径

100 String tf = null;101 if (dir.endsWith(File.separator)) {//如果以分隔符结尾,则直接加上文件名

102 tf = dir +filenames[i];103 } else {//否则加上分隔符再加文件名

104 tf = dir + File.separator +filenames[i];105 }106 //此时tf为文件的绝对路径

107 if(new File(tf).isDirectory()) continue;108 upload(tf);109 }110 s.close();111 System.exit(0);112 }113 int choice = 0;114 try{115 choice =Integer.parseInt(c);116 } catch(NumberFormatException e) {117 System.out.println("your input is wrong");118 s.close();119 System.exit(-2);120 }121 //上传第choice个文件

122 if (choice >= 1 && choice <=filenames.length) {123 String dir =f.getCanonicalPath();124 if(dir.endsWith(File.separator)) {125 upload(dir + filenames[choice - 1]);126 } else{127 upload(dir + File.separator + filenames[choice - 1]);128 }129

130 } else{131 System.out.println("your input is wrong");132 s.close();133 System.exit(-5);134 }135

136 } else{137 s.close();138 System.out.println(args[2] + "not exists");139 System.exit(-7);140 }141

142 s.close();143 System.exit(0);144

145 }146

147 }148

149 public Socket establish(String server, intport) {150 try{151 Socket s = newSocket(server, port);152 returns;153 } catch(Exception e) {154 e.printStackTrace();155 return null;156 }157 }158

159 public voidupload(String filename) throws Exception {160

161 File f = newFile(filename);162

163 if (!f.exists() || !f.isFile()) {164 System.out.println("it's wrong, maybe it is not a file or not exists");165 System.exit(-6);166 }167

168 byte[] buffer = new byte[4096];169 int rr = 0;170

171 dos.writeInt(1);//执行FTProtocol中的上传命令

172 dos.writeUTF(f.getName());//将文件名传递给FTProtocol

173 dos.writeLong(f.length());//将文件长度传递给FTProtocol

174 dos.flush();//清空缓存

175

176 FileInputStream fis = newFileInputStream(f);177 BufferedInputStream bis = newBufferedInputStream(fis);178

179 while ((rr = bis.read(buffer)) != -1) {180 dos.write(buffer, 0, rr);181 dos.flush();182 }183

184 bis.close();185 fis.close();186

187 }188

189 public voiddownload(String filename) throws Exception {190 dos.writeInt(2);//执行FTProtocol中的下载命令

191 dos.writeUTF(filename);//将要下载的文件名传递给FTProtocol

192 dos.flush();193

194 //filename = dis.readUTF();

195 long len = dis.readLong();//获得文件长度

196

197 byte[] buffer = new byte[4096];198 long r = 0;199 int rr = 0;200

201 //将先下载后的文件输出到C盘的_Client文件夹

202 FileOutputStream fos = new FileOutputStream("C:/_Client/" +filename);203 BufferedOutputStream bos = newBufferedOutputStream(fos);204

205 while (r = buffer.length) {//若文件未传输的部分大于buffer的长度则每次传输buffer.length的字节

207 rr = dis.read(buffer, 0, buffer.length);208 } else {//将剩余(小于buffer.length)的数据传输

209 rr = dis.read(buffer, 0, (int) (len -r));210 }211 r = r +rr;212 bos.write(buffer, 0, rr);//将下次传输可接收的字节传递给FTProtocol,rr=-1则传输结束

213 }214

215 bos.close();216 fos.close();217

218 System.out.println("download Finished!");219

220 }221

222 public static voidmain(String[] args) throws Exception {223 if(args.length==0) {224 System.out.println("Usage:");225 System.out.println("java FTClient host get");226 System.out.println("java FTClient host put afile");227 System.exit(0);228

229 }230 FTClient ftc = newFTClient();231 ftc.args =args;232 ftc.start(args[0], 4321);//args[0]为host

233 }234 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值