2015-04-06 32 views
-2

解决它自己 - 解决方案低于上传图片到JavaEE文件夹时如何修复以下FileNotFoundException?

我想上传图片到服务器目录(而不是在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); 

     } 
+0

不知你有没有'g'在路径的'e'驱动器。显然它只能是其中之一。 – developerwjk

+0

服务器安装在G驱动器中。项目目录位于E驱动器上。我不认为是这样。 – robinleathal

+0

可能是这种情况,但没有一个路径可以包含两个驱动器,所以情况就是这样。 – developerwjk

回答

0

解决它自己这样

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; 





     } 
相关问题