码迷,mamicode.com
首页 > Web开发 > 详细

文件上传之Apache commons fileupload使用

时间:2016-12-10 00:50:36      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:前台   name   null   buffer   检测   art   esc   attr   final   

后台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{
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while((len = ins.read(buffer)) > -1)
                                ous.write(buffer,0,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{
                            byte[] buffer = new byte[1024];
                            int len = 0;
                            while((len = ins.read(buffer)) > -1)
                                ous.write(buffer,0,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使用

标签:前台   name   null   buffer   检测   art   esc   attr   final   

原文地址:http://www.cnblogs.com/panweiwei/p/6152841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!