从网络上下载文件到sd卡上
String SDPATH = Environment.getExternalStorageDirectory() + "/";
String path = SDPATH + "files/";
/** * 此文件支持下载docx,pdf,xls,jpg, * @param urlStr * @param path * @param fileName * @return * -1:文件下载出错 * 0:文件下载成功 * 1:文件已经存在 */ public int downFile(String urlStr, String path, String fileName) { InputStream inputStream = null; HttpURLConnection urlConn = null; try { // 判断文件是否存在 if (FileUtil.isFileExist(path + fileName)) { System.out.println("====File==exits==="); return 1; } else { url = new URL(urlStr); urlConn = (HttpURLConnection) url.openConnection(); // 设置请求方式为"GET" urlConn.setRequestMethod("GET"); // 超时响应时间为5秒 urlConn.setConnectTimeout(5 * 1000); // 得到io输入流,即从url读取到的数据 inputStream = urlConn.getInputStream(); // 从input流中将文件写入SD卡中 File resultFile = FileUtil.write2SDFromInput(path, fileName, inputStream); if (resultFile == null) { return -1; } } } catch (Exception e) { e.printStackTrace(); return -1; } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return 0; }
/** * 将一个InputStream里面的数据写入到SD卡中 * * @param path * @param fileName * @param input * @return */ public static File write2SDFromInput(String path, String fileName, InputStream inputStream) { File file = null; OutputStream outputStream = null; try { // 创建文件,父目录若无,则会去先创建 file = createFile(path + fileName); // 开启输出流,准备写入文件 outputStream = new FileOutputStream(file); // 缓冲区 byte[] buffer = new byte[FILESIZE]; int bytesRead = 0; // 使用一个输入流从buffer里把数据读取出来 while ((bytesRead = inputStream.read(buffer)) != -1) { // outputStream.write(buffer); // 每次读多少,写多少进去,如果没有加bytesRead,则会出现随机将每一行后面的空数据也写入,造成一些文件格式的损坏和文件大小增大。 outputStream.write(buffer, 0, bytesRead); } // 关闭输入流 inputStream.close(); outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return file; }