zoukankan      html  css  js  c++  java
  • 上传

    jsp

                <form action="<%=path%>/Indexmanage/fileUpload" method="post" enctype="multipart/form-data">
                    <!-- type="file"这一标签必须同时添加name属性,否则在处理上传时,无法检测到上传文件 -->
                    <input type="file" name="myfiles"/>
                    <input type="submit" value="上传"/>                 
                </form>

    controller

        /**
         * 文件上传
         */
        @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
        @ResponseBody
        public int fileUpload(@RequestParam MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws Exception {
            if(myfiles.length <= 0){
                return 0;
            }
            MultipartFile myfile = myfiles[0];
            String fileName = myfile.getOriginalFilename();
            System.out.println("文件长度: " + myfile.getSize());
            System.out.println("文件类型: " + myfile.getContentType());
            System.out.println("文件名称: " + myfile.getName());
            System.out.println("文件原名: " + fileName);
            System.out.println("========================================");
            //如果用的是Tomcat服务器,则文件会上传到\%TOMCAT_HOME%\webapps\YourWebProject\WEB-INF\upload\文件夹中
            String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
            File f = new File(realPath, fileName);
            //这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
            FileUtils.copyInputStreamToFile(myfile.getInputStream(), f);
        }

    需要导入的包

    import org.springframework.web.multipart.MultipartFile;
    import org.apache.commons.io.FileUtils;

    maven依赖

        <dependency>
            <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
            <version>4.0.4.RELEASE</version>
        </dependency> 
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
  • 相关阅读:
    直接插入排序
    合并排序--分治法思想
    scanf printf函数返回值
    转自CSDN,关于状态机
    关于制表符
    网上找的一篇博文,原文搞错了,应该是 ,本文已改正!——回车CR和换行line feed
    再看c语言之getchar/putchar
    使用FL2440之问题1
    Java:String、StringBuffer和StringBuilder的区别
    编译哈工大语言技术平台云LTP(C++)源码及LTP4J(Java)源码
  • 原文地址:https://www.cnblogs.com/superJF/p/6110361.html
Copyright © 2011-2022 走看看