【问题标题】:Write a txt file in web-inf folder of a web application in Spring MVC在 Spring MVC 中的 web 应用程序的 web-inf 文件夹中写入一个 txt 文件
【发布时间】:2016-05-03 20:03:43
【问题描述】:

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

我已经使用 ServletContext 来获取上下文路径。当我尝试在该路径上保存文件时,错误提示为 java.io.IOException: The system cannot find the path specified.

现在有人可以告诉我怎么做,这样当我部署 .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)

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

【问题讨论】:

  • 不能写入war文件;您可能应该重新考虑您的方法。

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


【解决方案1】:

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

文件编写器:

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();
        }
}

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 2021-09-11
    • 1970-01-01
    • 2014-09-08
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多