java 传送文件

编程入门 行业动态 更新时间:2024-10-15 04:26:51

java 传送<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件"/>

java 传送文件

展开全部

//以前写的一个文件传输的小程序,有客户端和服e5a48de588b63231313335323631343130323136353331333238653938务器端两部分,服务器可//以一直运行,客户端传输完一个后退出,当然你也可以根据你的需要改。

//服务器端可以支持多个客户端同时上传,用到了多线程

/**

* 文件传输,客户端

* @aurth anyx

*/

//package per.anyx.ftp;

import java.*;

import java.io.*;

public class FtpClient{

public static void main(String[] args){

if(args.length != 3){

System.out.println("Usage: FtpClient host_add host_port src_file");

System.exit(0);

}

File file = new File(args[2]);

if(!file.exists() || !file.isFile()){

System.out.println("File \"" + args[2] + "\" does not exist or is not a normal file.");

System.exit(0);

}

Socket s = null;

FileInputStream in = null;

OutputStream out = null;

try{

s = new Socket(args[0], Integer.parseInt(args[1]));

in = new FileInputStream(file);

out = s.getOutputStream();

byte[] buffer = new byte[1024*8];

int len = -1;

System.out.println("File tansfer statr...");

while((len=in.read(buffer)) != -1){

out.write(buffer, 0, len);

}

System.out.println("File tansfer complete...");

}catch(Exception e){

System.out.println("Error: " + e.getMessage());

System.exit(1);

}finally{

try{

if(in != null) in.close();

if(out != null) out.close();

if(s != null) s.close();

}catch(Exception e){}

}

}

}

/**

* 文件传输,服务器端

* @aurth anyx

*/

//package per.anyx.ftp;

import java.*;

import java.io.*;

public class FtpServer{

public static void main(String[] args){

if(args.length != 1){

System.out.println("Usage: FtpServer server_port");

System.exit(0);

}

ServerSocket ss = null;

try{

ss = new ServerSocket(Integer.parseInt(args[0]));

System.out.println("FtpServer start on port ..." + args[0]);

while(true){

Socket s = ss.accept();

new FtpThread(s).start();

System.out.println(s.getInetAddress().getHostAddress() + " connected.");

}

}catch(Exception e){

System.out.println("Error: " + e.getMessage());

}finally{

try{

if(ss != null) ss.close();

}catch(Exception e){}

}

}

}

class FtpThread extends Thread{

Socket s;

long fileName = 0;

public FtpThread(Socket s){

this.s = s;

}

public void run(){

FileOutputStream out = null;

InputStream in = null;

File file = null;

do{

file = new File("" + (fileName++));

}while(file.exists());

try{

out = new FileOutputStream(file);

in = s.getInputStream();

byte[] buffer = new byte[1024*8];

int len = -1;

while((len=in.read(buffer)) != -1){

out.write(buffer, 0, len);

}

}catch(Exception e){

System.out.println("Error: " + e.getMessage());

}finally{

try{

if(in != null) in.close();

if(out != null) out.close();

if(s != null) s.close();

System.out.println(s.getInetAddress().getHostAddress() + " connect closed..");

}catch(Exception e){}

}

}

}

参考资料:

自己写的

本回答由提问者推荐

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

更多推荐

java 传送文件

本文发布于:2024-03-04 18:21:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1710038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!