问题 无法从弹簧形式获取图像


从中获取文件时 spring form 我正进入(状态 null 值和如果我尝试这个代码的其余字段我的意思是   multipart input types 它的工作正常。在调试时我得到了 null 来自行的价值。如果我试着去取 image 从现有文件夹我在webapp下的图像,该网址能够在浏览器中显示图像,但无法从中读取值 files 使用浏览器并抱歉我的英语不好

编辑 如果我评论图像代码,应用程序工作正常,但当我介绍图像的代码我收到错误

MultipartFile file = domain.getImage(); //this is getting null 

这是相关的代码 调节器

@RequestMapping(value = "/form", method = RequestMethod.GET)
    public String formInputGet(Model model) {
        model.addAttribute("domain", new Domain());
        return "form";
    }



@RequestMapping(value = "/form", method = RequestMethod.POST)
        public String formInputPost(@ModelAttribute("domain") Domain domain, HttpServletRequest httpServletRequest) {

            MultipartFile file = domain.getImage();
if (image== null)
            throw new NullPointerException("unable to fetch "+file); //getting NPE everytime
            String rootDirectory = httpServletRequest.getSession().getServletContext().getRealPath("/");
            if (domain.getImage() != null && !domain.getImage().isEmpty())
                try {
                    File path = new File(rootDirectory + "images\\" + domain.getFirstName() + ".png");
                    file.transferTo(path);
                } catch (IllegalStateException | IOException e) {
                    e.printStackTrace();
                }
            repositiry.addToList(domain);
            return "redirect:/";
        }

form.jsp

<form:form modelAttribute="domain" enctype="multipart/form-data">
    First Name<br>
        <form:input path="firstName" />
        <br>Last Name :<br>
        <form:input path="lastName" />
        <br>upload Image<br>
        <form:input path="image" type="file" />
        <hr>
        <input type="submit">
    </form:form>

DispatcherServlet的

<mvc:annotation-driven />
    <mvc:resources location="/images/" mapping="/images/**" />
    <context:component-scan base-package="com" />
    <bean id="multipartReslover"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10240000" />
    </bean>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

我添加了一些额外的代码来查找我是否收到了 domain 如 null 变得如此。我不知道如何解决这个问题。

添加文件检查后我收到错误

java.lang.NullPointerException: unable to fetch : null

domain.java

public class Domain {
    private String firstName;
    private String lastName;
    private MultipartFile image;

//getters and setters

注意 任何有用的答案,如果它有其他工作方式也欢迎:)

任何帮助表示赞赏,谢谢:)


4890
2017-10-01 11:37


起源

你能展示你的域名吗? - kuhajeyan
@kuhajeyan我做了编辑
你可以改为公共String formInputPost(@ModelAttribute(“domain”)Domain domain,BindingResult result){...}来自public String formInputPost(@ModelAttribute(“domain”)Domain domain,HttpServletRequest httpServletRequest){..} - kuhajeyan
好的,我会尝试将它们全部绑定
@kuhajeyan我把所有结果都绑定,然后添加到我的 formdatapost(..) 控制器但仍然传递给其他部分


答案:


你应该做所有事情@kuhajeyen说,如果从域对象获取图像不顺利你可以试试这个

public String formInputPost(@ModelAttribute("domain") Domain domain,
                            @RequestParam("image") MultipartFile imagefile,
                            HttpServletRequest httpServletRequest ) {

                            imagefile.transferTo(path);

                            }

编辑: - 改变 method 属性为 POST 在形式内,否则它将成为一个 GET 请求。

<form:form modelAttribute="domain" method="post" enctype="multipart/form-data">

并用此行替换您的输入类型文件,我认为在尝试将输入类型文件与对象绑定时存在一些问题。

<input type="file" name="image" />

5
2017-10-02 17:47



好但是 @requestParm(..) 会取的 http 请求,我甚至无法从表单中获取值
你错过了 method="post" 表单内的属性,这就是您无法从请求中获取值的原因。 - Priyamal
但是当我尝试相同的方法时没有 post 我能够获取其余的参数
当我添加时它失败了 multipart
我对代码做了一些更改,试一试。 - Priyamal


我的配置文件中有两个拼写错误

1) <mvc:resources location="/images/" mapping="/images/**" /> 这里映射应该是这样的 mapping ="images/**"

2)File path = new File(rootDirectory + "images\\" + domain.getFirstName() + ".png"); 这里的路径应该是 rootDirectory+"\\images\\"+.... 代替


3
2017-10-08 11:17





你需要告诉spring,如何解决multipart文件

添加这个bean

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

而且似乎你没有在表格中映射你的行动

<form:form modelAttribute="domain" enctype="multipart/form-data" action="xxxx/form">
....

</form:form>

2
2017-10-01 18:36



哦,直到我明白动作将使用映射重定向到其他servlet,但这里应该去哪里?
并感谢回复:)
@kuhejeyan我试过你的方法但是你能告诉我在哪里重定向那个表格吗?
@HelloWorld如果你愿意,你可以重定向到同一个页面,但我想你正在查找要发布的确切表单url,如果是的话,那将是你的formInputPost方法的动作网址 - kuhajeyan
@kuhaheyan实际问题是我申请了 NPE 并且它显示为true请检查我的代码更新