tomcat服务器上的Java应用程序在服务器而不是路径上保存文件

时间:2015-09-29 15:51:52

标签: java file tomcat io

我目前在Tomcat 7服务器上有一个应用程序Java Web应用程序,它应该生成文件并将它们保存在用户指定的路径上。当我在我的本地计算机上测试它时工作正常但是当我部署到开发服务器时,文件将保存在以下路径中" conf / Catalina / localhost"在具有指定路径的服务器上作为名称的一部分。

例如,如果我将目录设置为我希望文件保存到的位置" C:\ Users \ kbbj \ Desktop",文件将作为" C:\保存在服务器上用户\ kbbj \桌面\ FILENAME.EXT"

这是我创建Word文件的代码(注意它保存的最后一部分):

public static void createWordDoc(Request request) throws Docx4JException {
        String filename = request.study+"_data_specs_v"+request.currentVersion+".docx";

        wordMLPackage = WordprocessingMLPackage.createPackage();

        //Create titles
        String title = request.study+" Data Specifications v."+request.currentVersion;
        String subtitle = request.analysis.analysisType+" Analysis for "+request.purpose.purpose;

        // Add titles to document
        wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title",title);
        wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle",subtitle);
        wordMLPackage.getMainDocumentPart().addParagraphOfText("");
        wordMLPackage.getMainDocumentPart().addParagraphOfText("");

        //Create specification table
        wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading2", "Specifications");
        factory = Context.getWmlObjectFactory();

        Tbl table = factory.createTbl();
        Tr tableRow = factory.createTr();

        addStyledTableCell(tableRow, "Variable Name",true,null);
        addStyledTableCell(tableRow, "Values",true,null);
        addStyledTableCell(tableRow, "Method",true,null);
        addStyledTableCell(tableRow, "Analyst Instructions",true,null);
        table.getContent().add(tableRow);

        System.out.println("Number of specs: "+request.specs.size());
        //Add specs to table
        for(Specification spec:request.specs){
            Tr newRow = factory.createTr();
            addTableCell(newRow, spec.variable.name!=null ? spec.variable.name : "");
            addTableCell(newRow, spec.variable.codeList!=null ? spec.variable.getCodeListAsString():"");
            addTableCell(newRow, spec.method!=null ? spec.method.method:"");
            addTableCell(newRow, spec.instructions!=null ? spec.instructions:"");
            table.getContent().add(newRow);
        }


        wordMLPackage.getMainDocumentPart().addObject(table);

        //Save the file on project directory
        String filepath = request.directory+"\\"+filename;
        System.out.println("file path: "+filepath);
        wordMLPackage.save(new java.io.File(filepath));
    }

有关正在发生的事情的任何想法? 感谢帮助!

1 个答案:

答案 0 :(得分:0)

不可能有两个原因:

  1. 安全性,你用来启动tomcat服务的用户没有访问Catalina / localhost以外的权限所以你不能创建文件外侧文件夹有权限,并且不建议用root运行tomcat服务“如果可能的话“
  2. 操作系统不同,如果您在Linux上托管您的应用程序,那么您根本没有c:驱动器
  3. 建议您可以创建相对路径并使用File.mkdirs()创建嵌套文件夹