将图像上传到 JavaEE 中的文件夹时如何修复以下 FileNotFoundException?

How to fix following FileNotFoundException while uploading image to a folder in JavaEE?

自己解决了-解决方案如下

我正在尝试将图像上传到服务器目录(不是数据库中的字节),因为我不想将图像存储到 MySQL。但是,我没有像我想要的那样上传图像并且在服务器日志中有 FileNotFoundException。我下面用于文件上传的 servlet 代码有什么问题。是导致问题的 getRealPath("") 吗?如果是,它的解决方案或替代方案是什么。如果这不是我现在得到的原因,请帮助我了解我的错误。 这是我的 servlet 版本的正确方法吗?我想我有 servlet api 3.0 或更高版本。如有错误请指出

我正在使用的平台 Netbeans 8.0.2 安装到 C:\Program Files Glassfish 4.1 解压到 G:\glassfish4\ 项目位置 E:\NetBeansProjects

这是我的 glassfish 服务器日志

java.io.FileNotFoundException: G:\glassfish4\glassfish\domains\domain1\generated\jsp\e-Shop\E:\NetBeansProjects\e-Shop\build\web\img\products\Untitled.png (The filename, directory name, or volume label syntax is incorrect)

这是我的 servlet 上传代码的一部分

else if (userPath.contains("/admin/uploadCategory")){

            String SAVE_DIR = "/img/categories";

            // gets absolute path of the web application
            String appPath = request.getServletContext().getRealPath("");
            // constructs path of the directory to save uploaded file
            String savePath = appPath + File.separator + SAVE_DIR;


            // creates the save directory if it does not exists
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdir();
        }

        for (Part part : request.getParts()) {
            String fileName = extractFileName(part);
            part.write(savePath + File.separator + fileName);
        }

        request.setAttribute("message", "Upload has been done successfully!");
        getServletContext().getRequestDispatcher("/admin/message.jsp").forward(
                request, response);

        }
        else if (userPath.contains("/admin/uploadProduct")){

            String SAVE_DIR = "/img/products";

            // gets absolute path of the web application
            String appPath = request.getServletContext().getRealPath("");
            // constructs path of the directory to save uploaded file
            String savePath = appPath + File.separator + SAVE_DIR;


            // creates the save directory if it does not exists
            File fileSaveDir = new File(savePath);
            if (!fileSaveDir.exists()) {
                fileSaveDir.mkdir();
            }

            for (Part part : request.getParts()) {
                String fileName = extractFileName(part);
                part.write(savePath + File.separator + fileName);
            }

        request.setAttribute("message", "Upload has been done successfully!");
        getServletContext().getRequestDispatcher("/admin/message.jsp").forward(
                request, response);

        }

自己这样解决的

else if (userPath.contains("/admin/uploadProduct")){
            String fileName="";
            String SAVE_DIR = "/img/products";

            try {
                Part filePart = request.getPart("file");
                fileName = getFileName(filePart);
                String applicationPath = request.getServletContext().getRealPath("");
                String basePath = applicationPath + File.separator + SAVE_DIR + File.separator;
                InputStream inputStream = null;
                OutputStream outputStream = null;
                try {
                    File outputFilePath = new File(basePath + fileName);
                    inputStream = filePart.getInputStream();
                    outputStream = new FileOutputStream(outputFilePath);
                    int read = 0;
                    final byte[] bytes = new byte[1024];
                    while ((read = inputStream.read(bytes)) != -1){
                        outputStream.write(bytes, 0, read);
                    }

                }catch (Exception e){
                e.toString();
                fileName = "";
                }finally {
                    if (outputStream != null){
                        outputStream.close();
                    }
                    if (inputStream != null){
                        inputStream.close();
                    }
                }

            }catch (Exception e){
                e.toString();
                fileName = "";
            }
            //return fileName;





        }