下面给出源代码。 ---------------------------------------------------------------- 客户端: FTPClient.java import java.io.*; import java.net.*;public class FTPClient{
public static void main(String[] args){
new FTPClient().start();
}
private void start(){
Socket socket; //BufferedReader in; PrintStream out; String line; DataInputStream din = new DataInputStream(System.in); BufferedWriter of = null; try{ of = new BufferedWriter(new FileWriter(new File("D:\\a.txt"))); }catch(IOException e){ System.out.println("No such file"); }
try{ socket = new Socket("localhost",1999); DataInputStream in = new DataInputStream(socket.getInputStream()); //in = getBufferedReader(socket); out = new PrintStream(socket.getOutputStream());
while(true){
System.out.println(); System.out.print(":"); line = din.readLine();
/* * pre represents that if the blank should be add to the previous of the string * len represents that the standard length of the string */ private String format(String s, int len, boolean pre){
if(g.exists()&&g.isFile()){ try{ FileInputStream fin = new FileInputStream(g); int len = (int)g.length(); byte[] content = new byte[len]; out.println(len); fin.read(content,0,len); fin.close(); out.write(content,0,len); } 和FileOutputStream fout = new FileOutputStream("C:\\"+line.substring(3).trim()); byte[] content = new byte[len]; in.read(content,0,len); fout.write(content,0,len); fout.close(); System.out.println(len + " bytes received"); 对应的, FileOutputStream传输的时候不用考虑字节,文字问题,只有读出来显示时就要用到DataInputStream,PrintStream
学习 to dyan09(初识java) 是public final String readUTF()吧 本方法从文件中读取字节,这些字节被转成用UTF-8编码的字符并被转换成unicode字符,最后返回一个与编码后的字符相同的字符串,在没有数据时等待,可抛出EOFException、IOException、UTFDataFormatException
to Hodex(小何才露尖尖角) :有没有试过执行FTP传过来的文件?我这边是传过来的文件大小是一样的,不过就是不能执行。文本文件和MP3文件是好的,我试过了。
----------------------------------------------------------------
客户端:
FTPClient.java
import java.io.*;
import java.net.*;public class FTPClient{
public static void main(String[] args){
new FTPClient().start();
}
private void start(){
Socket socket;
//BufferedReader in;
PrintStream out;
String line;
DataInputStream din = new DataInputStream(System.in);
BufferedWriter of = null;
try{
of = new BufferedWriter(new FileWriter(new File("D:\\a.txt")));
}catch(IOException e){
System.out.println("No such file");
}
//if(args.length!=1){
// System.out.println("Usage: java FTPClient <hostname>");
// return;
//}
try{
socket = new Socket("localhost",1999);
DataInputStream in = new DataInputStream(socket.getInputStream());
//in = getBufferedReader(socket);
out = new PrintStream(socket.getOutputStream());
while(true){
System.out.println();
System.out.print(":");
line = din.readLine();
if(line.trim().equals(""))
continue;
out.println(line);
if(line.trim().equals("quit"))
break;
else if(line.trim().substring(0,2).equals("ls")){
int num = Integer.parseInt(in.readLine());
for(int i = 0; i < num; i++){
line = in.readLine();
of.write(line);
System.out.println(line);
}
System.out.println("Total: "+num+" Items"); }else if(line.trim().substring(0,3).equals("get")){
int len = Integer.parseInt(in.readLine());
if(len < 0){
line = in.readLine();
System.out.println(line);
}
else{
FileOutputStream fout = new FileOutputStream("C:\\"+line.substring(3).trim());
byte[] content = new byte[len];
in.read(content,0,len);
fout.write(content,0,len);
fout.close();
System.out.println(len + " bytes received");
}
}
else{
line = in.readLine();
System.out.println(line);
}
}
in.close();
out.close();
socket.close();
of.close();
}
catch(IOException e){
System.out.println(e);
}
}
}----------------------------------------------------------------------
服务器端:FTPServer.java
import java.io.*;
import java.net.*;public class FTPServer{
ThreadGroup group;
FTPServer(){
group = new ThreadGroup("FTPThreads");
}
protected void finalize(){
group.stop();
}
public static void main(String[] args){
ServerSocket server;
Socket socket;
FTPServer ftp = new FTPServer();
try{
server = new ServerSocket(1999);
System.out.println("Listening on port: 1999");
}catch(IOException e){
System.out.println("e");
return;
}
while(true){
try{
socket = server.accept();
System.out.println("New user form" + socket.getInetAddress().toString());
(new FTPServerThread(ftp.group, ftp, socket)).start();
}catch(IOException e){
System.out.println(e);
return;
}
}
}
}--------------------------------------------------------------------
FTPServerThread.java
import java.io.*;
import java.net.*;class FTPServerThread extends Thread{
FTPServer ftp;
Socket socket;
DataInputStream in;
PrintStream out;
String line;
FileList fl;
public FTPServerThread(ThreadGroup group,FTPServer ftp,Socket socket){
super(group,"FTPThread");
this.ftp = ftp;
this.socket = socket;
fl = new FileList("d:\\");
}
public void run(){
try{
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
while(true){
line = in.readLine();
if(line.trim().equals("ls")){
fl.Display(out);
}else if(line.trim().equals("quit")){
break;
}else if(line.substring(0,2).equals("cd")){
System.out.println("new directory " + line.substring(2).trim());
fl.ChDir(line.substring(2).trim(),out);
}else if(line.substring(0,3).equals("get")){
fl.put(line.substring(3).trim(),out);
}else{
out.println("Invalid command");
}
}
in.close();
out.close();
System.out.println("User "+socket.getInetAddress().toString()+" left");
socket.close();
}
catch(IOException e){
System.out.println(e);
}
}
}-------------------------------------------------------
FileList.javaimport java.io.*;
import java.net.*;class FileList{
File f;
FileList(String dir){
f = new File(dir);
}
public void ChDir(String dir,PrintStream out){
if(dir.equals("..")){
if(f.getParent() != null){
f = new File(f.getParent());
out.println("Current directory: " + f.getPath());
}
else{
out.println("Already root directory!");
}
}
else{
File g = new File(f, dir);
if(g.exists() && g.isDirectory()){
f = g;
out.println("Current directory: "+f.getPath());
}
else{
out.println("No such directory!");
}
}
}
public void Display(PrintStream out){
String[] st = f.list();
File g;
out.println(st.length);
for(int i = 0; i < st.length; i++){
g = new File(f,st[i]);
if(g.isFile()){
out.println(format(st[i], 30, false) + format(String.valueOf(g.length()), 10, true));
}
else{
out.println(format(st[i], 30, false) + format("<DIR>", 10, true));
}
}
}
/*
* pre represents that if the blank should be add to the previous of the string
* len represents that the standard length of the string
*/
private String format(String s, int len, boolean pre){
StringBuffer buf = new StringBuffer(s);
while(buf.length() < len){
if(pre){
buf.insert(0," ");
}else{
buf.append(" ");
}
}
return(new String(buf));
}
public void put(String fn, PrintStream out){
File g = new File(f,fn);
if(g.exists()&&g.isFile()){
try{
FileInputStream fin = new FileInputStream(g);
int len = (int)g.length();
byte[] content = new byte[len];
out.println(len);
fin.read(content,0,len);
fin.close();
out.write(content,0,len);
}
catch(IOException e){}
}
else{
out.print(-1);
out.println("No such file!");
}
}
}
我的问题是如果是二进制文件的话,在客户端用DataInputStream读出来的数据和原文件会有出入,文本文件没有问题。
还有怎样让DataInputStream读出来的中文正确显示。
楼上的楼上那位老兄,转俺的帖子也要说一声啊,俺就在你楼上也
try{
FileInputStream fin = new FileInputStream(g);
int len = (int)g.length();
byte[] content = new byte[len];
out.println(len);
fin.read(content,0,len);
fin.close();
out.write(content,0,len);
}
和FileOutputStream fout = new FileOutputStream("C:\\"+line.substring(3).trim());
byte[] content = new byte[len];
in.read(content,0,len);
fout.write(content,0,len);
fout.close();
System.out.println(len + " bytes received");
对应的,
FileOutputStream传输的时候不用考虑字节,文字问题,只有读出来显示时就要用到DataInputStream,PrintStream
to dyan09(初识java)
是public final String readUTF()吧
本方法从文件中读取字节,这些字节被转成用UTF-8编码的字符并被转换成unicode字符,最后返回一个与编码后的字符相同的字符串,在没有数据时等待,可抛出EOFException、IOException、UTFDataFormatException