读书人

android 从web服务器下载word文件在

发布时间: 2012-04-05 12:42:40 作者: rapoo

android 从web服务器下载word文件,在模拟器可以正常打开,在设备端提示文件损坏是怎么一回事?
代码:

Java code
public int downFile(String serverPath, String clientPath, String fileName){        InputStream inputStream = null;        try{          String encodeFileName = URLEncoder.encode(fileName, "UTF-8");          String serverFilePath = serverPath +"/"+ encodeFileName;    //      String gbkFilePath = URLEncoder.encode(serverFilePath, "UTF-8");            FileUtils fileUtils = new FileUtils();            //测试路径和文件是都存在            if(fileUtils.isFileExist(serverFilePath)){                return 1;            }else{                inputStream = getInputStreamFromUrl(serverFilePath);                File resultFile = fileUtils.write2SDFormInput(clientPath, fileName, inputStream);                if(resultFile == null){                    return -1;                }            }        }catch (Exception e) {            e.printStackTrace();            return -1;        }finally{            try{                inputStream.close();            }catch (Exception e) {                e.printStackTrace();            }        }                return 0;    }    /**     * 根据URL的到输入流     * @param urlStr     * @return     * @throws MalformedURLException     * @throws IOException     */    public InputStream getInputStreamFromUrl(String urlStr)            throws MalformedURLException, IOException{        url = new URL(urlStr);        HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();        InputStream inputStream = urlConn.getInputStream();        return inputStream;    }


[解决办法]
下载的数据不一致吧,比较一下下载的数据就行了。

读书人网 >Android

热点推荐