当前页面: 开发资料首页 → J2EE 专题 → 问一个文件保存的问题,兄弟们过来看看啊! 
问一个文件保存的问题,兄弟们过来看看啊! 
摘要: 问一个文件保存的问题,兄弟们过来看看啊!  
我现在用弄了一个文件传输的小软件,文件上传是通过HTTP协议上传的,服务器是用SOCKET的监听一个端口。把数据拆分,然后把文件数据保存成文件,但现在有点问题,就是PDF,DOC,EXE,RAR等文件上传以后就打不开,出错。只有TXT,INI,XML文件没有问题,请大家说说可能的原因,如果需要我写的代码说明,我可以帖出来。谢谢
你可能用的是字符流而不是用的二进制流
你有没有这方面的资料,发出来看看啊。
贴
///////以下是客户端向服务器端写文件////////////////////////////////////////// 
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
              {
                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;
  }
///////////////////////////////////////////////////////////////
input.read(buffer);
String msg = new String(buffer);
你这里,不就把接收的数据buffer变成String了吗?  很多STRING型不能处理的二进制数据都丢了.
实际上,是从第一个碰到的00开始,以后的都被截掉了.
你这个服务器端的接收文件的写法,错得太厉害.
这些代码是你粘来的吧,粘来的也要自己能读懂,才能根据自己的要求进行修改啊.
多看看JAVA的文件操作吧.这里不是学校,没法说太多.
路过,友情up!
theforever(碧海情天)说得对,我应该好好看看书了。谢谢!
顺便说一句。你在这里就不能用/r/n作为结束符号了。
最好在http协议中先传入长度,然后按长度接收。
/r/n 是拆分HTTP头信息的,我文件里面没有加啊。
现在传输任何文件都已经搞定.如果谁需要代码,我可以发给他.谢谢大家