| 本帖最后由 高会仁 于 2012-12-20 16:56 编辑 
 可以这样:
 
 在客户端这样只写部分代码:
 FileInputStream fis = new FileInputStream(file);//输入流
 //定义一个200字节的区域来保存文件信息 这里200个字节可以根据需要定义,文件名占不了几个字节
 byte[] b = file.getName().getBytes();//这里根据需要,我只获取了file.getName()
 byte[] info = Arrays.copyOf(b, 200);
 ByteArrayInputStream bais = new ByteArrayInputStream(info);
 //合并流
 SequenceInputStream sis = new SequenceInputStream(bais,fis);
 OutputStream out = s.getOutputStream(); //输出流,目的为服务器
 byte[] buf = new byte[1024];
 
 intlen = 0;
 
 while((len=sis.read(buf))!=-1)
 {
 out.write(buf,0,len);
 }
 服务器取出文件名部分代码如下:
 Socket s = ss.accept();
 InputStream in = s.getInputStream();//获取socket的输入流
 byte[] info = new byte[200];//定义一个200个字节的数组,因为客户端就是定义的一个200个字节的数组来存储文件信息的
 in.read(info);//所以这里读取到的就是客户端上传文件的名称
 StringfileName = new String(info).trim();//获取文件名
 //获取完文件名后,后面就是惯例了
 FileOutputStream fos = new FileOutputStream(file);
 
 byte[]buf = new byte[1024];
 
 intlen = 0;
 while((len=in.read(buf))!=-1)
 {
 fos.write(buf,0,len);
 }
 
 
 |