Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建jsp搜索表单以运行java搜索程序_Java_Jsp - Fatal编程技术网

创建jsp搜索表单以运行java搜索程序

创建jsp搜索表单以运行java搜索程序,java,jsp,Java,Jsp,这里的背景信息是,我有一个工作索引器和搜索(java),它对文件目录中的文件名进行索引和搜索,然后将文件复制到“Results”目录 我需要/没有太多的经验来编写jsp文件。我需要jsp文件有一个文本搜索栏,然后是一个搜索按钮。当在栏中输入文本并单击按钮时,我需要它将输入的文本作为参数来运行搜索程序 我添加了IndexFiles和SearchFiles类以供参考 如果你能帮忙,请用一个好的例子来解释 public class SearchFiles { static File se

这里的背景信息是,我有一个工作索引器和搜索(java),它对文件目录中的文件名进行索引和搜索,然后将文件复制到“Results”目录

我需要/没有太多的经验来编写jsp文件。我需要jsp文件有一个文本搜索栏,然后是一个搜索按钮。当在栏中输入文本并单击按钮时,我需要它将输入的文本作为参数来运行搜索程序

我添加了IndexFiles和SearchFiles类以供参考

如果你能帮忙,请用一个好的例子来解释

   public class SearchFiles {
    static File searchDirectory = new File(
            "C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results");
    static String v = new String();
    static String path = null;
    String title = null;
    File addedFile = null;
    OutputStream out = null;
    String dirName = "C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results";

    public static void main(String[] args) throws Exception {
        String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string]";
        if (args.length > 0
                && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
            System.out.println(usage);
            System.exit(0);
        }
        for (int j = 5; j < args.length; j++) {
            v += args[j] + " ";
        }
        String index = "index";
        String field = "contents";
        String queries = null;
        boolean raw = false;
        String queryString = null;
        int hits = 100;

        for (int i = 0; i < args.length; i++) {
            if ("-index".equals(args[i])) {
                index = args[i + 1];
                i++;
            } else if ("-field".equals(args[i])) {
                field = args[i + 1];
                i++;
            } else if ("-queries".equals(args[i])) {
                queries = args[i + 1];
                i++;
            } else if ("-query".equals(args[i])) {
                queryString = v;
                i++;

            }
        }
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(
                index)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

        BufferedReader in = null;
        if (queries != null) {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(
                    queries), "UTF-8"));
        } else {
            in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
        }
        QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
        for (int m = 0; m < 2; m++) {
            if (queries == null && queryString == null) {
                System.out.println("Enter query: ");
            }

            String line = queryString != null ? queryString : in.readLine();
            if (line == null || line.length() == -1) {
                break;
            }

            line = line.trim();
            if (line.length() == 0) {
                break;
            }

            Query query = parser.parse(line);
            System.out.println("Searching for: " + query.toString(field));

            doPagingSearch(in, searcher, query, hits, raw, queries == null
                    && queryString == null);

            if (queryString == null) {
                break;
            }
        }
        reader.close();
    }

    public static void doPagingSearch(BufferedReader in,
            IndexSearcher searcher, Query query, int hitsPerPage, boolean raw,
            boolean interactive) throws IOException {

        // Collect enough docs to show 500 pages
        TopDocs results = searcher.search(query, 5 * hitsPerPage);
        ScoreDoc[] hits = results.scoreDocs;

        int numTotalHits = results.totalHits;
        System.out.println(numTotalHits + " total matching documents");
        int start = 0;
        int end = Math.min(numTotalHits, hitsPerPage);
        FileUtils.deleteDirectory(searchDirectory);
        while (true) {

            for (int i = start; i < end; i++) {
                Document doc = searcher.doc(hits[i].doc);
                path = doc.get("path");
                if (path != null) {
                    System.out.println((i + 1) + ". " + path);

                    File addFile = new File(path);
                    try {
                        FileUtils.copyFileToDirectory(addFile, searchDirectory);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

            if (!interactive || end == 0) {
                break;
            }
            System.exit(0);

        }

    }

}

public class IndexFiles {

    private IndexFiles() {
    }

    public static void main(String[] args) {
        String usage = "java org.apache.lucene.demo.IndexFiles"
                + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
                + "This indexes the documents in DOCS_PATH, creating a Lucene index"
                + "in INDEX_PATH that can be searched with SearchFiles";
        String indexPath = null;
        String docsPath = null;
        boolean create = true;
        for (int i = 0; i < args.length; i++) {
            if ("-index".equals(args[i])) {
                indexPath = args[i + 1];
                i++;
            } else if ("-docs".equals(args[i])) {
                docsPath = args[i + 1];
                i++;
            } else if ("-update".equals(args[i])) {
                create = false;
            }
        }

        if (docsPath == null) {
            System.err.println("Usage: " + usage);
            System.exit(1);
        }

        final File docDir = new File(docsPath);
        if (!docDir.exists() || !docDir.canRead()) {
            System.out
                    .println("Document directory '"
                            + docDir.getAbsolutePath()
                            + "' does not exist or is not readable, please check the path");
            System.exit(1);
        }

        Date start = new Date();
        try {
            System.out.println("Indexing to directory '" + indexPath + "'...");

            Directory dir = FSDirectory.open(new File(indexPath));
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
            IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,
                    analyzer);

            if (create) {
                iwc.setOpenMode(OpenMode.CREATE);
            } else {

                iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
            }
            IndexWriter writer = new IndexWriter(dir, iwc);
            indexDocs(writer, docDir);
            writer.close();

            Date end = new Date();
            System.out.println(end.getTime() - start.getTime()
                    + " total milliseconds");
        } catch (IOException e) {
            System.out.println(" caught a " + e.getClass()
                    + "\n with message: " + e.getMessage());
        }
    }

    static void indexDocs(IndexWriter writer, File file) throws IOException {
        if (file.canRead()) {
            if (file.isDirectory()) {
                String[] files = file.list();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        indexDocs(writer, new File(file, files[i]));
                    }
                }
            } else {

                FileInputStream fis;
                try {
                    fis = new FileInputStream(file);
                } catch (FileNotFoundException fnfe) {
                    return;
                }
                try {
                    Document doc = new Document();
                    Field pathField = new StringField("path",
                            file.getAbsolutePath(), Field.Store.YES);
                    doc.add(pathField);
                    doc.add(new LongField("modified", file.lastModified(),
                            Field.Store.NO));
                    doc.add(new TextField("title", file.getName(), null));
                    System.out.println(pathField);

                    if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
                        System.out.println("adding " + file);
                        writer.addDocument(doc);
                    } else {
                        System.out.println("updating " + file);
                        writer.updateDocument(new Term("path", file.getPath()),
                                doc);
                    }

                } finally {
                    fis.close();
                }
            }
        }
    }
}
公共类搜索文件{
静态文件searchDirectory=新文件(
“C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results”);
静态字符串v=新字符串();
静态字符串路径=null;
字符串标题=null;
文件addedFile=null;
OutputStream out=null;
String dirName=“C:\\Users\\flood.j.2\\Desktop\\IndexSearch\\Results”;
公共静态void main(字符串[]args)引发异常{
String usage=“用法:\tjava org.apache.lucene.demo.SearchFiles[-index dir][-field f][-repeat n][-querys file][-query String]”;
如果(args.length>0
&&(“-h”.equals(args[0])| |“-help”.equals(args[0])){
System.out.println(用法);
系统出口(0);
}
对于(int j=5;j