使用jsf Primefaces将文件上传到网络根目录

uploading file to web root directory using jsf Primefaces

我正在尝试将客户徽标存储到 "src/main/webapp/clientImage/clientcode" clientcode目录将根据different-2 client创建。
例如,如果客户端代码是 TEST,那么完整路径将类似于 "src/main/webapp/clientImage/TEST",当我们上传客户端图像时,客户端徽标将位于 "TEST" 目录下。
所以上传客户端标志后(假设图像名称是"test.jpeg",那么完整目录将是"src/main/webapp/clientImage/TEST/test.jpeg"上传客户端标志的代码是::

`public boolean upload(UploadedFile uploadFile) {
String LOGO_PATH= "/clientImage/";
String fileName = uploadFile.getFileName();
String realPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(LOGO_PATH + selectedClient.getClientCode());
File f = new File(realPath);
if (!f.exists()) {
        f.mkdirs();
        logger.debug("Directory created : {}", f.getName());
}
try {
new UserUtils().copyFile(fileName, uploadFile.getInputstream(), realPath);
            selectedClient.setLogo(fileName);
saveClient();
FacesMessage msg = new FacesMessage("Success! ", uploadFile.getFileName() + " set as logo.");
FacesContext.getCurrentInstance().addMessage(null, msg);
return true;
} catch (IOException e) {
    logger.error("{}", e);
    return false;
}
}

`

问题:: 我的问题是当我为 TEST 客户端上传徽标时,我得到的 realPath 是 "C:\Users\narendra\tls_workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\iclock\clientImage\TEST"

realPath 应该是 "src/main/webapp/clientImage/TEST/test.jpeg" 并且 logo 应该存储到 TEST 目录中,但 logo 甚至没有进入该目录

您不是 运行 来源本身。你的源文件被编译成 类,然后你的 类 和资源被打包成一个 Web 应用程序存档 - 一个 WAR 文件。所以你的 realPath 不在 src 中,它在部署 WAR 的任何地方。在你的例子中,它在 Eclipse 的 tmp 文件中,因为你 运行 它来自 Eclipse。

将文件上传到源目录的想法是荒谬的,因为您的源目录不会部署到服务器 - 打包的 WAR 会。如果您出售您的 Web 应用程序,您可能不会将您的源代码发送给您的客户 - 您将向他们发送一个可执行文件,即 WAR 文件。

您也不应在 WAR 中保留用户数据。您的 WAR 在运行期间应该是不可变的。相反,您应该向您的服务器或 Java 询问临时目录或某种数据目录。例如。 Wildfly 服务器具有以下系统属性:

jboss.server.data.dir - directory the server will use for persistent data file storage jboss.server.temp.dir - directory the server will use for temporary file storage