使用带有spring mvc的jquery上传文件时,为什么会出现Bad Request类错误?

时间:2016-08-20 11:22:28

标签: java jquery spring-mvc

当我在弹簧MVC中使用Jquery时,我在浏览器端出现错误“Bad Request”并且控制没有进入控制器。虽然我使用简单的表单并向同一个控制器发送请求然后它正在运行。 以下是我的代码,请告诉我哪里出错了?

    >>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\%USER%\Miniconda\lib\site-packages\numpy\__init__.py", li
ne 180, in <module>
    from . import add_newdocs
  File "C:\Users\%USER%\Miniconda\lib\site-packages\numpy\add_newdocs.py",
 line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\%USER%\Miniconda\lib\site-packages\numpy\lib\__init__.py"
, line 8, in <module>
    from .type_check import *
  File "C:\Users\%USER%\Miniconda\lib\site-packages\numpy\lib\type_check.p
y", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Users\%USER%\Miniconda\lib\site-packages\numpy\core\__init__.py
", line 14, in <module>
    from . import multiarray
ImportError: DLL load failed: The specified module could not be found.

我在下面给出的弹簧映射中的Controller类

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

 <script src="files/jquery-1.10.2.js"></script>
    <script src="files/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

var isJpg = function(name) {
    return name.match(/jpg$/i)
};

var isPng = function(name) {
    return name.match(/png$/i)
};

$(document).ready(function() {
    var file = $('[name="file"]');
    var imgContainer = $('#imgContainer');

    $('#btnUpload').on('click', function() {
        var filename = $.trim(file.val());

        if (!(isJpg(filename) || isPng(filename))) {
            alert('Please browse a JPG/PNG file to upload ...');
            return;
        }

        $.ajax({
            url: 'FileData.htm',
            type: "POST",
            data: new FormData(document.getElementById("fileForm")),
            enctype: 'multipart/form-data',
            processData: false,
            contentType: false
          }).done(function(data) {
              imgContainer.html('');
              var img = '<img src="data:' + data.contenttype + ';base64,'
                  + data.base64 + '"/>';

              imgContainer.append(img);
          }).fail(function(jqXHR, textStatus) {
              //alert(jqXHR.responseText);
              alert('File upload failed ...');
          });

    });

    $('#btnClear').on('click', function() {
        imgContainer.html('');
        file.val('');
    });
});

</script>

</head>
<body>
 <!-- <form name="dlgContent" action="FileData.htm" id="dlgcon"  enctype="multipart/form-data" method="POST">
 <input type="file" name="excelfile"/>
 <input type="submit"/>
 </form> -->


 <div>
<form id="fileForm">
    <input type="file" name="file" />
    <button id="btnUpload" type="button">Upload file</button>
    <button id="btnClear" type="button">Clear</button>
</form>
<div id="imgContainer"></div>
</div>


</body>
</html>

感谢。

1 个答案:

答案 0 :(得分:0)

实际上你发送的是Json而不是html,你应该使用@ResponseBody

    @RequestMapping(value="/upload", method = RequestMethod.POST)
public @ResponseBody
FileData upload(MultipartHttpServletRequest request,
                            @RequestParam String albumName,
                             HttpServletResponse response) {
  Iterator<String> itr =  request.getFileNames();

    //others code here

也别忘了!!配置多部分数据, 再加上使用jackson lib发送回来的对象到jquery完成的功能才能工作 Gson lib不适合与@ResponseBody一起使用,我们正在使用Gson和RestTemplate。