tempLen = in.read(buffer,0,buffer.length永远是true
因为只有一个等号

解决方案 »

  1.   

    tempLen = in.read(buffer,0,buffer.length)) != -1
    改為:
    tempLen = in.read(buffer)) != -1
      

  2.   

    在while循环结束后,加上bout.flush()试试,我以前也出现过这种情况,记得好像是这么解决的
      

  3.   

    为什么条件是永真? = 不是赋值么
    tempLen = in.read(buffer,0,buffer.length)
    把每次读取的长度赋予tempLen,如果读取结束,tempLen被赋予-1
    楼主的程序好像没有错啊?
      

  4.   

    Don't believe JDK Document.
      

  5.   

    全部代码如下
    import java.io.*;import javax.servlet.*;
    import javax.servlet.http.*;public class Hello extends HttpServlet {
        public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException
        {
            System.out.println("execute");
            int len = request.getContentLength();
            System.out.println(len);
            DataInputStream in = new DataInputStream(request.getInputStream());
            ByteArrayOutputStream bout = new ByteArrayOutputStream();        int tempLen =0;
            byte[] buffer = new byte[1024];
           while((tempLen = in.read(buffer,0,buffer.length)) != -1) {
             System.out.println(tempLen);
             bout.write(buffer,0,tempLen);
            }
            bout.flush();
         
            System.out.println("out while"); String[] tokens={"name=\"","\"; filename=\"", "\"\r\n","Content-Type: ","\r\n\r\n"};
    String gs = "-----------------------------7d22f7261e00dc";
    int count = 0;

    System.out.println(request.getContentType());
            /*OutputStream outt=new BufferedOutputStream(
                new FileOutputStream("Receive.log",true));
            byte[] breaker="\r\nNewLog: -------------------->\r\n".getBytes();
            System.out.println(request.getContentType());
            outt.write(breaker,0,breaker.length);
            outt.write(buffer);
            outt.close();*/        //--------------------------------
            
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();        out.println("<html>");
            out.println("<head>");        out.println("</head>");
            out.println("<body>");
            out.println("hello world!");
            out.println("</body>");
            out.println("</html>");
        }
    }
      

  6.   

    The read() methd didn't return -1 except disk error, network error etc.So first thing, you should get the file's size.
    And the second, you should compare it with the data size which has been read from file.
    Last, if one value is equal to another, then quit loop.
      

  7.   

    我前面提到的方法你試了嗎,在我機子上都可以的
     try{
         FileInputStream fi=new FileInputStream("d:\\MasterEjb\\testJList.java");
         //int len = fi.getContentLength();
         //System.out.println(len);
         byte[] buffer = new byte[1024];
         InputStream in = new DataInputStream(fi);
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         int tempLen =0;
         while((tempLen = in.read(buffer))!= -1) {
             System.out.println(tempLen);
             bout.write(buffer,0,tempLen);
         }
         System.out.println("out while");
        }catch(Exception e){
        }
      

  8.   

    lifeiqhp(飛哥) :你的方法不适用于network。
     menlion(浪·星魂):it should be right when u operate on the net stream.But it'll return the value "-1" when the program reach the end of the file on the disk.
    我最后改了while循环:
    while(bout.size() < len) {
            tempLen = in.read(buffer,0,buffer.length);
             bout.write(buffer,0,tempLen);
     }