在没有multipart jsp的情况下调用Request.getPart

时间:2016-07-30 04:52:53

标签: java jsp

  

java.lang.IllegalStateException:在没有多部分配置的情况下调用Request.getPart。将@MultipartConfig添加到servlet,或将multipart-config元素添加到web.xml

当我尝试将图像添加到项目中时,我收到此错误

我的HTML是

<form action="../include/crud.jsp" method="POST">
    <input type='file' onchange="readURL(this);" style="display:none;" name="photo" />
</form>

我的crud.jsp是

<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ include file="dbconfig.jsp" %>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.swing.*" %>
<%@page 
    import="org.apache.commons.fileupload.FileItem"
    import="org.apache.commons.fileupload.FileUploadException"
    import="org.apache.commons.fileupload.disk.DiskFileItemFactory"
    import="org.apache.commons.fileupload.servlet.ServletFileUpload"


    %>

<% 

    Part filePart = request.getPart("photo"); // Retrieves <input type="file" name="file">
    String fileName = filePart.getSubmittedFileName();
    InputStream fileContent = filePart.getInputStream();

    try {
        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = null;



        String act = request.getParameter("btncrud");
        if (act == null) {
            //no button has been selected
        } else if (act.equals("Add")) {
            //Add button was pressed


            String add_disbursement = "INSERT INTO photo_table "
                                + "(ri_photo,ri_photo_name)"
                                + " VALUES (?,?)"; 

            preparedStatement.setBlob(1,fileContent);
            preparedStatement.setString(2,fileName);

            preparedStatement.executeUpdate();

        } else if (act.equals("Update")) {
            //update button was pressed

        } else {
            //someone has altered the HTML and sent a different value!

        }         
%>
  1. 如何摆脱错误
  2. 如何将数据库中的照片文件保存为中等blob
  3. FYI

    ri_photo的类型为mediumblob

    UPDATE

    已添加cos-multipart.jarimport=" com.oreilly.servlet.MultipartRequest"仍为同一错误

    如何在JSP页面中添加@MultipartConfig

1 个答案:

答案 0 :(得分:0)

只要请求正文大小大于默认值,就会发生此异常。

如果您想要自定义大小或大于默认大小,则需要覆盖web.xml中的multipart-config设置 你可以参考以下内容:

<multipart-config>
    <max-file-size>*size in bytes*</max-file-size>
    <max-request-size>*size in bytes*</max-request-size>
    <file-size-threshold>*size in bytes*</file-size-threshold>
</multipart-config>