zoukankan      html  css  js  c++  java
  • SSM框架之中如何进行文件的上传下载

    SSM框架的整合请看我之前的博客:http://www.cnblogs.com/1314wamm/p/6834266.html

    现在我们先看如何编写文件的上传下载:你先看你的pom.xml中是否有文件上传的组件包

    先看我们写的测试jsp页面:

        <%@ page language="java" contentType="text/html; charset=utf-8"  
            pageEncoding="utf-8"%>  
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
        <html>  
        <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
        <title>文件上传下载</title>  
        </head>  
        <body>  
            <form action="http://localhost:8080/uploadDemo/rest/file/upload" method="post" enctype="multipart/form-data">  
                选择文件:<input type="file" name="file" width="120px">  
                <input type="submit" value="上传">  
            </form>  
            <hr>  
            <form action="http://localhost:8080/uploadDemo/rest/file/down" method="get">  
                <input type="submit" value="下载">  
            </form>  
        </body>  
        </html>  

    spring的servlet视图解析器下面定义CommonsMultipartResolver文件解析器,就是加入这个的时候运行项目,如果没有fileuload相关的jar包就会报错。

     在controller层写上springmvc上传下载的代码

        package com.baidu;  
        @RequestMapping("file")  
        @Controller  
        public class FileController {  
            /**  
             * 文件上传功能  
             * @param file  
             * @return  
             * @throws IOException   
             */  
            @RequestMapping(value="/upload",method=RequestMethod.POST)  
            @ResponseBody  
            public String upload(MultipartFile file,HttpServletRequest request) throws IOException{  
                String path = request.getSession().getServletContext().getRealPath("upload");  
                String fileName = file.getOriginalFilename();    
                File dir = new File(path,fileName);          
                if(!dir.exists()){  
                    dir.mkdirs();  
                }  
                //MultipartFile自带的解析方法  
                file.transferTo(dir);  
                return "ok!";  
            }  
              
            /**  
             * 文件下载功能  
             * @param request  
             * @param response  
             * @throws Exception  
             */  
            @RequestMapping("/down")  
            public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{  
                //模拟文件,myfile.txt为需要下载的文件  
                String fileName = request.getSession().getServletContext().getRealPath("upload")+"/myfile.txt";  
                //获取输入流  
                InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));  
                //假如以中文名下载的话  
                String filename = "下载文件.txt";  
                //转码,免得文件名中文乱码  
                filename = URLEncoder.encode(filename,"UTF-8");  
                //设置文件下载头  
                response.addHeader("Content-Disposition", "attachment;filename=" + filename);    
                //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型    
                response.setContentType("multipart/form-data");   
                BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());  
                int len = 0;  
                while((len = bis.read()) != -1){  
                    out.write(len);  
                    out.flush();  
                }  
                out.close();  
            }  
        }  
  • 相关阅读:
    2、Mybatis中一些常用的概念
    1、Mybatis的基本CRUD
    4、spring 官方下载地址
    3、Spring注解用法的一般步骤
    2、Spring开发的jar文件
    1、Spring的xml完整版命名空间
    1、Struts2和Hibernate的简单整合(带Session的管理方式)
    2、Struts2引入多个配置文件
    利用filter和动态代理解决全站乱码问题
    1、Struts2 的简单配置
  • 原文地址:https://www.cnblogs.com/1314wamm/p/7444134.html
Copyright © 2011-2022 走看看