与FileUpload问题 - 多部分文件的空值 [英] Problem with FileUpload - Null Value for Multipart File

查看:182
本文介绍了与FileUpload问题 - 多部分文件的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

 < form:form commandName =editMemberInfoModelObjmethod =postenctype = 多部分/格式数据 > 
< h1>编辑会员资讯< / h1>
< table>
//其他表单输入字段...
< tr>
< td>档案< / td>
< td>< input type =filename =file/>< / td>
< / tr>
< tr>
< td>< input type =submitvalue =Update Info/>< / td>
< / tr>
< / table>
< / form:form>

这个JSP的模型如下所示:

  public class EditMerchandiserModel(){
private MultipartFile file;

//所有属性的getters和setters
}

处理文件上传的控制器中的代码如下所示:

  if(model.getFile()。isEmpty() ) - >在这里抛出NULLPOINTER EXCEPTION 
{
MultipartFile file = model.getFile();
String fileName = file.getOriginalFilename();
String filePath =/ usr / local /+ fileName;
FileOutputStream fos = new FileOutputStream(filePath);
尝试
{

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

}
finally {
fos.close();




$ b我无法打内部代码,因为它是在文件中读取为空值。
为什么不把值绑定到字段?

解决方案

它看起来像你的文件输入框的名称文件,而它应该绑定的属性名称为照片(至少你试图使用getPhoto()来检索它)Spring是聪明的,但它不是那么聪明。 p>

I am working on a Spring web application and need to implement a simple FileUpload for one of my pages.

The page for the JSP contains the following snippet of code which included an upload field for uploading the file.

<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>

The model for this JSP looks like the following

public class EditMerchandiserModel(){
        private MultipartFile file;

        //getters and setters for all the properties
}

The code in the controller that handles the file upload looks like the following

    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();
         }
    }

I am unable to hit the inside code because it is reading in the file as a null value. Why is it not binding the value to the field?

解决方案

It looks like your file input box has the name "file" while the property it's supposed to bind to has the name "photo" (at least you're trying to retrieve it using "getPhoto()". Spring is smart, but it ain't that smart. :)

这篇关于与FileUpload问题 - 多部分文件的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆