///////以下是客户端向服务器端写文件////////////////////////////////////////// 
DataOutputStream dstream=new DataOutputStream(myHttpCon.getOutputStream());
        FileInputStream fis1=null;
        File file1=new File(filePath);
        fis1=new FileInputStream(file1);
        byte[] filebuff=new byte[2048];
        int howMany=-1;
        int totMany=0;
        howMany=fis1.read(filebuff,0,2048);
        dstream.write("filebegin\r\n".getBytes());
        while(howMany!=-1)
        {
           totMany+=howMany;
           dstream.write(filebuff,0,howMany);
           System.out.println(new String(filebuff));
           howMany=fis1.read(filebuff,0,2048);
         }
        dstream.write("\r\n".getBytes());
        dstream.write("fileend\r\n".getBytes());
        System.err.println("read"+totMany+"bytes from file,wrote to outputstream.");
        fis1.close();;
        dstream.close();//////////////////////////////////////////////////////////////////////////////////////////以下是服务器端的接收文件的写法,有点长,判断了几个状态。/////////////////////// public httpRequestHandler(Socket ssocket) throws Exception
  {
    this.socket=ssocket;
    this.input=socket.getInputStream();
    this.output=new PrintStream(socket.getOutputStream());
    this.br=new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
    String prefix=System.getProperty("user.dir")+System.getProperty("file.separator")+"uploadfile"+System.getProperty("file.separator");;
    String newfilename="";
    String downfilename="";
    boolean isfile=false;
    String operateType="";
    StringBuffer buf=new StringBuffer();
    boolean isoverfile=false;
     byte[] buffer = new byte[2048];
     try
     {
        while( true)
        {
           if(isoverfile==true)
           {
             break;
            }
            input.read(buffer);
            String msg = new String(buffer);
            String[] temparr=msg.split("\r\n");            if(temparr!=null)
            {
              for(int i=0;i<temparr.length;i++)
              {
                if(temparr[i].indexOf("operatetype")!=-1)
                {
                   operateType=getKeyValue(temparr[i],"operatetype");                   
                }
                if(operateType.equals("0"))  //upload file begin
                {
                    if (temparr[i].indexOf("filename") != -1) 
                    {
                        newfilename = getKeyValue(temparr[i], "filename");
                    }
                    if (temparr[i].indexOf("fileend") != -1) 
                    {
                        isoverfile = true;
                        break;
                    }
                    if (isfile == true) 
                    {
                        System.out.println("file content="+temparr[i]);
                        buf.append(temparr[i] + "\r\n");
                    }
                    if (temparr[i].indexOf("filebegin") != -1) {
                        isfile = true;
                    }
                }  // upload file end;                
              }
            }
        }
        if(operateType.equals("0"))
        {
            prefix+=newfilename;
            String retv=SaveFile(prefix,buf.toString());
            if(retv.equals("ok"))
            {
                output.println("file is accept ok!");
                output.println("file is save ok!");
                output.write(("savefilepath:"+prefix+"\r\n").getBytes());
                output.write("end".getBytes());
             }
             else
             {
                 output.println("file is accept ok!");
                 output.println("file is save error!");
                 output.write(("savefilepath:"+prefix+"\r\n").getBytes());
                 output.write("end".getBytes());
              }
        }
       
          output.close();
          input.close();
          br.close();   }
   catch(Exception ex)
   {
     ex.printStackTrace();
   }
  }  public String getKeyValue(String line,String keystr)
  {
      int startindex=(keystr+":").length();
      String retv="";
      if(startindex!=-1)
      {
        retv=line.substring(startindex).trim();
      }
      return retv;
  }
  public String SaveFile(String filepath,String filecontent)
  {
      String retv="error";
      try
      {
          FileWriter fw = new FileWriter(filepath, true);
          PrintWriter pw = new PrintWriter(fw);
          pw.println(filecontent);
          pw.flush();
          pw.close();
          fw.close();
          retv="ok";
      }
      catch(Exception ex)
      {
        ex.printStackTrace();
      }
      return retv;
  }
///////////////////////////////////////////////////////////////

解决方案 »

  1.   

    input.read(buffer);
    String msg = new String(buffer);你这里,不就把接收的数据buffer变成String了吗?  很多STRING型不能处理的二进制数据都丢了.
    实际上,是从第一个碰到的00开始,以后的都被截掉了.
      

  2.   

    你这个服务器端的接收文件的写法,错得太厉害.
    这些代码是你粘来的吧,粘来的也要自己能读懂,才能根据自己的要求进行修改啊.
    多看看JAVA的文件操作吧.这里不是学校,没法说太多.
      

  3.   

    theforever(碧海情天)说得对,我应该好好看看书了。谢谢!
      

  4.   

    顺便说一句。你在这里就不能用\r\n作为结束符号了。
    最好在http协议中先传入长度,然后按长度接收。