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

时间:2015-04-06 19:54:10

标签: jsp java-ee servlets netbeans glassfish

我自己解决了 - 解决方案低于

我正在尝试将图像上传到服务器目录(而不是db中的字节),因为我不想将图像存储到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);

        }

1 个答案:

答案 0 :(得分:1)

这样解决了这个问题

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;





        }