java - FileUpload 问题 - 多部分文件为空值

标签 java spring jsp file-upload multipartform-data

我正在开发一个 Spring Web 应用程序,需要为我的一个页面实现一个简单的 FileUpload。

JSP 页面包含以下代码片段,其中包括用于上传文件的上传字段。

<form:form commandName="editMemberInfoModelObj" method="post" enctype="multipart/form-data">
        <h1>Edit Member Information</h1>
        <table>
            //Other Form Input Fields ...
            <tr>
                <td>File</td>
                <td><input type="file" name="file"/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Update Info"/></td>
            </tr>
        </table>
    </form:form>

此 JSP 的模型如下所示

public class EditMerchandiserModel(){
        private MultipartFile file;

        //getters and setters for all the properties
}

Controller 中处理文件上传的代码如下所示

    if(model.getFile().isEmpty())  -->THROWING NULLPOINTER EXCEPTION HERE
    {
        MultipartFile file = model.getFile();
        String fileName = file.getOriginalFilename();
        String filePath = "/usr/local/" + fileName;
        FileOutputStream fos = new FileOutputStream(filePath);
         try 
             {

            fos.write(file.getBytes());
         } catch (IllegalStateException e) {
            System.out.println(e);

         }
         finally{
             fos.close();
         }
    }

我无法点击内部代码,因为它在文件中读取为空值。 为什么它不将值绑定(bind)到字段?

最佳答案

看起来你的文件输入框的名称为“file”,而它应该绑定(bind)的属性的名称为“photo”(至少你正在尝试使用“getPhoto()”检索它。Spring 很聪明,但它没那么聪明。:)

关于java - FileUpload 问题 - 多部分文件为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713831/

相关文章:

java - Web 应用程序性能和部署架构 (java)

java - 当Spring Boot注释的类位于库中时,如何正确加载它们?

java - Spring Data JPA 中关系@ManyToMany 的查询方法

function - JSP错误: The method is not applicable for the arguments

apache - 日文字母在我的页面中显示不正确

html - 这是什么意思?

JavaFX16 警告 : Unsupported JavaFX configuration: classes were loaded from 'unnamed module @***'

java - setText 和 setValue 无法正常工作

java - Spring-Security:认证后调用方法

java - AcceptHeaderLocaleResolver 的 defaultLocale 不起作用