SSM文件的上传与下载

1.在自己已经构建好的maven  web项目中 pom.xml配置文件中添加上传下载所需要的jar包
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
2.在spring的applicationContext.xml配置文件中添加文件上传解析器
<!-- 配置文件上传解析器 -->
    <!-- id的值是固定的-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--请求编码格式,必须和JSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--上传文件大小上限,单位为字节(10485760=10M)-->
        <property name="maxUploadSize" value="10485760"/>
        <!--能使用的最大内存-->
        <property name="maxInMemorySize" value="4096000"/>
    </bean>
3.文件上传

前台页面:

<form action="/upload222" method="post" enctype="multipart/form-data">
        文件:<input type="file" id="tp" name="file">
        <input type="submit" value="提交">
    </form>

后台实现:

//@RequestParam("file")将name=file控件得到的文件封装成CommonsMultipartFile对象
    //批量上传CommonsMultipartFile则为数组即可
    @RequestMapping("/upload222")
    public String fileUpload(@RequestParam("file") MultipartFile file , HttpServletRequest request) throws IOException {
            //上传路径保存设置  服务器路径 E:\apache-tomcat-8.5.87\webapps\ROOT
            //String path = request.getSession().getServletContext().getRealPath("/static/upload");
            //tomcat根目录 E:\apache-tomcat-8.5.87\webapps
            // upload\file
            File path = new File(request.getSession().getServletContext().getRealPath("/")).getParentFile();
            String savePath = "/upload/file/";
            File realPath = new File(path+savePath);
            if (!realPath.exists()){
                realPath.mkdirs();
            }
            //上传文件地址
            System.out.println("上传文件保存地址:" + realPath);

            //通过CommonsMultipartFile的方法直接写文件
            //System.currentTimeMillis()根据系统时间产生随机数,保证上传的图片名字不一样
            //UUID.randomUUID()
            String filename=UUID.randomUUID()+"_"+file.getOriginalFilename();
            file.transferTo(new File(realPath + "/" + filename));


            /*String url=path.getPath()+"/upload/file/"+filename;
            System.out.println("URL"+url);*/
            return "redirect:/index.jsp";
    }
4.文件下载

前台页面:

<form action="/download222" method="post">
        <input type="submit" value="下载">
    </form>

后台实现:

 @RequestMapping("/download222")
    public String downloads(String fileNames,HttpServletResponse response , HttpServletRequest request) throws Exception{
        //要下载的图片地址  E:\apache-tomcat-8.5.87\webapps\ROOT
        //String path = request.getSession().getServletContext().getRealPath("/static/upload");
        //String property = System.getProperty("user.dir");
        File path = new File(request.getSession().getServletContext().getRealPath("/")).getParentFile();
        String savePath = "/upload/file/";
        String fileName = savePath+"/"+"87ea4c0d-7e48-46d2-9ff2-4283a1ab7699_签名.png";

        //1.设置response响应头
        response.reset();//设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8");//字符编码
        response.setContentType("multipart/form-data");//二进制传输数据
        //设置响应头
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + URLEncoder.encode(fileName,"UTF-8"));

        File file = new File(path, fileName);
        OutputStream os = response.getOutputStream();
        // 读取文件
        InputStream in = new FileInputStream(file);
        // copy文件
        IOUtils.copy(in, os);
        in.close();
        os.close();


        return null;

    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值