后台servlet代码:
        File file1 = null,file2=null;
        String description1 = null,description2 = null;
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
        DiskFileUpload diskFileUpload = new DiskFileUpload();
        try{
            List<FileItem> list = diskFileUpload.parseRequest(request);
           
            out.println("下面开始遍历所有的FileItem<br/><br/>");
           
            for(FileItem fileItem : list){
                if(fileItem.isFormField()){
                    if("description1".equals(fileItem.getFieldName())){
                        out.println("遍历到description1 ... 现在description1是空-----批注1---<br/><br/>");
                        description1 = new String(fileItem.getString().getBytes(),"UTF-8");
                    }
                    if("description2".equals(fileItem.getFieldName())){
                        out.println("遍历到description2 ... 现在description2是空-----批注2---<br/><br/>");
                        description2 = new String(fileItem.getString().getBytes(),"UTF-8");
                    }
                } else{
                    if("file1".equals(fileItem.getFieldName())){
                        File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
                        out.println("已经检测到用户上传了文件1,并且已经遍历到file1...<br/>");
                        out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"<br/><br/>");
                        /*将客户端路径放到session中*/
                        HttpSession session = request.getSession();
                        session.setAttribute("clientAddress", remoteFile.getAbsolutePath());
                       
                        file1 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
                        file1.getParentFile().mkdirs();
                        file1.createNewFile();
                       
                        InputStream ins = fileItem.getInputStream();
                        OutputStream ous = new FileOutputStream(file1);
                       
                        try{
];
;
)
,len);
                            out.println("已经保存文件到绝对路径——"+file1.getAbsolutePath()+"<br/><br/>");
                            /*将绝对路径放到session中*/
                            session.setAttribute("JueDuiAddress", file1.getAbsolutePath());
                            out.println("<img src="+file1.getAbsolutePath() +" alt='商务小人'/><br/><br/>");
                        }finally{
                            ous.close();
                            ins.close();
                        }
                    }
                    if("file2".equals(fileItem.getFieldName())){
                        File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
                        out.println("已经检测到用户上传了文件2,并且已经遍历到file2...<br/><br/>");
                        out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"<br/><br/>");
                       
                        HttpSession session = request.getSession();
                        session.setAttribute("clientAddress", remoteFile.getAbsolutePath());
                       
                        file2 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
                        file2.getParentFile().mkdirs();
                        file2.createNewFile();
                       
                        InputStream ins = fileItem.getInputStream();
                        OutputStream ous = new FileOutputStream(file2);
                       
                        try{
];
;
)
,len);
                            out.println("已经保存文件到绝对路径——"+file2.getAbsolutePath()+"<br/><br/>");
                            /*将绝对路径放到session中*/
                            session.setAttribute("JueDuiAddress", file2.getAbsolutePath()); 
                        }finally{
                            ous.close();
                            ins.close();
                        }
                    }
                }
                out.println("Request 解析完毕<br/><br/>");
            }
        }
前台jsp代码: 
<form action="/login/UploadSer" method="post" enctype="multipart/form-data">
        <div align="center">
            <fieldset style="width:80%">
                <legend>上传文件</legend><br/>
                    <div align="left">上传文件1</div>
                    <div align="left">
                        <input type="file" name="file1"/>
                    </div>
                    <div align="left">上传文件2</div>
                    <div align="left">
                        <input type="file" name="file2"/>
                    </div>
                    <div>
                        <div align='left'>上传文件说明1</div>
                        <div align='left'>
                             <input type="text" name="description1"/>
                        </div>
                    </div>
                    <div>
                        <div align='left'>上传文件说明2</div>
                        <div align='left'>
                             <input type="text" name="description2"/>
                        </div>
                    </div>
                    <div>
                        <div align='left'>
                            <input type='submit' value="上传文件"/>
                        </div>
                    </div>
            </fieldset>
        </div>
    </form>
 
运行结果截图:
 

文件上传之Apache commons fileupload使用的更多相关文章

  1. Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)

    相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...

  2. 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

    最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...

  3. (转)文件上传org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed

    文件上传时,tomcat报错org.springframework.web.multipart.MultipartException: Failed to parse multipart servle ...

  4. WEB文件上传之apache common upload使用(一)

    文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...

  5. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  6. Spring MVC的文件上传

    1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...

  7. Spring中MultipartHttpServletRequest实现文件上传

    Spring中MultipartHttpServletRequest实现文件上传 转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能 ...

  8. Spring中MultipartHttpServletRequest实现文件上传 生成缩略图

    转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传  用户必须能够上传图片,因此需要文件上传的功能.比较常见的文件上传组件有Commons Fil ...

  9. Spring中实现文件上传

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt110 实现图片上传  用户必须能够上传图片,因此需要文件上传的功能.比较常见 ...

随机推荐

  1. react native 刷新机制----通知

    在项目中,不知道大家有没有遇到这样的一个问题,比如说有两个页面A,B.A页面中有某个按钮点击后可以跳转到B页面,现在有一个需求就是,我在B页面中做了某些操作,然后点击回退按钮,回到A页面,A页面中的数 ...

  2. Codeforces Round #388 (Div. 2) - B

    题目链接:http://codeforces.com/contest/749/problem/B 题意:给定平行四边形的3个点,输出所有可能的第四个点. 思路:枚举任意两个点形成的直线,然后利用这两个 ...

  3. lua

    lua的语言特性: 1. lua 的table可以实现多种数据结构:数组.记录.线性表.队列.集合等: 2. lua的closure闭合函数 3. lua的迭代器和泛型的for 4. lua的协同程序 ...

  4. Linux 用户添加sudo 权限

    编辑/etc/sudoers 搜索root 添加 账号 ALL=(ALL) ALL

  5. Python列表和元组

    Python是没有数组的概念,但是和数组比较相近的概念是列表和元素. 下面两个例子展示列表和元组. # coding=utf-8 # 元组 students = ('小明', '小黄', '小李', ...

  6. 【第二课】WEBIX 入门自学-获取WEBIX及相关资料

    下载WEBIX组件库 http://webix.com/download/ WEBIX在线文档 http://docs.webix.com/ 离线的手册 下载

  7. View的滑动

    View的滑动 通过三种方式可以实现View的滑动: 1.通过View本身提供的scrollTo/scrollBy方法来实现滑动 2.通过动画给View施加平移效果来实现滑动 3.通过改变View的L ...

  8. 安卓手机USB网络共享,电脑卡顿、反应慢

    1.首先需要把手机连接到电脑,在手机上打开USB网络共享. 2.打开设备管理器 3.在网络适配器中,找到Remote NDIS based Internet Sharing Device,右键更新驱动 ...

  9. swiper的初步使用

    1.引入文件,顺序引入(此处基于jquery,且版本至少1.7以上) <link rel="stylesheet" href="path/to/swiper-3.4 ...

  10. Jenkins部署配置简介

    前段时间研究了一下自动化测试,因而接触到了Jenkins,今天有时间进行一下Jenkins部署配置相关知识的总结分享 前言:由于本次只是实验性研究,采用Windows环境,因此Jenkins可以通过下 ...