[Bug Collection] part.getSubmittedFileName() cannot be used

Upload files with @MultipartConfig annotation ( Tomcat8 and servlet 3.0 )

        Uploading files in JavaWeb can be said to be more troublesome. Before servlet3.0, uploading files was usually done with some jar packages. Among them, there are many related APIs, which is very inconvenient to write. After servlet3.0, some APIs have been improved. , You can easily submit files through @multipartConfig annotations and related methods.

Detailed explanation of the connection: https://blog.csdn.net/mytt_10566/article/details/71077154

protected void updateUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //1.获得请求数据
            //1.1 获得请求的map
            Map<String, String[]> map = req.getParameterMap();
            //1.2 创建即将被封装的user
            User user = new User();
            //1.3 封装数据
            BeanUtils.populate(user, map);
            // -------------------------------------------------------------------文件上传
            // 图片对象
            Part pic = req.getPart("pic");
            if (pic != null && pic.getSize() != 0) {//一定有文件上传了
                //1.获得文件的名称
                // 使用tomcat 8以及servlet3.0时使用下面方法获取文件名称
                String fileName = pic.getSubmittedFileName(); //文件名称
                //2.获得文件即将保存位置
                //获得web项目在本地发布的后路径
                String realPath = getServletContext().getRealPath("/img");
                System.out.println(fileName);
                //3.拿到文件的流
                InputStream is = pic.getInputStream();
                //4.输出文件
                FileOutputStream os = new FileOutputStream(realPath + "/" + fileName);
                IOUtils.copy(is, os);
                //5.释放资源
                is.close();
                os.close();
                //赋值的路径
                //user.setPic(realPath + "/" + fileName)
                //使用相对路径
                user.setPic("img/" + fileName);
            }
            //-------------------------------------------------------------------文件上传
            //2.处理数据
            UserService userService = new UserServiceImpl();
            //修改用户信息
            userService.updateUser(user);
            //数据库和缓存session数据不同步 需要再次查询数据 放入session替换
            User loginUser = (User) req.getSession().getAttribute("user");
            //再次查询 替换数据
            loginUser = userService.findByTelephone(loginUser.getTelephone());
            //替换session用户数据
            req.getSession().setAttribute("user", loginUser);
            //3.响应结果
            resp.sendRedirect(req.getContextPath() + "/home_index.jsp");
        } catch (Exception e) {
            e.printStackTrace()
        }
    }

Upload files with @MultipartConfig annotation ( Tomcat 7 )

In the environment of tomcat7, there is no part.getSubmittedFileName() method, and the file name cannot be obtained directly

solution:

Replace String fileName = pic.getSubmittedFileName( ); in the above code  with the following code to solve the problem.

String cd = part.getHeader("Content-Disposition");
//截取不同类型的文件需要自行判断
 String filename = cd.substring(cd.lastIndexOf("=")+2, cd.length()-1);

 

Guess you like

Origin blog.csdn.net/weixin_43267344/article/details/108955955