Pratish Mishra
Pratish Mishra

Reputation: 25

Write a txt file in web-inf folder of a web application in Spring MVC

I want to create a .txt file on the server(of the domain on which the .war file will be deployed) and want it to download it via a download link. I can create and save the txt on my PC's desktop using user.home and getting the absolute path.

I have used ServletContext to get the context path. When I try to save file on that path, the error says, java.io.IOException: The system cannot find the path specified.

Now can someone tell me how to do it so that when I deploy the .war file I can write and download the file.

Controller:

@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";
}

File Writer:

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

Everytime I run this controller, it gives this error:

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)

any help would be appreciated. Thanks.

Upvotes: 2

Views: 3316

Answers (1)

CrawlingKid
CrawlingKid

Reputation: 994

The directory you were specifying didnt exist on server. So the exception happens. You could write file on server as follows:

File Writer:

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

Upvotes: 4

Related Questions