我在客户端选择一个文件,然后上传到服务器端,我现在用下面这个程序来做,为什么不行呢? 大家帮我看一下吧,小弟这里有礼了。
import java.io.*;
import java.net.*;
// 此程序是为了在服务器端接收一个文本文件,已经收到
//服务器端的任务是接收管理员传送过来的一个文件,并用将其导入到数据库中
//在这里,能用到有关于bcp的使用
public class up_load_file_server
{
public static void main(String[] args)
{
byte []bs=new byte[200];
try
{
File file=new File("d:\\ooo.txt");
if(!file.exists())
{
file.createNewFile();
}
//建立服务器端,监听8887端口
ServerSocket serversocket=new ServerSocket(8887);
//接收客户端的输出
Socket socket=serversocket.accept();
// DataInputStream inStream=new DataInputStream(socket.getInputStream());
// DataOutputStream outStream=new DataOutputStream(socket.getOutputStream());
BufferedInputStream bufferin=new BufferedInputStream(socket.getInputStream());
FileOutputStream fileOut=new FileOutputStream(file);
System.out.println("主要程序已经开始执行");
while(bufferin.available()>0)
{
System.out.println("服务器端接收到的字节数为"+bufferin.available()); bufferin.read(bs,0,bs.length);
fileOut.write(bs);//将读出来的内容写入文件输出流中 }
System.out.println("程序已经执行完结");
}
catch (IOException e1)
{
e1.printStackTrace();
} }};
import java.io.*;
import java.net.*;
public class up_load_file_client
{
// 此类是向服务器上传一个文件,将文件的内容写入BufferedReader中,然后发送此缓冲流到服务器
public static void main(String[] args)
{
byte[] buf=new byte[10000];
Socket socket;
DataOutputStream outStream;
try
{
File file=new File("d:\\ccc.txt");
FileInputStream fin=new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fin);
bis.read(buf,0,buf.length);
System.out.println(bis.available());
socket=new Socket("localhost",8887);//连接服务器的8887端口
outStream=new DataOutputStream(socket.getOutputStream());
System.out.println(bis.available());
outStream.write(buf,0,buf.length-2);//向服务器端发送一个流
}
catch (IOException e)
{
e.printStackTrace();
}
};
};
那样,我怎么样在客户端来接收这个变量呢?
在保证客户端程序能上传到服务器端的前提下啊
麻烦大哥哥帮我想一下办法,谢谢