0

我想用.jsp和servlet的图像上传mysql数据库中囤积的详细信息,但是当我调用我的servlet时,它给出了一个空白页..

我的.jsp

<form action="HoardingProfile" method="post" enctype="multipart/form-data">
    <table style="height: 100%; width:100%;">
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Location</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" name="txtLocation"
                    placeholder="Location" required="yes">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Size</h3>
            </td>
            <td class="padd10" style="width: 50%;">
                <input class="tx bk" type="text" name="txtSize" placeholder="Size"
                    required="yes">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Owner Name</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" placeholder="Owner Name"
                    required="yes" name="txtName">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Contact No.</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" placeholder="Number"
                    required="yes" name="txtNumber">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Address</h3>
            </td>
            <td style="width: 50%;">
                <textarea class="tx bk2" placeholder="Address" required="yes"
                    name="txtAddress"></textarea>
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 15%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Picture</h3>
            </td>
            <td style="width: 50%;">
                <input type="file" name="file" id="file">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Google map</h3>
            </td>
            <td style="width: 50%;">
                <textarea class="tx bk2" placeholder="Google Map iframe"
                    name="txtMap"></textarea>
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 80%;">
                <input class="btn" type="submit" name="Add" value="Save"
            </td>
            <td style="width: 20%;">
                <input class="btn" type="reset" value="Reset"
            </td>
        </tr>
    </table>
</form> 

和我的 servlet

 PrintWriter out = response.getWriter();
    try {
        // Apache Commons-Fileupload library classes
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu  = new ServletFileUpload(factory);

        if (! ServletFileUpload.isMultipartContent(request)) {
            System.out.println("Sorry. No file uploaded");
            return;
        }

        // parse request
        List<FileItem> items = sfu.parseRequest(request);
        out.print(items);
        FileItem  txtLocation = (FileItem) items.get(0);
        String location =  txtLocation.getString();
        out.println(location);
        FileItem txtSize = (FileItem) items.get(1);
        String   size =  txtSize.getString();
        out.println(size);
        FileItem txtName = (FileItem) items.get(2);
        String   name =  txtName.getString();
        out.println(name);
        FileItem txtNumber = (FileItem) items.get(3);
        String   number =  txtNumber.getString();
        out.println(number);
        FileItem txtAddress = (FileItem) items.get(4);
        String   address =  txtAddress.getString();
        out.println(address);
        // get uploaded file
        FileItem file = (FileItem) items.get(5);

        // Connect to Oracle
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/opm?zeroDateTimeBehavior=convertToNull","root","");
        con.setAutoCommit(false);

        PreparedStatement ps = con.prepareStatement("insert into hprofile(location,size,owner,address,phone,deleted,img) values(?,?,?,?,?,?,?)");
        ps.setString(1, location);
        ps.setString(2, size);
        ps.setString(3, name);
        ps.setString(4, number);
        ps.setString(5, address);
        // size must be converted to int otherwise it results in error
        ps.setBinaryStream(6, file.getInputStream(), (int) file.getSize());
        ps.executeUpdate();
        con.commit();
        con.close();
        out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");
    }
    catch(Exception ex) {
        out.println( "Error --> " + ex.getMessage());
    }
     finally {            
        out.close();
    }
4

1 回答 1

0

我发现我在链接库时犯了一些错误......

于 2013-06-15T05:06:59.760 回答