无法从弹簧形式获取图像

11 java spring file-upload spring-mvc

而获取文件从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:/";
        }
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我添加了一些额外的代码来找到,如果我得到domainnull来到是真实的.我不知道如何解决这个问题.

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

java.lang.NullPointerException: unable to fetch : null
Run Code Online (Sandbox Code Playgroud)

domain.java

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

//getters and setters
Run Code Online (Sandbox Code Playgroud)

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

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

Pri*_*mal 5

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

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

                            imagefile.transferTo(path);

                            }
Run Code Online (Sandbox Code Playgroud)

编辑: - 将method属性更改POST为表单内部,否则它将发出GET请求.

<form:form modelAttribute="domain" method="post" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" name="image" />
Run Code Online (Sandbox Code Playgroud)


小智 3

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

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

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