springmvc上傳文件

  1. maven依賴
<!-- Apache Commons FileUpload --> 
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

<!-- Apache Commons IO --> 
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

2 . spring配置文件

<beans:bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

         <!-- setting maximum upload size -->
        <beans:property name="maxUploadSize" value="100000" />

</beans:bean>

3 . jsp中

<form method="POST" action="uploadMultipleFile" enctype="multipart/form-data">
        File1 to upload: <input type="file" name="file">

        Name1: <input type="text" name="name">

        <input type="submit" value="Upload"> Press here to upload the file!
    </form>

4 . controller

@RequestMapping("audio")
    @ResponseBody
    public Map<String,Object> uploadAudio(MultipartHttpServletRequest multipartRequest){
String name = multipartRequest.getParameter("name");
 for (Iterator it = multipartRequest.getFileNames(); it.hasNext(); ) {
            String key = (String) it.next();
            MultipartFile orderFile = multipartRequest.getFile(key);
            name = orderFile.getOriginalFilename();
            try {
                orderFile.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println(name);
        }
        return null;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章