【问题标题】:Why doesn't this java post method controller receive this post form?为什么这个 java post 方法控制器没有收到这个 post 表单?
【发布时间】:2018-10-21 08:37:30
【问题描述】:

这是表格:

<form method="post"  enctype="multipart/form-data" id="uploadFileForm" action="/uploadTest">
     <div>
        <input type="file" id="image_uploads" name="filename" accept=".jpg, .jpeg, .png, .pdf" multiple>
    </div>
    <div id="uploadpreview">
        <p>No files currently selected for upload</p>
    </div>
    <div>
        <button id="uploadBtn" type="submit">Submit</button>
    </div>
</form>

这是控制器:

@Controller
@RequestMapping(value = "/**/uploadTest")
public class UserFileUploadController {

    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String TestRun(@ModelAttribute("filename") final File uploadFile)
    {
    //code in which i use the uploaded file
        return ("redirect:/solutions");
    }
}

我收到“405 Method Not Allowed”状态代码 = 屏幕显示“服务器错误”。 但是页面刷新到 /uploadTest 所以我不知道控制器出了什么问题。

【问题讨论】:

  • 请修正您问题中的代码格式。
  • 为什么要在路径中添加/**

标签: java spring spring-mvc forms http-post


【解决方案1】:

我认为你应该将File替换为MultipartFile,然后如果你想将文件存储在文件系统中,则调用transferTo方法。

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String TestRun(@ModelAttribute("filename") final MultipartFile uploadFile) throws IOException {
    File file = ...
    uploadFile.transferTo(file);
    return ("redirect:/solutions");
}

您还应该检查您是否在dispatcher-servler.xml 中定义了此bean,并将maxUploadSize 设置为正确的值。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1000000" />
</bean>

【讨论】:

    猜你喜欢
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多