天天看點

SpringMVC流式上傳檔案

1.添加jar包

SpringMVC流式上傳檔案

2.spring-controller.xml配置:

<!-- 檔案上傳解析器配置以及大小編碼等參數 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="10485760000" />
        <property name="maxInMemorySize" value="40960" />
    </bean>
           

3.控制層代碼:

1.單檔案上傳:

@RequestMapping("upload")
    @ResponseBody
    public void upload(@RequestParam("file") CommonsMultipartFile file,HttpSession session)throws Exception {
        System.out.println("filename------->"+file.getOriginalFilename());
        if(!file.isEmpty()){
            try {
                String filename = new Date().getTime()+file.getOriginalFilename();
                String realPath = session.getServletContext().getRealPath("/WEB-INF/upload/");                 
                FileOutputStream os = new FileOutputStream(realPath + filename);
                InputStream in = file.getInputStream();
                int b = ;
                while((b=in.read()) != -){
                    os.write(b);
                }               
                os.flush();
                os.close();     
                in.close();             
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
           

2.多檔案上傳:

@RequestMapping("threeFileupload")
    public String threeFileUpload(
            @RequestParam("file") CommonsMultipartFile files[],
            HttpSession session) {

        List<String> list = new ArrayList<String>();
        // 獲得項目的路徑
        String path = session.getServletContext().getRealPath(
                "/WEB-INF/upload/");
        // 上傳位置
        File f = new File(path);
        if (!f.exists())
            f.mkdirs();
        for (int i = ; i < files.length; i++) {
            // 獲得原始檔案名
            String fileName = files[i].getOriginalFilename();
            System.out.println("原始檔案名:" + fileName);
            // 新檔案名
            String newFileName = new Date().getTime()
                    + files[i].getOriginalFilename();
            if (!files[i].isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(path
                            + newFileName);
                    InputStream in = files[i].getInputStream();
                    int b = ;
                    while ((b = in.read()) != -) {
                        fos.write(b);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("上傳檔案到:" + path + newFileName);
            list.add(path + newFileName);

        }
        HashMap map =new HashMap();
        map.put("content", list);
        map.put("code",);
        map.put("msg", "OK");
        return Tools.getJson(map);

    }
           

這樣一個基于架構的簡單的上傳檔案功能就好啦!

繼續閱讀