文件上传

private void doFileUpload(HttpSession session, HttpServletRequest request,
            HttpServletResponse response) throws IOException
    {
        try
        {
            long totalSize = request.getContentLength();
           
            // 创建UploadListener对象
            UploadListener listener = new UploadListener(totalSize);
           
            // 启动监听状态
            listener.start();
           
            // 将监听器对象的状态保存在Session中
            session.setAttribute("FILE_UPLOAD_STATS",
                    listener.getFileUploadStats());
           
            // 创建MonitoredDiskFileItemFactory对象
            MonitoredDiskFileItemFactory factory = new MonitoredDiskFileItemFactory(
                    listener);
            factory.setSizeThreshold(UPLOAD_THRESHOLD_SIZE);
           
            // 通过该工厂对象创建ServletFileUpload对象
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("UTF-8");
            upload.setSizeMax(UPLOAD_MAX_SIZE);
            // 将转化请求保存到list对象中
            List items = upload.parseRequest(request);
           
            // 停止使用监听器
            listener.done();
           
            boolean hasError = false;
        
            // 循环list中的对象
            for (Iterator i = items.iterator(); i.hasNext();)
            {
                FileItem fileItem = (FileItem) i.next();
                //如果该FileItem不是表单域
                if (!fileItem.isFormField())
                {
                    try
                    {
                        //调用processUploadedFile方法,将数据保存到文件中
                        processUploadedFile(fileItem, request);
                    }
                    catch (FileNotFoundException fe)
                    {
                        throw fe;
                    }
                    finally
                    {
                        //内存中删除该数据流
                        fileItem.delete();
                    }
                }
                else
                {
                    //获取节点名称
                    String name = fileItem.getFieldName();
                    if ("AddNodeTextName1".equals(name))
                    {
                        nodeName = new String(fileItem.getString().getBytes("ISO8859-1"),"UTF-8");
                    }
                  
                       
                }
            }
           
            //添加节点          
            try
            {
                //调用添加节点方法                           
            }
            catch (AppException e)
            {
                session.setAttribute("isSuccess", false);
                e.printStackTrace();
            }
           
            if (!hasError) // 如果没有出现错误
            {
                //调用sendCompleteResponse方法
                sendCompleteResponse(response, null);
            }
            else
            {
                sendCompleteResponse(response, this.getMessage("Upload.fail",
                        request));
            }
        }
                    }

你可能感兴趣的:(文件上传)