jspsmartupload的代码~~public void downloadFile(String sourceFile,String contentType,String destFileName,int bufferSize)
    {
         byte[] b = new byte[bufferSize];  //存储每次读取的字节
         long fileSize =0;   //文件大小
         int once = 0;        //每次读取的字节数
         int total = 0;        //总共读取的字节数
         String headInfo = "";
         FileInputStream fileIn = null;
         String filename = "";
         //转换编码
        if(sourceFile != null)
            sourceFile = chgStr(sourceFile,"ISO8859_1","GBK");        filename = getFileName(sourceFile);
       //设置路径
        if (isVirtualPath(sourceFile))
           sourceFile = getPhysicalFilePath(sourceFile)+filename;
       //设置contentType
         if (contentType == null)
         {
             contentType = "application/x-msdownload";
         }
         else if(contentType =="")
         {
             contentType = "application/x-msdownload";
         }
         else
         {
             contentType = contentType.trim();
         }
          response.setContentType(contentType);
          response.setContentLength((int)fileSize);
         //设置页面的Header         if (destFileName == null)
         {
             filename= chgStr(filename,"GBK","ISO8859_1");
             headInfo = new String(new StringBuffer("attachment;").append(" filename=").
                                   append(filename));
         }
         else if (destFileName.length() ==0)
         {
             filename =chgStr(filename,"GBK","ISO8859_1");
             headInfo = new String(new StringBuffer("attachment;").append(" filename=").
                                   append(filename));
         }
         else
         {
             headInfo  = new String(new StringBuffer("attachment;").append(" filename=").
                                    append(destFileName));         }        response.setHeader("content-disposition",headInfo);         java.io.File downFile = new java.io.File(sourceFile);
         fileSize = downFile.length(); //获得文件大小           try
           {
             fileIn = new FileInputStream(downFile);
           }
           catch (FileNotFoundException ex)
           {
              System.out.println("Greate FileInputStream error!"+ex.getMessage());
           }         //是否读取完文件。
         while((long)total < fileSize)
         {
             try
             {
               once = fileIn.read(b,0,bufferSize);             total += once;
               response.getOutputStream().write(b,0,once);
             }
             catch (IOException ex)
             {
                 System.out.println("write file failed!"+ex.getMessage());
             }
         }         try
         {           response.getOutputStream().flush();
           response.flushBuffer();
         }
         catch (IOException ex)
         {
             System.out.println("flushBuffer() error!"+ex.getMessage());
         }
         try
         {
           if (fileIn != null)
             fileIn.close(); //释放资源
         }
         catch (IOException ex)
         {
             System.out.println("fileIn.close() error!"+ex.getMessage());
         }
    }

解决方案 »

  1.   

    jspsmartupload的代码~~public void downloadFile(String sourceFile,String contentType,String destFileName,int bufferSize)
        {
             byte[] b = new byte[bufferSize];  //存储每次读取的字节
             long fileSize =0;   //文件大小
             int once = 0;        //每次读取的字节数
             int total = 0;        //总共读取的字节数
             String headInfo = "";
             FileInputStream fileIn = null;
             String filename = "";
             //转换编码
            if(sourceFile != null)
                sourceFile = chgStr(sourceFile,"ISO8859_1","GBK");        filename = getFileName(sourceFile);
           //设置路径
            if (isVirtualPath(sourceFile))
               sourceFile = getPhysicalFilePath(sourceFile)+filename;
           //设置contentType
             if (contentType == null)
             {
                 contentType = "application/x-msdownload";
             }
             else if(contentType =="")
             {
                 contentType = "application/x-msdownload";
             }
             else
             {
                 contentType = contentType.trim();
             }
              response.setContentType(contentType);
              response.setContentLength((int)fileSize);
             //设置页面的Header         if (destFileName == null)
             {
                 filename= chgStr(filename,"GBK","ISO8859_1");
                 headInfo = new String(new StringBuffer("attachment;").append(" filename=").
                                       append(filename));
             }
             else if (destFileName.length() ==0)
             {
                 filename =chgStr(filename,"GBK","ISO8859_1");
                 headInfo = new String(new StringBuffer("attachment;").append(" filename=").
                                       append(filename));
             }
             else
             {
                 headInfo  = new String(new StringBuffer("attachment;").append(" filename=").
                                        append(destFileName));         }        response.setHeader("content-disposition",headInfo);         java.io.File downFile = new java.io.File(sourceFile);
             fileSize = downFile.length(); //获得文件大小           try
               {
                 fileIn = new FileInputStream(downFile);
               }
               catch (FileNotFoundException ex)
               {
                  System.out.println("Greate FileInputStream error!"+ex.getMessage());
               }         //是否读取完文件。
             while((long)total < fileSize)
             {
                 try
                 {
                   once = fileIn.read(b,0,bufferSize);             total += once;
                   response.getOutputStream().write(b,0,once);
                 }
                 catch (IOException ex)
                 {
                     System.out.println("write file failed!"+ex.getMessage());
                 }
             }         try
             {           response.getOutputStream().flush();
               response.flushBuffer();
             }
             catch (IOException ex)
             {
                 System.out.println("flushBuffer() error!"+ex.getMessage());
             }
             try
             {
               if (fileIn != null)
                 fileIn.close(); //释放资源
             }
             catch (IOException ex)
             {
                 System.out.println("fileIn.close() error!"+ex.getMessage());
             }
        }
      

  2.   

    你应该建立一个BufferedOutputStream的引用,而不应该两次调用response.getOutputStream
      

  3.   

    在response.getOutputStream().write(b,0,once);之下加上
     if(getSize() <= 0x10000)
        response.flushBuffer();再试试