org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

时间:2017-07-01 06:37:47

标签: mysql hibernate spring-mvc struts1

我尝试从商品详情页面添加图片。图像添加成功,但当我尝试在itemdetails页面上显示图像时,我得到了异常org.springframework.web.multipart.MultipartException: Current request is not a multipart request

JSP:

<div class="panel panel-info">
    <%-- <form:form method="POST" name="moduleForm1" modelAttribute="DocumentEntity"> --%>
    <input type="hidden" name="selectedId3" value="${DocumentEntity.documentSeq}"  />
    <div class="panel-heading clearfix">
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
            <h4 class="pull-left">Item Images</h4>
        </div>
        <div class="col-lg-1 col-md-1 col-sm-12 col-xs-12">
             <h4 class="pull-left">Select File</h4>
         </div>
         <div class="col-lg-3 col-md-3 col-sm-12 col-xs-12">
         <%-- <html:file name="ItemForm" property="desc" styleClass="form-control"></html:file> --%>
         <input type="file" name="file" id="file" enctype="multipart/form-data"/>
    </div>

AttachmentService:

    package com.groups.attachements;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    import org.hibernate.Query;
    import org.hibernate.Session;

    import com.groups.GroupsConstants;
    import com.groups.GroupsCustomeException;
    import com.groups.GroupsFileManager;
    import com.groups.GroupsObjectImpl;
    import com.groups.GroupsUtils;
    import com.groups.dao.DropDownListBean;
    import com.groups.dao.GroupsBaseAccess;
    import com.groups.dao.GroupsDAOContext;
    import com.groups.dao.GroupsDAOContextFactory;
    import com.groups.spring.annotations.GroupsRequest;

    @GroupsRequest
    public class AttachmentServiceImpl extends GroupsObjectImpl implements
            AttachmentService
    {
        String appID = "IMS";

        String sponsorID;

        long sponsorSeq;

        String moduleID;

        DocumentEntity documentEntity;

        public static final String ITEM_ENTITY = "ITEM";

        public AttachmentServiceImpl()
        {
            appID = GroupsConstants.getApplicationDependable().getAppID();

        }

        @Override
        public Long createAttachment(InputStream is, String moduleType,
                String moduleRecordSeq, String createUser, String fileName)
                throws IOException, Exception
        {

            ensureSponsorIDVal();

            String homeDir = GroupsConstants.getApplicationDependable()
                    .getAttachmentsDir(sponsorID);

            String attachFileDir = moduleType + File.separator + moduleRecordSeq;

            homeDir = homeDir + File.separator + attachFileDir;

            File parentDir = new File(homeDir);

            GroupsFileManager.createDir(parentDir.getAbsolutePath());

            File uploadingFile = new File(parentDir, fileName);

            FileOutputStream fos = null;

            try
            {

                fos = new FileOutputStream(uploadingFile);

                GroupsFileManager.write(is, fos);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (fos != null)
                {
                    fos.close();
                }
            }



            DocumentEntity ae = new DocumentEntity();

            int dcid = GroupsUtils.generateUniqueId();

            ae.setFileName(fileName);

            String extension = "";

            int i = fileName.lastIndexOf('.');
            int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));

            if (i > p)
            {
                extension = fileName.substring(i + 1);
            }

            String docid = ("FS" + dcid);

            String documentId = ("FS" + dcid + "." + extension);

            ae.setDocumentId(documentId);

            String path = attachFileDir + File.separator + docid;

            ae.setModuleRecordSeq(moduleRecordSeq);
            ae.setModuleType("ITEM_ENTITY");

            ae.setAppID(appID);

            ae.setVersionNo(1);

            ae.setFileSystem("FS");

            ae.setPath(path);

            // String sponsorSeq =
            // nullToEmptyString(DocumentEntity.getSponsorSeq());

            ae.setSponsorSeq(sponsorSeq);

            ae.setCreateUser(createUser);
            // Date createDate=now();
            ae.setCreateDate(new Date());

            Session s = getSessionHibernate();
            s.save(ae);
            s.flush();

            return ae.getDocumentSeq();





        }

        private String ensureSponsorIDVal() throws SQLException
        {
            sponsorSeq = GroupsUtils.getLong(GroupsUtils.getSponsorSeq());

            if (sponsorID == null)
                sponsorID = GroupsUtils.getSponsorID(sponsorSeq + "",
                        getConnection());

            return sponsorID;

        }

        @Override
        public void createAttachment(File file, String moduleType,
                String moduleRecordSeq, String createUser, String fileName)
                throws IOException, Exception
        {
            InputStream is = null;

            try
            {
                if (file != null)
                    is = new FileInputStream(file);

                createAttachment(is, moduleType, moduleRecordSeq, createUser,
                        fileName);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                throw ex;
            }
            finally
            {
                if (is != null)
                    is.close();
            }

        }

        @Override
        public List<DocumentEntity> getAttachments(String moduleType,
                String moduleRecordSeq) throws IOException, Exception
        {
            String query = " from DocumentEntity where moduleType='" + moduleType
                    + "'and moduleRecordSeq='" + moduleRecordSeq
                    + "'and sponsor_seq='" + sponsorSeq + "' ";
            Query q = getSessionHibernate().createQuery(query);

            List<DocumentEntity> documentEntityBeans = q.list();

            return documentEntityBeans;
        }

        @Override
        public AttachmentEntity getAttachmentEntity(Long docSeq)
        {
            return null;
        }

        @Override
        public InputStream getAttachmentStream(long docSeq) throws IOException,
                Exception
        {

            return null;
        }

        @Override
        public List<AttachmentEntity> getAttachmentEntity(String moduleType,
                String moduleSeq)
        {

            return null;
        }

    }

控制器:

    @Autowired
        AttachmentService attachmentService;

        @RequestMapping("/ItemDetailImageAdd.htm")
        public ModelAndView itemDetailImageAdd(@RequestParam("file")
        MultipartFile file, @RequestParam(value = "selectedId2", required = false)
        String itemSeq) throws Exception
        {

            InputStream is = file.getInputStream();
            String fileName = file.getOriginalFilename();

            if (fileName.length() == 0)
            {
                // gce.add("error.importstudents.file.required");

                throw new GroupsCustomeException(
                        "error.image.required");
            }

的struts-config:

 <action path="/ItemDetailsSubmit"
                type="com.groups.hms.items.action.ItemDetailsSubmitAction"
                name="ItemForm"
                scope="session"
                validate="true"
                input="/pages/items/ItemDetails.jsp">     

            <exception
                key="error.general"
                className="com.groups.GroupsExceptionConfig"
                type="com.groups.GroupsCustomeException"
                handler = "com.groups.GroupsExceptionHandler" />

           <forward name="viewDetails" path="/ViewItemDetails.do"/>
     </action> 

1 个答案:

答案 0 :(得分:0)

您的<form>应包含enctype="multipart/form-data"属性。您目前在输入中设置了该组:

<input type="file" name="file" id="file" enctype="multipart/form-data"/>

哪个不对。供参考,请参阅:

https://www.w3schools.com/tags/att_form_enctype.asp https://www.w3.org/TR/html5/forms.html#attr-fs-formenctype