java - isMultipartContent(请求)不起作用。我不知道发生了什么,它应该返回 true,但它返回 false

标签 java jsp

我正在使用此代码使用 jsp 在 Web 应用程序中上传文件(图像)。在此代码中 isMultipartContent(request) 应该返回 true,但它返回 false。我不明白出了什么问题。

<form class="form-contact contact_form" method="post" id="contactForm" novalidate="novalidate" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <input class="form-control" name="ff_ctype" id="ctype" type="text" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Course Type'" placeholder = 'Course Type'>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <input  name="cupload" id="cupload" type="file">
                            </div>
                        </div>
                    </div>
                    <div class="form-group mt-3">
                        <button type="submit" name="up_btn" value="submit" class="button button-contactForm btn_1">UPLOAD</button>
                    </div>
                </form>
<%
            String ctype="", cimage="", path="";
            boolean successful=true;
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
            if(isMultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List<FileItem> items = null;
                try {
                    items = upload.parseRequest(request);
                }
                catch (FileUploadException e) {
                    e.printStackTrace();
                }
                for(FileItem myitem:items) {
                    if (myitem.isFormField()) {
                        String itemName1 = myitem.getFieldName();
                        String value=myitem.getString();
                        if(itemName1.equals("ff_ctype")) //control's name - textbox name
                        {
                            ctype=value;
                        }
                    }
                    else {
                        String type=myitem.getContentType();
                        long size=myitem.getSize()/1024; //kbytes
                        if(size==0) {
                            cimage="default.png";
                        }
                        else if((type.equals("image/pjpeg") 
                                || type.equals("image/jpeg")
                                || type.equals("image/png") 
                                || type.equals("image/x-png")
                                || type.equals("image/gif")) 
                                && size<400)
                        {
                            cimage=new java.util.Date().getTime()+myitem.getName();
                            path=config.getServletContext().getRealPath("/") + "uploads\\" + cimage;
                            File savefile=new File(path);
                            myitem.write(savefile);
                        }
                        else {
                            successful=false;
                            out.print("Sorry only pictures of less than 400kb are allowed to upload");
                        }
                    }
                }
                if(successful==true) {
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        Connection MyConnection=DriverManager.getConnection(PATH + PLACE, USERNAME, PASSWORD);
                        try {
                            String q1="insert into coursetypes(ctype, cimage) values(?,?)";
                            PreparedStatement insertvalues=MyConnection.prepareStatement(q1);
                            insertvalues.setString(1, ctype);
                            insertvalues.setString(2, cimage);
                            if(insertvalues.executeUpdate()==1) {
                                out.print("Image Uploaded Successfully");
                            }
                            else {
                                out.print("Error occured");
                            }
                        }
                        catch(Exception e) {
                            out.print("Error in query due to: " + e.getMessage());
                        } 
                    }
                    catch(Exception e) {
                        out.print("Error in connection due to: "+e.getMessage());
                    }
                }
            }
        }
    %>

没有错误消息。

这段代码应该上传我项目中的 uploads 文件夹中的文件,但它的第一步出错了,即 isMultipartContent(request) 返回 false

最佳答案

我的错误是我使用了 form method='get'。但 multipart 不适用于 get 方法。因为据我所知,由于图像无法显示在地址栏上,所以这不起作用。这里显示的所有代码都可以完美运行。

错误的代码是:-

<form method="get">

正确的方法是:-

<form method="post">

此后,它工作得非常好。

关于java - isMultipartContent(请求)不起作用。我不知道发生了什么,它应该返回 true,但它返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58019471/

相关文章:

java - 应用结果集的获取大小会终止程序

java - 带 selectitem 的 richface 树(复选框)

java - 微调 Java 同步块(synchronized block)行为

java - 设计模式 - 我应该在哪里操作从数据库检索的字符串。离它更近还是更远

java - 在 Java 中创建可下载文件并存储在 Web 应用程序子目录中

java - 输出流问题

java - 如何在 Struts JSP 中过滤下拉选项

json - 如何使用tomcat服务器使json在jsp页面中工作?

java - 如何调用 "toString"的默认实现?

java - 如何使用 lambda 从列表中找到最大数字及其出现次数?