• ssm框架下文件上传


    springmvc实现文件上传的步骤:

    1.页面上,通过input来准备file组件,该标签,必须给定name属性值
    同时,要求form表单必须给定一个属性:enctype="multipart/form-data"
    2.在pom.xml文件中,添加文件上传的第三方工具:
    commons-fileupload-1.3.2.jar
    commons-io-2.2.jar
    3.在app-springmvc.xml配置文件中,准备上传操作的对象:CommonsMultipartResolver
    在这个对象中,我们可以对文件大小,编码方式等进行设定
    4.在控制器中,通过@RequestParam MultipartFile pic这种方式,来接收页面传递的文件
    这里,参数的名字必须与页面上file组件的name属性值一致
    此时,在控制器中,已经能够正常地接收页面上传的文件了,下一步,只需要把接收的这个文件,保存到服务器的硬盘上即可
    <!-- 2文件上传依赖 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.2</version>
            </dependency>
    <!-- 3文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
             <property name="maxUploadSizePerFile" value="102400000" ></property>
         </bean>

    控制器:

    <!-- 4 控制器。。。文件上传 -->
    @RequestMapping(value="addFile",method=RequestMethod.POST)
        public String addFile(@RequestParam MultipartFile pic,HttpServletRequest request,Model model){
            String filename = pic.getOriginalFilename();
            System.out.println("接收到的文件的名字: "+filename);
            String contentType = pic.getContentType();
            System.out.println("接收文件的类型: "+contentType);
            InputStream iStream=null;
            OutputStream oStream=null;
            String realPath = request.getSession().getServletContext().getRealPath("/imgs");
            //得到一个随机的字符串
            String string = UUID.randomUUID().toString();
            String endname = filename.substring(filename.lastIndexOf("."), filename.length());
            
            /*try {
                is = pic.getInputStream();
                os = new FileOutputStream(new File(realpath+"/"+uuid+endname));
                //要把文件输出到硬盘上,第一种方式:自己写字节流 并通过边读边写操作完成输出
                byte [] b = new byte[1024];
                int len = is.read(b);
                while(len!=-1){
                    os.write(b, 0, len);
                    len = is.read(b);
                }
                os.flush();
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            
            
            //通过commons-io包提供的工具来完成拷贝
            try {
                iStream=pic.getInputStream();
                oStream=new FileOutputStream(new File(realPath+"/"+string+endname));
                FileCopyUtils.copy(iStream, oStream);
                oStream.flush();
                oStream.close();
                iStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("文件保存路径:"+realPath+"/"+string+endname+"....");
            return "index";
        }
  • 相关阅读:
    并发编程-阻塞队列&JUC常用工具
    并发编程-Condition源码分析&基于Condition实现阻塞队列
    并发编程-ReentrantLock锁源码分析&Condition设计
    并发编程-JMM&ReentrantLock锁以及原理
    数据库操作支持函数
    python内置进制转换函数
    三目运算符
    数据库常见操作
    宏使用汇总
    sort: invalid comparator
  • 原文地址:https://www.cnblogs.com/dztHome/p/8929902.html
Copyright © 2020-2023  润新知