我想把本地磁盘比如D:/text.doc放到我的web项目中的webroot下的content文件夹中,当然是通过程序拷贝过去,不是直接copy过去,我是通过路径拷贝的,可怎么也考不进去,我的路径是request.getContextPath()+"content";不知大家有没有好的方法,非常感谢大家帮忙

解决方案 »

  1.   

    request.getContextPath()+"/content";
      

  2.   


    System.out.println输出路径。
      

  3.   

    向二楼所说的那样,改了,错误提示:FileNotFoundException:\wmdw\content(系统找不到指定路径)
    wmdw是我的项目名称
      

  4.   


    把我的main方法挪到你的action中,路径换下试试:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;public class Test {    /**
         * 复制文件。targetFile为目标文件,file为源文件
         * 
         * @param targetFile
         * @param file
         */
        public static void copyFile(File targetFile, File file) {        if (targetFile.exists()) {
                System.out.println("文件" + targetFile.getAbsolutePath() + "已经存在,跳过该文件!");
                return;
            } else {
                createFile(targetFile, true);
            }
            System.out.println("复制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
            try {
                InputStream is = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(targetFile);
                byte[] buffer = new byte[1024];
                while (is.read(buffer) != -1) {
                    fos.write(buffer);
                }
                is.close();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }    /**
         * createFile
         * 
         * @param file
         * @param isFile
         * 
         * @author guoqiang <[email protected]>
         */
        public static void createFile(File file, boolean isFile) {        if (!file.exists()) {
                if (!file.getParentFile().exists()) {
                    createFile(file.getParentFile(), false);
                } else {
                    if (isFile) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        file.mkdir();
                    }
                }
            }
        }    /**
         * main
         * 
         * @param args
         * 
         * @author guoqiang <[email protected]>
         */
        public static void main(String[] args) {        // 源文件
            File initFile = new File("D:/test/1item.txt");        // 目标文件
            File targetFile = new File("D:/test2/1item.txt");        copyFile(targetFile, initFile);
        }
    }
      

  5.   

    关键点式获取web应用的绝对路径。
      

  6.   

    String projectAbsolutePath = System.getProperty("user.dir");
    这是获取当前工程路径的代码,工程中的webroot下的content文件夹路径是固定的,自己加上就OK了。
      

  7.   

    WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()).getServletContext().getRealPath("");我以前是用这个获取绝对路径的