如何修复java.nio.file.NoSuchFileException:C:\ Users \ profilepics \ default.jpg

时间:2017-10-05 13:18:40

标签: java spring file-upload glassfish

我的glassfish服务器出错了。说我的文件夹/文件不存在。我复制并粘贴了此处所述的路径,图像就在那里。我在这里向我的JSP显示图像。

    [2017-10-05T20:39:38.389+0800] [glassfish 4.1] [WARNING] [] 
    [javax.enterprise.web] [tid: _ThreadID=28 _ThreadName=http-listener-1(3)] 
    [timeMillis: 1507207178389] [levelValue: 900] [[
    StandardWrapperValve[spring]: Servlet.service() for servlet spring threw 
    exception
    java.nio.file.NoSuchFileException: C:\Users\Adminitrator-
    IT\Desktop\default.jpg
    at 
    sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at 
    sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at 
    sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at 
sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
    at java.nio.file.Files.newByteChannel(Files.java:317)
    at java.nio.file.Files.newByteChannel(Files.java:363)
    at 
java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:380)
    at java.nio.file.Files.newInputStream(Files.java:108)
    at java.nio.file.Files.copy(Files.java:2895)
    at com.project.controller.UploadController.readImage(UploadController.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

我绝对是初学者。我使用的是Spring和glassfish 4.这是我控制器中的代码,

 @RequestMapping(value = "/getavatar/{value}", method = RequestMethod.GET)
        public void readImage(HttpServletRequest request, HttpServletResponse response,@PathVariable String value)
                throws ServletException, IOException {
            //int cifid = authenticationFacade.getSessionData().getCifid(); 

            ServletContext sc = request.getServletContext();



            File image = new File(com.aimglobalinc.constants.AimConstants.AVATAR_FOLDER + value + "_avatar.jpg");

            if(!image.exists()){
                image = new File(com.aimglobalinc.constants.AimConstants.AVATAR_FOLDER + "default.jpg");
            } 

            String contentType = sc.getMimeType(image.getName());
            response.reset();
            response.setContentType(contentType);
            response.setHeader("Content-Length", String.valueOf(image.length()));
            Files.copy(image.toPath(), response.getOutputStream());
        }

0 个答案:

没有答案