如何从jsp上传文件?

时间:2015-03-03 21:59:47

标签: java-ee file-upload

我有简单的J2ee应用程序,我想上传一个图像文件。所以我有这个代码:

page.jsp

<form action="javascript:saveAnnuncio();" method="POST" enctype="multipart/form-data">
                <label>Titolo <span class="color-red">*</span></label>
                <input type="text" class="form-control margin-bottom-20" name="titolo" id="titolo" required></input>        

                <label>Nome <span class="color-red">*</span></label>
                <input type="text" class="form-control margin-bottom-20" 
                    name="nome" id="nome" value="<c:out value="${USER.nome}"/>" 
                    required readonly/>

                <label>Email <span class="color-red">*</span></label>
                <input type="text" class="form-control margin-bottom-20" 
                    name="email" id="email" value="<c:out value="${USER.email}"/>" 
                    readonly required/>

                <label>Descrizione <span class="color-red">*</span></label>
                <textarea type="text" class="form-control margin-bottom-20" name="descrizione" id="descrizione" required></textarea>
                Select file to upload: <input type="file" name="selectFile" />   
                <button type="submit" class="btn btn-primary">Inserisci</button>
                <button type="reset" class="btn btn-primary" >Resetta</button>
            </form>

这是javascript函数:

function saveAnnuncio(){

                try{
                    remove_old_errors();
                    var titolo = document.getElementById("titolo").value;
                    var nome = document.getElementById("nome").value;
                    var email = document.getElementById("email").value;
                    var descrizione = document.getElementById("descrizione").value;

                    var ajaxHandler = new AjaxHandler('POST', '<c:url value="${pageName}"/>.html', false);   
                    ajaxHandler.encodeParameter('method', 'saveItems');
                    ajaxHandler.encodeParameter('titolo', titolo);
                    ajaxHandler.encodeParameter('nome', nome);
                    ajaxHandler.encodeParameter('email', email);
                    ajaxHandler.encodeParameter('descrizione', descrizione);
                    ajaxHandler.callbackHandler = function()
                    {           
                        if (ajaxHandler.getRequest().readyState == 4)
                        {   
                            if (ajaxHandler.getRequest().status == 200)
                            {
                                var xmlDoc = ajaxHandler.getRequest().responseXML;

                                var itemsException = xmlDoc.getElementsByTagName("exception");
                                var items = xmlDoc.getElementsByTagName("registrazioneOK");
                                var itemsEmail = xmlDoc.getElementsByTagName("emailEsistente");
                                if (itemsException.length!=0)
                                {
                                    var punto=itemsException[0];
                                    alert(punto.getAttribute("text"));
                                    document.getElementById("error").innerHTML=punto.getAttribute("text");
                                    alert('Impossibile1 eseguire l\' operazione');
                                }
                                if (items.length!=0)
                                {
                                    var punto=items[0];
                                    alert(punto.getAttribute("text"));
                                    window.location.href = "home.html";
                                }

                                if (itemsEmail.length!=0)
                                {
                                    var punto=itemsEmail[0];
                                    //alert(punto.getAttribute("text"));
                                    append_errors(document.getElementById("email"),punto.getAttribute("text"));
                                }
                            }
                        }
                    };

                    ajaxHandler.startRequest();   
                }
                catch (e)
                {
                    alert('Impossibile eseguire l\' operazione');
                }      
            }         

这是控制器的方法:

public ModelAndView saveItems(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        // Check that we have a file upload request  
          boolean isMultipart = ServletFileUpload.isMultipartContent(request);  

        try
        {
        } catch (Exception e) {
            LoggerFactory.logStackTrace(e);
        }
        return new ModelAndView("secure/ajax_callback", model);
        //return loadPage(request, response);
    }

但是这种方法不起作用,因为字段isMultipart就是一切都是假的。

0 个答案:

没有答案