Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在webapp中创建文件夹并在其中保存图像_Java_Tomcat_Primefaces - Fatal编程技术网

Java 在webapp中创建文件夹并在其中保存图像

Java 在webapp中创建文件夹并在其中保存图像,java,tomcat,primefaces,Java,Tomcat,Primefaces,朋友们好, 我有以下代码将图像上载到文件夹WebContent-->images-->MenuItemg中的WebApp private String doUploadFile(String menuItemName,Long menuItemId){ FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesC

朋友们好,

我有以下代码将图像上载到文件夹WebContent-->images-->MenuItemg中的WebApp

    private String doUploadFile(String menuItemName,Long menuItemId){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        InputStream inputStream = null;
        OutputStream out = null;
        String imageName = null;
        try{
            String dirPath = externalContext.getRealPath(File.separator+"images"+File.separator+"menuitemimg");
            File targetFolder = new File(dirPath);
            targetFolder.mkdirs();
            imageName = file.getFileName();
            imageName = menuItemName+"_"+menuItemId+"."+FilenameUtils.getExtension(imageName);
            inputStream = file.getInputstream();
            out = new FileOutputStream(new File(targetFolder+File.separator+imageName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            inputStream.close();
            out.flush();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return imageName;
}
代码运行良好。但当我重新启动服务器时,上传到MenuItemg文件夹中的图像就会消失。我不想将图像存储在光盘或数据库中。那我该怎么办呢


提前感谢。

我仔细阅读了您的代码,您似乎正在项目文件夹中存储图像。
我建议您将图像上传到容器外部的其他地方

您不应该将图像保存在web应用程序中,因为这更像是一个临时文件夹,每次上载新WAR文件时都会刷新。或者保存到其他文件夹(WAR外部)或者数据库。谢谢你的重播。我会听从你的建议。