SpringMVC上传文件参数传递错误?

java类代码(部分):

@RequestMapping(value= "/doUpload", method = RequestMethod.POST)
    public String uploadFileHandler(HttpServletRequest request,Model model,
            @RequestParam("file") MultipartFile[] files)
    {
        //Root Directory
        String uploadRootPath = request.getServletContext().getRealPath("upload");
        System.out.println("uploadRootPath=" + uploadRootPath);
        File uploadRootDir = new File(uploadRootPath);
        if(!uploadRootDir.exists())//Creat if it not exists
        {
            uploadRootDir.mkdirs();
        }

        List<File> uploadedFiles = new ArrayList<File>();
        for (int i=0;i<files.length;i++)
        {
            MultipartFile file = files[i];
            String name = file.getOriginalFilename();
            System.out.println("Client File Name = " + name);
            if(name != null && name.length() > 0)
            {
                try
                {
                    byte[] bytes = file.getBytes();
                    File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);
                    BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
                    stream.write(bytes);
                    stream.close();
                    uploadedFiles.add(serverFile);
                    System.out.println("Write file: "+ serverFile);
                }
                catch(Exception e)
                {
                    System.out.println("Error Write file : "+ name);
                }
            }
        }
        model.addAttribute("uploadedFiles", uploadedFiles);
        return "uploadResult";
    } 
jsp文件:
    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Upload Multi File</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <h2>Upload Multiple File:</h2>
    <form method="POST" action="doUpload" enctype="multipart/form-data">
        File to upload (1): <input type="file" name="file"><br />
        File to upload (2): <input type="file" name="file"><br />
        File to upload (3): <input type="file" name="file"><br />
        File to upload (4): <input type="file" name="file"><br />
        File to upload (5): <input type="file" name="file"><br />

        <input type="submit" value="Upload">
    </form>
  </body>
</html>

错误信息:
HTTP Status 400 - Required MultipartFile[] parameter 'file' is not present

type: Status report

message: Required MultipartFile[] parameter 'file' is not present

description: The request sent by the client was syntactically incorrect.

初学者,百思不得其解

3个回答

问题原因是参数不对,前台传过来的不是数组,而你用数组接收,试试,这样写:

 MultipartHttpServletRequest multiRequest = (HttpServletRequest)request;
 List<MultipartFile> files = multiRequest.getFiles(paramName);
qq_23039605
小与哥 回复xxjkdtx: 那是什么问题,我也长长见识。
3 年多之前 回复
xxjkdtx
xxjkdtx 不是这个问题,xml写错了,现已解决,感谢回答
3 年多之前 回复

传的参数不对。。。你可以试试去掉"file",将的name改成files

去掉requestParam注解,把files改为file试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问