java.io.IOException:java.io.FileNotFoundException :(无此类文件或目录)

时间:2019-08-20 01:51:00

标签: java spring post postman

我目前正在从事spring项目。  我目前正在使用Postman测试文件以将其上传到server。问题是出现错误,指出该文件的路径不是,但是如果文件不在该路径中,我正在运行一个命令来创建文件。为什么找不到文件?

邮递员 postnam

控制器

    @RequestMapping(value="/v1/point/action/user/update_user_profile_image", method = RequestMethod.POST)
    public @ResponseBody Map<String, Object> update_user_profile_image(HttpServletRequest webRequest
            , @RequestParam(value="image", required=false) MultipartFile image
            ) {
        Map<String, String> param = new HashMap<String, String>();
        Map<String, Object> result = new HashMap<String, Object>();

        String seq_no = (webRequest.getParameter("seq_no") != null) ? webRequest.getParameter("seq_no") : "";

        Log.w("\r\n\r\n" +
                "***** API Calling - [ /v1/point/action/notice/update_user_profile_image ]********\r\n" +
                "-------------------------------------------------------------------------------\r\n" +
                " - seq_no              : [ " + seq_no + " ]                                    \r\n" +
                " - image               : [ " + image + " ]                                     \r\n" +
                "*******************************************************************************\r\n");




        try {

            // The image has been updated.
            String imagePath = "";
            if (image != null) {
                String Extension =  Config.USER_PROFILE_IMAGE_EXT;
                String fileName = "user_profile_" + Utils.getCurrentTime("yyyyMMddHHmmssSSS");
                imagePath = Define.CONTENTS_FILE_PATH_4 + fileName + Extension ;

                File saveDir = new File(Define.CONTENTS_SAVE_PATH + Define.CONTENTS_FILE_PATH_4);
                if (!saveDir.isFile()) saveDir.mkdirs();

                image.transferTo(new File(Define.CONTENTS_SAVE_PATH + imagePath));

                String fileName_thumbnail = fileName + "_thumb" + Extension;
                File thumbnail = new File(Define.CONTENTS_SAVE_PATH + Define.CONTENTS_FILE_PATH_4 + fileName_thumbnail);

                thumbnail.getParentFile().mkdirs();
                Thumbnails.of(saveDir + "/" + fileName + Extension).size(Config.USER_PROFILE_IMAGE_WIDTH, Config.USER_PROFILE_IMAGE_HEIGHT).outputFormat("jpg").toFile(thumbnail);
            }

            param.put("imagepath", (!imagePath.equals("")) ? Define.PLATFORM_SERVER_HOST_NAME + imagePath : "");
            param.put("seq_no", seq_no);

            if (backOfficeDaoSecond.update_user_profile_image(param) < 1) {
                result.put("resultCode", Define.ERROR_CODE_002);
                result.put("resultMsg", "Databases update fail..");

            } else {
                result.put("resultCode", Define.SUCCESS_CODE_000);
                result.put("resultMsg", "Shop successful in modifying product information.");
            }


        } catch (Exception e) {
            e.printStackTrace();
            result.put("resultCode", Define.ERROR_CODE_001);
            result.put("resultMsg", "[ERROR] Server error");
        }

        return result;

    }

错误:

java.io.IOException: java.io.FileNotFoundException: /mnt/platform/contents/member_profile_img/user_profile_20190820104359375.jpg (No such file or directory)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:122)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:255)
    at net.point.service.game.admin.backoffice.controller.BackOfficeDaoSecondController.update_user_profile_image(BackOfficeDaoSecondController.java:1441)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:142)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:901)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: /mnt/platform/contents/member_profile_img/user_profile_20190820104359375.jpg (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:400)
    at org.apache.catalina.core.ApplicationPart.write(ApplicationPart.java:120)

首先,问题发生在这里。

image.transferTo(new File(Define.CONTENTS_SAVE_PATH + imagePath));

预先感谢

2 个答案:

答案 0 :(得分:1)

我直接在该路径上创建了一个文件来解决此问题。错误消失了。我不知道为什么创建文件的命令不起作用,但是问题已得到解决。

答案 1 :(得分:-1)

嗨,当我遇到此问题时,我的第一步是检查我是否可以访问该服务器,我认为这是防火墙问题。

second,用于检查路径是否正确。