You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC文件上传JSP与Controller功能异常求助

Troubleshooting Your Spring JSP File Upload Issue

Let’s break down the potential problems with your file upload setup, starting with the most obvious fixes that could be causing your functionality to fail.

1. Critical Typo in JSP URL Variable

First, I spotted a typo in your JSP code that’s almost certainly breaking your form submission:

<spring:url value="/doUpload" var="doUplodaURL" />
<form:form method="post" modelAttribute="formUpload" action="${doUplodaURL }" enctype="multipart/form-data" >

You named the variable doUplodaURL (missing a 'd' in "Upload") instead of doUploadURL. This means your form is trying to submit to an invalid path, which would result in a 404 error or no action at all. Fix the variable name to match the correct endpoint:

<spring:url value="/doUpload" var="doUploadURL" />
<form:form method="post" modelAttribute="formUpload" action="${doUploadURL }" enctype="multipart/form-data" >

2. Missing File Upload Dependencies

Spring’s multipart file upload requires the commons-fileupload and commons-io libraries. If these aren’t in your project, your Controller won’t be able to process the file uploads. Add these dependencies to your build file:

Maven (pom.xml)

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

Gradle (build.gradle)

implementation 'commons-fileupload:commons-fileupload:1.4'
implementation 'commons-io:commons-io:2.11.0'

3. Missing MultipartResolver Configuration

Spring needs a MultipartResolver bean to handle multipart/form-data requests. Without this, your Controller won’t receive the uploaded files. Add this configuration to your Spring config class:

@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setMaxUploadSizePerFile(5242880); // Set max file size (5MB example)
    return resolver;
}

If you’re using XML configuration instead:

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

4. View Name Mismatch on Validation Failure

In your Controller’s doUpload method, if validation fails you return "uploadPage":

if(result.hasErrors()) {
    return "uploadPage";
}

But your GET endpoint uses ModelAndView("upload_page") (with an underscore). Make sure your view name matches the actual JSP filename (e.g., if your JSP is named upload_page.jsp, return "upload_page" instead of "uploadPage").

5. Verify FileUpload Class Properties

Double-check that your FileUpload model class has the correct property type and getter/setter methods:

public class FileUpload {
    private CommonsMultipartFile[] files;

    // Getter and Setter are REQUIRED for Spring to bind the form data
    public CommonsMultipartFile[] getFiles() {
        return files;
    }

    public void setFiles(CommonsMultipartFile[] files) {
        this.files = files;
    }
}

6. File System Permissions

Your code saves files to C:\\Users\\bogda\\Desktop\\input\\:

filePath = "C:\\Users\\bogda\\Desktop\\input\\" + multipartFile.getOriginalFilename();
  • Ensure the input directory exists (create it if not).
  • Make sure your application has read/write permissions to this folder. Without permissions, the file copy will fail silently or throw an IOException.

Start with fixing the typo first—it’s the most likely culprit. Then work through the other checks one by one, testing your application after each change.

内容的提问来源于stack exchange,提问作者Bogdan Rogojan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:04:22