后台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. Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  2. google开发者工具调试技巧

    http://blog.sina.com.cn/s/blog_60a4fcef0102v3vt.html

  3. sip协议音视频性能测试

    http://www.cnblogs.com/wxiaoqin/p/3629926.html https://www.myvoipapp.com/cn/docs/faq/performance_tes ...

  4. Scrapy shell调试网页的信息

    通过scrapy shell "http://www.thinkive.cn:10000/zentaopms/www/index.php?m=user&f=login"

  5. 关于安装Apache之后,解析PHP的配置

    需要配置四个地方 LoadModule php5_module modules/libphp5.soServerName localhost:80DirectoryIndex index.phpAdd ...

  6. [NHibernate]利用LINQPad查看NHibernate生成SQL语句

    上篇文章中我们提到可以通过重写NHibernate的 EmptyInterceptor 拦截器来监控NHibernate发送给数据库的SQL脚本,今天看到有朋友用LINQPad工具来进行NHibern ...

  7. 循环遍历checkbox按钮,和点击后提示。

    今天做了一个有关checkbox点击的项目,点击checkbox获取这行的数值,然后相加.运用的是jquery和PHP传值. $('input[type=checkbox]').click(funct ...

  8. QuanbenSoft Windows Runtime (Windows Store)Apps 应用及其框架总览

    Parrot Simple audio repeater for language learners http://www.windowsphone.com/en-au/store/app/parro ...

  9. CodeSimth-.NetFrameworkDataProvider可能没有安装。解决方法

    原文地址:http://www.haogongju.net/art/2561889 1.下载System.Data.SQLite驱动:注意:根据自己的CPU选择是32位还是64位的驱动.建议选择4.0 ...

  10. compass reset和layout [Sass和compass学习笔记]

    reset 可以重置浏览器的的html的默认样式,因为各个浏览器对有些元素解析差别很大 通过重置样式可以让样式的浏览器兼容 更简单 使用方法简单 @import "compass/reset ...