0

我编写了一个程序,可以成功搜索硬盘中的文件。但现在我想为它增加一项功能。我希望我的程序通过 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();
        }
    }

}
4

1 回答 1

0

好的,假设你有一个绝对的文件名。

只是大致了解应该做什么(未经测试):

     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) {}
            }
        }
于 2013-07-29T09:59:55.600 回答