springboot+layui:请求上传接口异常 FileNotFoundException:MultipartFile file

文件上传时,chrome与IE/Edge对于originalFilename处理方式不同
chrome会获取到该文件的直接文件名称,IE/Edge会获取到文件上传时完整路径/文件名

 @RequestMapping(value="/uploadSource" , method = RequestMethod.POST)
    @ResponseBody
    public String uploadSource(@RequestParam("file") MultipartFile file , HttpServletRequest request) {
     
        System.out.println(file);
        String pathString = null;
        if(file!=null) {
     
        //获取上传的文件名称
        String filename = file.getOriginalFilename();
        //不同浏览器
        //Check for Unix-style path
        int unixSep = filename.lastIndexOf('/');
        //Check for Windows-style path
        int winSep = filename.lastIndexOf('\\');
        //cut off at latest possible point
        int pos = (winSep > unixSep ? winSep:unixSep);
        if (pos != -1)
            filename = filename.substring(pos + 1);
            pathString = "E:/upload/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" +filename;
        }
        try {
     
            File files=new File(pathString);//在内存中创建File文件映射对象
            //打印查看上传路径
            System.out.println(pathString);
            if(!files.getParentFile().exists()){
     //判断映射文件的父文件是否真实存在
                files.getParentFile().mkdirs();//创建所有父文件夹
            }
            file.transferTo(files);//采用file.transferTo 来保存上传的文件
        } catch (Exception e) {
     
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "{\"code\":0,\"msg\":\""+pathString+"\"}";
    }

如果上传文件dianshiju.txt,则处理后的filename = dianshiju.txt
file-pathString

你可能感兴趣的:(Java,java)