Struts Upload  + Java I/O handle file.

解决方案 »

  1.   

    据我所知,只能判断出是否是.txt文件,是的话一个一个的用io操作读取到新的文件中
      

  2.   

    /**
     * 拷贝路径下的文件到另外一个路径
     * 
     * @param in
     * @param out
     */
    public static void copyPath(File srcDir, File outDir)
    throws FileNotFoundException, IOException {final String src = srcDir.getPath();
    final String dest = outDir.getPath();
    if ((srcDir == null) || !srcDir.isDirectory()) {
    return;
    }
    File[] entries = srcDir.listFiles();
    int sz = entries.length;
    for (int i = 0; i < sz; i++) {
    if (entries[i].isDirectory()) {
    // 路径相对于输入路径的路径
    String relPath1 = entries[i].getPath().substring(src.length());
    // 写入目标的路径
    String writeDirPath = dest + relPath1;
    // 建立文件夹
    new File(writeDirPath).mkdirs();
    // 递归
    copyPath(entries[i], outDir);
    } else {
    // 源文件的路径
    String srcFilePath = entries[i].getPath();
    System.out.println("源文件路径:" + srcFilePath);
    // 源文件相对与输入路径的路径
    String relPath2 = entries[i].getPath().substring(src.length());
    System.out.println("输入路径:" + src);
    System.out.println("源文件相对与输入路径的路径:" + relPath2);
    // 写入文件的路径
    String writeFilePath = dest + relPath2;
    System.out.println("写入路径:" + writeFilePath);
    // 建立文件
    new File(writeFilePath).createNewFile();
    // 拷贝文件
    copyFile(srcFilePath, writeFilePath);
    }
    }
    }
      

  3.   

    /**
     * <h3>拷贝文件</h3>
     * InputStream ----> OutputStream
     * 
     * @param in
     * @param out
     * @param close
     */
    public static void copyFile(InputStream in, OutputStream out, boolean close)
    throws IOException { int b;
    while ((b = in.read()) != -1) {
    out.write(b);
    }
    in.close();
    if (close)
    out.close();
    }
      

  4.   

    /**
     * <h3>拷贝文件</h3>
     * 输入路径 ----> 输出路径
     * 
     * @param in
     * @param out
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void copyFile(String in, String out)
    throws FileNotFoundException, IOException { BufferedInputStream is = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
    out));
    System.out.println("拷贝文件:" + in + "\n" + "CopTo>" + out);
    System.out.println("\n");
    copyFile(is, bos, true);
    }
      

  5.   

    如果我把代码写到JSP中,不知道可否利用文件选择框来指定路径,然后传到后台的serverlet中去?