各位老大. 我做了个APPLET 抓取摄相头 然后 上传到 SERVLET  ,用的是URLConnection 的OutputStream .可是只能上传10K以下的,大的文件后面的没有传过去.为什么???拜托

解决方案 »

  1.   


    请各位老大指教啊!!顺便帖上代码
    发送端:URL url = new URL(hurl + "?fpath="+fpath+"&sid="+sid+"&uname="+uname+"&number="+pNumber);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("POST");
       conn.setAllowUserInteraction(true); // you may not ask the user
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(true);
       conn.setRequestProperty("Content-Type","application/octet-stream");
    OutputStream out = conn.getOutputStream(); 
      
    JPEGImageEncoder   encoder   =   JPEGCodec.createJPEGEncoder(out);   
    JPEGEncodeParam   param   =   encoder.getDefaultJPEGEncodeParam(bi);   
    param.setQuality(1f,   false);   
    encoder.setJPEGEncodeParam(param);   
    encoder.encode(bi);   
    out.close();  
    System.out.println(conn.getContentType()+": "+ conn.getResponseCode()); //不知为什么必须这样数据才能发送接收: InputStream in=request.getInputStream();
    int len= request.getContentLength();
    //ObjectInputStream ins = new ObjectInputStream(in);
    byte[] b =new byte[1024*1024];
    in.read(b,0,len);
    File f = new File(pictPath+fpath);
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(b,0,len);
    System.out.println(len);
    in.close();
      

  2.   

    自己解决了,是没有接收完呀。以前也做过c++下的socket 。没想到java的read也不能一次读完。只是20分没地方送了。
    scdn这点要改进呀。呵呵,顺便把代码贴上。InputStream in=request.getInputStream();
    File f = new File(pictPath+fpath);
    FileOutputStream fos = new FileOutputStream(f);

    //ObjectInputStream ins = new ObjectInputStream(in);
    byte[] b =new byte[1024*1024];
    int totalBytes, bytes, sumBytes =0;
    totalBytes= request.getContentLength();
    while(true){
        bytes = in.read(b);
    if (bytes <=0) break;
    sumBytes += bytes;
    fos.write(b,0,bytes);
    System.out.println ( sumBytes + " of " + totalBytes + " " + (  ( float ) sumBytes/ (    float ) totalBytes ) *100 + "% done" ) ;

    }
    fos.close();
    in.close();