如何在struts 2应用程序文件夹中存储文件?

时间:2012-12-17 05:10:35

标签: java-ee file-upload struts2

我正在使用此链接 file upload进行文件上传。但它给了我临时位置的文件。但我想存储在我的应用程序文件夹中。请分享您对此的看法。

1 个答案:

答案 0 :(得分:0)

使用struts fileupload标记

<s:file name="fileUpload" id="fileUpload" />

在动作类

    File fileUpload;
    String fileUploadContentType;
    String fileUploadFileName;
    HttpServletRequest servletRequest;

编写此方法在webcontent中创建/ Upload / Document文件夹

private String saveDocumentDoc()
    {
        String filePath = servletRequest.getSession().getServletContext().getRealPath("/Upload/Document/");

    System.out.println("Server Path "+filePath);

    Date d = new Date();
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Document");
    String s=stringBuilder.toString();
    File fileToCreate = new File(filePath, s);  

    try {
        FileUtils.copyFile(this.fileUpload, fileToCreate);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return s;
}

希望它有助于享受