在Spring MVC中的Web应用程序的web-inf文件夹中编写一个txt文件

时间:2016-05-03 20:03:43

标签: java spring spring-mvc file-io web-applications

我想在服务器(将部署.war文件的域)上创建一个.txt文件,并希望它通过下载链接下载。我可以使用user.home在我的PC桌面上创建并保存txt并获取绝对路径。

我使用ServletContext来获取上下文路径。当我尝试在该路径上保存文件时,错误说,java.io.IOException:系统找不到指定的路径。

现在有人可以告诉我该怎么做,这样当我部署.war文件时,我可以编写并下载该文件。

控制器:

@Autowired
ServletContext context;
@RequestMapping(value = "/calculate")
public String getDataQuote(ModelMap map, HttpSession session, @ModelAttribute("dataBean") DataBean dataBean) {
    Calculator calc = new Calculator();
    Quote quote = calc.calculate(dataBean);
    Writer writer = new Writer();
    writer.printCloudData(quote, context);
    map.addAttribute(quote);
    return "result";
}

文件编写者:

public void printData(Bean bean, ServletContext context) {

    try {
        String path = context.getRealPath("/WebContent/files/");
        System.out.println(path);
        path = path.replace("\\", "/");
        File file = new File(path + "/files", "abc.txt");
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(bean.a);
        bw.newLine();
        bw.newLine();
        bw.write(bean.b);
        bw.close();
        } catch (
            IOException e)
        {
            e.printStackTrace();
        }
}

每次我运行此控制器时,都会出现此错误:

java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.business.Writer.printQuotationData(Writer.java:32)
at com.controler.DefaultController.getQuote(DefaultController.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:2)

您指定的目录在服务器上不存在。所以异常发生了。您可以按如下方式在服务器上写文件:

文件编写者:

public void printData(Bean bean, ServletContext context) {

try {
            //if you want to create file outside web-inf directory.
            //String path = context.getRealPath("/");

          //if you want to create file under web-inf directory.
          String path = context.getRealPath("/WEB-INF");

            System.out.println(path);
            //path = path.replace("\\", "/");

            File file = new File(path, "files");

            if (!file.exists()) {
                file.mkdir();
            }

            file = new File(file, "abc.txt");
            if (!file.exists()) {
                file.createNewFile();
            }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(bean.a);
        bw.newLine();
        bw.newLine();
        bw.write(bean.b);
        bw.close();
        } catch (IOException e){
            e.printStackTrace();
        }
}