isMultipartContent(request)无法正常工作。我不知道发生了什么,它应该返回true,但是返回false

时间:2019-09-19 22:09:17

标签: 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

1 个答案:

答案 0 :(得分:0)

我的错误是我使用method ='get'形式。但是multipart不适用于get方法。因为据我所知,由于图像无法显示在地址栏上,因此无法正常工作。此处显示的所有代码都可以正常运行。

错误的代码是:-

<form method="get">

正确的方法是:-

<form method="post">

此后,它运行正常。