SpringMVC流式上传文件

1.添加jar包

这里写图片描述

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 = 0;
                while((b=in.read()) != -1){
                    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 = 0; 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 = 0;
                    while ((b = in.read()) != -1) {
                        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",200);
        map.put("msg", "OK");
        return Tools.getJson(map);

    }

这样一个基于框架的简单的上传文件功能就好啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值