将文件从手机传输到网络共享

时间:2013-06-19 18:22:06

标签: android file

首先,请提前感谢您提供的任何帮助。

希望我的问题是可以解决的问题。我有一个应用程序,基本上,允许用户输入数据,然后通过电子邮件作为附件发送该数据。我想做的是,如果用户通过wifi连接到他们的网络,而不是通过电子邮件发送文件,它会将文件传输到网络共享。我一直在寻找答案,但很遗憾没有办法做到这一点。

所以我想我真正的问题是,如果这是可能的,如果是的话,该怎么做呢。

1 个答案:

答案 0 :(得分:1)

如下所示,你需要copy the file,在你的实例中,我会假设dest File会被设置为......

new File("\\\\server\\path\\to\\file.txt")

class FileUtils {
  public static boolean copyFile(File source, File dest) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {
      bis = new BufferedInputStream(new FileInputStream(source));
      bos = new BufferedOutputStream(new FileOutputStream(dest, false));

      byte[] buf = new byte[1024];
      bis.read(buf);

      do {
        bos.write(buf);
      } while(bis.read(buf) != -1);
    } catch (IOException e) {
      return false;
    } finally {
      try {
        if (bis != null) bis.close();
        if (bos != null) bos.close();
      } catch (IOException e) {
        return false;
      }
    }

    return true;
  }

  // WARNING ! Inefficient if source and dest are on the same filesystem !
  public static boolean moveFile(File source, File dest) {
    return copyFile(source, dest) && source.delete();
  }

  // Returns true if the sdcard is mounted rw
  public static boolean isSDMounted() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
  }
}