后台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. 2.View绘制分析笔记之onMeasure

    今天主要学习记录一下Android View绘制三部曲的第一步,onMeasure,测量. 起源 在Activity中,所有的View都是DecorView的子View,然后DecorView又是被V ...

  2. Swif - 可选型

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...

  3. MIT 6.828 JOS学习笔记17. Lab 3.1 Part A User Environments

    Introduction 在这个实验中,我们将实现操作系统的一些基本功能,来实现用户环境下的进程的正常运行.你将会加强JOS内核的功能,为它增添一些重要的数据结构,用来记录用户进程环境的一些信息:创建 ...

  4. iOS 使点击事件穿透透明的UIView

    如图: 悬浮的三个按钮下方有一个可以点击的灰色区域,但是点击按钮之间的透明区域, 这三个按钮的contentView会响应这个点击事件,这时候需要让这个contentView不响应这个点击事件. 解决 ...

  5. delay(和setTimeout()的区别

    近来几日在写游戏代码时,频繁会用到定时器,偶尔想到有个.delay()方法,用了几次发现两者效果相差很大,遂就仔细考究了一下两者的区别! 1. setTimeout函数是从页面开始的时候计算time的 ...

  6. Mac系统中配置Tomcat环境

    第一步:下载Tomcat 下载地址:http://tomcat.apache.org/download-80.cgi 直接下载如图选中的即可 第二步: 下载完成后,解压,随意放入目录.如我就把它放在/ ...

  7. vscode过滤pyc文件

    在工作区设置里添加如下代码: { "files.exclude": { "**/.git": true, "**/.svn": true, ...

  8. *HDU 1392 计算几何

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. Django数据导入

    从网上下载的一些数据,excel表格,xml文件,txt文件等有时候我们想把它导入数据库,应该如何操作呢? 以下操作符合 Django版本为 1.6 ,兼顾 Django 1.7, Django 1. ...

  10. java并发之volatile

    volatile是轻量级的synchronized,它在多处理器应用开发中保证了共享变量的“可见性”(可见性指当一个线程修改共享变量后,其它线程可以看到这个修改). volatile如果使用合理会比s ...