SpringMVC的文件上传需要注意的问题

简要的代码罗列

1、需要的jar包


    <dependency>
        <groupId>commons-iogroupId>
        <artifactId>commons-ioartifactId>
        <version>2.4version>
    dependency>

    
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.3.1version>
    dependency>

2、dispatcher-servlet.xml


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="209715200"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="resolveLazily" value="true"/>
bean>

3、Controller

@RequestMapping(value="/doUpload",method=RequestMethod.POST)
    public String doUploadFile(@RequestParam("file") MultipartFile file) throws IOException{
        if(!file.isEmpty()){
            FileUtils.copyInputStreamToFile(file.getInputStream(),new File("E:\\temp\\",System.currentTimeMillis()+file.getOriginalFilename()));
        }
        return "success";
    }

5、upload.jsp

"<%=request.getContextPath()%>/hello/doUpload" method="post" enctype="multipart/form-data"> type="file" name="file"> type="submit">

注意: jsp文件中enctype属性需要写,不然会报以下的错
SpringMVC的文件上传需要注意的问题_第1张图片
这里的坑我查了很久,需要很注意!!!!

你可能感兴趣的:(springmvc,上传文件)