提问:下载文件到sd卡 发现内容结尾不全或出现NUL!?

从自己的本机的tomcat上下载一个xml文件 到模拟器sd卡中打开 如下

本来这是个很简单的xml后面多了很多nul

所以解析时出现错误:



 在下载大的xml时出现结尾不全的情况 而且发现内容出现了一个像是乱码的东西:


我觉得是我的下载和写入sd卡的方法有问题 可是我查了很多还是没发现错误:

 

 

 

/**
     * 下载文件到sd
     * @param urlStr
     * @param path
     * @param fileName
     * @return 如果返回0成功  ;-1失败  
     */
    public int downFile(String urlStr,String path,String fileName){
        InputStream is = null;
        try {
            FileUtils fu = new FileUtils();
            //从URL获得输入流
            is = getISFromURL(urlStr);
            InputSource iSource = new InputSource(is); iSource.setEncoding("UTF-8");
            if(fu.isFileExist(path+fileName)){
                new  File(path+fileName).delete(); 
            }
                File resultFile = fu.write2SDFromIS(path, fileName, is);
                if(resultFile == null){
                    return -1;
                }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return -1;
        }finally{
            try {
                is.close(); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return 0;
    }

 

   /** is流写入sd卡
     * @param path
     * @param fileName 路径+名字
     * @param is
     * @return
     */
    public File write2SDFromIS(String path,String fileName,InputStream is){
        File file = null;
        OutputStream os = null;
        try {
            createSDDir(path);
            file = createSDFile(path+fileName);
            os = new FileOutputStream(file);
            
            byte buffer[] = new byte[4*1024];
            while(is.read(buffer) != -1){
                os.write(buffer);
            }
            os.flush();
        
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return file;
        
    }

 希望大家帮我发现错误 谢谢了~

我给你解决了,记得给我加分啊!