将搜索到的文件上传到服务器上

问题描述:

我编写了一个程序,可以成功地在硬盘上搜索文件。但现在我想添加一个更多的功能。我希望我的程序通过http将这些搜索到的文件上传到服务器上。所以任何人都可以解释一下这个策略是什么?将搜索到的文件上传到服务器上

这里是我的小程序

 public class Find { 

    public static class Finder extends SimpleFileVisitor<Path> { 

     private final PathMatcher matcher; 
     private int numMatches = 0; 

     Finder(String pattern) 
     { 
      matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); 
     } 

     // Compares the glob pattern against 
     // the file or directory name. 
     void find(Path file) 
     { 
      Path name = file.getFileName(); 
      if (name != null && matcher.matches(name)) 
      { 
       numMatches++; 
       System.out.println(file); 
      } 
     } 

     // Prints the total number of 
     // matches to standard out. 
     void done() 
     { 
      System.out.println("Matched: " 
       + numMatches); 
     } 

     // Invoke the pattern matching 
     // method on each file. 
     //@Override 
     public FileVisitResult visitFile(Path file, 
       BasicFileAttributes attrs) 
     { 
      find(file); 
      return CONTINUE; 
     } 

     // Invoke the pattern matching 
     // method on each directory. 
     //@Override 
     public FileVisitResult preVisitDirectory(Path dir, 
       BasicFileAttributes attrs) 
     { 
      find(dir); 
      return CONTINUE; 
     } 

     //@Override 
     public FileVisitResult visitFileFailed(Path file,IOException exc) 
     { 
      System.err.println(exc); 
      return CONTINUE; 
     } 
    } 

    static void usage() 
    { 
     System.err.println("java Find <path>" +" -name \"<glob_pattern>\""); 
     System.exit(-1); 
    } 

    public static void main(String[] args)throws IOException 
    { 

     if (args.length < 1) 
     { 
      usage(); 
     } 
     Iterable<Path> root; 
     root = FileSystems.getDefault().getRootDirectories(); 
     for (Path startingDir : FileSystems.getDefault().getRootDirectories()) 
     { 
      String pattern = args[0]; 

     Finder finder = new Finder(pattern); 
     Files.walkFileTree(startingDir, finder); 
     //finder.done(); 
     } 
    } 

} 
+0

我有一个帐户,主机服务器,我可以把我的文件。所以我试图把我搜索到的文件放在那个 – Vicky

+0

你想上传使用HTTP POST请求的文件吗?你将不得不有一个运行的服务器,它会听你的POST请求并上传文件 – WeMakeSoftware

+0

先生,我将自己做一个服务器,它不是一个大任务,但主要的是,我不明白如何从他们的位置挑选文件并将它们放入流中以上传到服务器上? – Vicky

好了,假设你已经拿到了文件的绝对路径。

只是应该怎样做一个粗略的想法(未测试):

 FileInputStream fileInputStream = null; 

     try { 
      new FileInputStream("absoluteFilename"); 

      byte[] buffer = new byte[MAX_SIZE]; 
      int bufferIndex = 0; 
      while (fileInputStream.available() > 0) { 
       buffer[bufferIndex++] = (byte) fileInputStream.read(); 
      } 
      byte[] fileContent = new byte[bufferIndex]; 
      System.arraycopy(buffer,0,fileContent,0,bufferIndex); 

      URL serverUrl = new URL(url); 
      URLConnection connection = serverURL.openConnection(); 
      connection.setConnectTimeout(60000); 
      connection.getOutputStream().write(fileContent); 

     } catch (Exception fatal) { 
      //proper handling?? 
     } finally { 
      if (fileInputStream != null) { 
       try { 
        fileInputStream.close(); 
       } catch (Exception ignored) {} 
      } 
     } 
+0

非常感谢先生。现在我可以进一步在我的程序 – Vicky

+0

她说“通过HTTP”。这可以很容易地使用一个库,如Apache的HttpComponents(http://hc.apache.org/),但它似乎无论如何适合她 –

+0

@Funtik我有120个文本文件在我的硬盘上。您的虚拟代码仅用于具有特定名称和目录的文件。我的文本文件分散在各个分区中,我的当前程序通过匹配作为参数提供的文件扩展名来搜索这些文件,然后使用绝对路径 – Vicky