⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadservlet.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                // Print file out.                outFile( data, response );                            } catch( FileManagerException ex ) {                Throwable t = ex.returnCause();                if( t != null && ( t instanceof FileNotFoundException ) ) {                    // File not found!                    response.setStatus( HttpServletResponse.SC_NOT_FOUND );                } else {                    throw new ServletException( ex );                }            }        }    }        //    // Uploads a new file by ID.    //    protected void doNewAction( HttpServletRequest request,                                HttpServletResponse response )        throws ServletException, SecurityException, IOException {        // Init.        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        List items = getPostItems( request );        // Parse HTTP request.        String fileName = null;        String subFolder = null;        String entityName = null;        Long recordID = null;        FileItem fileItem = null;        boolean isInlineMemo = false;                for( Iterator it = items.iterator(); it.hasNext(); ) {            FileItem item = ( FileItem ) it.next();            if( !item.isFormField() ) {                fileItem = item;            } else if( item.getFieldName().equals( "filename" ) ) {                fileName = item.getString();            } else if( item.getFieldName().equals( "subfolder" ) ) {                subFolder = item.getString();            } else if( item.getFieldName().equals( "entity" ) ) {                entityName = item.getString();            } else if( item.getFieldName().equals( "recordId" ) ) {                recordID = new Long( item.getString() );            } else if( item.getFieldName().equals( "inlineMemo" ) ) {                isInlineMemo = true;            }        }        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "File data:" +                          "\n\t file name = '" + fileName + "'" +                          "\n\t folder = '" + subFolder + "'" +                          "\n\t entity = '" + entityName + "'" +                          "\n\t record ID = " + recordID );        }        // Check for the required parameters.        if( fileItem == null ) {            throw new IncorrectParameterException( "file" );        }        // Create a new file and get its URL.        File file = manager.saveFile( fileItem, subFolder, entityName, recordID );        // Get file path and name.        String filePath = manager.getLocalName( file );        if( StringHelper.isEmpty( fileName ) ) {            fileName = filePath;        }        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "Inserted file: " + filePath );        }        // Send HTML to close the window on client.        response.setContentType( ServletHelper.CONTENT_TYPE_TEXT );        PrintWriter out = response.getWriter();        out.println("|" + filePath + "|RESULT=SUCCESS|");    }    //    // Uploads a new file by ID.    //    protected void doAttachAction( HttpServletRequest request,                                HttpServletResponse response )        throws ServletException, SecurityException, IOException {        // Init.        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        List items = getPostItems( request );        // Parse HTTP request.        String fileName = null;        String subFolder = null;        FileItem fileItem = null;        boolean isInlineMemo = false;                for( Iterator it = items.iterator(); it.hasNext(); ) {            FileItem item = ( FileItem ) it.next();            if( !item.isFormField() ) {                fileItem = item;            } else if( item.getFieldName().equals( "filename" ) ) {                fileName = item.getString();            } else if( item.getFieldName().equals( "subfolder" ) ) {                subFolder = item.getString();            } else if( item.getFieldName().equals( "inlineMemo" ) ) {                isInlineMemo = true;            }        }        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "File data:" +                          "\n\t file name = '" + fileName + "'" +                          "\n\t folder = '" + subFolder + "'" );        }        // Check for the required parameters.        if( fileItem == null ) {            throw new IncorrectParameterException( "file" );        }        // Create a new file and get its URL.        File file = manager.saveFile( fileItem, subFolder );        // Get file path and name.        String filePath = manager.getLocalName( file );        if( StringHelper.isEmpty( fileName ) ) {            fileName = filePath;        }        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "Inserted file: " + filePath );        }        // Send HTML to close the window on client.        response.setContentType( ServletHelper.CONTENT_TYPE_TEXT );        PrintWriter out = response.getWriter();        out.println("|" + filePath + "|RESULT=SUCCESS|");    }    //    // Updates the existing file.    //    protected void doUpdateAction( HttpServletRequest request,                                   HttpServletResponse response )        throws ServletException, com.queplix.core.integrator.security.SecurityException, IOException {        // Init.s        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        List items = getPostItems( request );        // Get file parameters.        Long id = null;        FileItem fileItem = null;        for( Iterator it = items.iterator(); it.hasNext(); ) {            FileItem item = ( FileItem ) it.next();            if( !item.isFormField() ) {                fileItem = item;            } else if( item.getFieldName().equals( "id" ) ) {                id = new Long( item.getString() );            }        }        // Check for the required parameters.        if( fileItem == null ) {            throw new IncorrectParameterException( "file" );        }        if( id == null ) {            throw new IncorrectParameterException( "id" );        }        // Replace the file.        manager.replaceFile( id.longValue(), fileItem );        // Send HTML to close the window on client.        response.setContentType( ServletHelper.CONTENT_TYPE_HTML );        response.getWriter().print( "<script>window.close()</script>" );    }    //    // Deletes the file(s).    //    protected void doDeleteAction( HttpServletRequest request,                                   HttpServletResponse response )        throws ServletException, SecurityException, IOException {        // Init.        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        long[] ids = ServletHelper.getAllParamValuesAsLong( request, "id" );        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "The IDs of files to delete: " + StringHelper.toString( ids ) );        }        // Delete the file(s) by ID.        for( int i = 0; i < ids.length; i++ ) {            manager.deleteFile( ids[i] );        }    }    //    // Synchronizes the folders tree with the related database tables.    //    protected void doSyncAction( HttpServletRequest request,                                 HttpServletResponse response )        throws ServletException, SecurityException, IOException {        // Init.        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        // Do sync.        manager.syncDirectories();    }    //    // Synchronize files action    //    protected void doSyncFilesAction( HttpServletRequest request,                                      HttpServletResponse response )        throws IOException, SecurityException, ServletException {        // Init.        LogonSession ls = WebLoginManager.getLogonSession( request );        FileUploadManager manager = new FileUploadManager( ls );        // Do sync.        manager.syncFiles();    }    // ========================================================= Protected methods    // Output file content.    protected void outFile( FileManager.FileInfo fi,                            boolean saveOnClientSide,                            HttpServletResponse response )        throws ServletException, IOException {        if( logger.getLogger().isDebugEnabled() ) {            logger.DEBUG( "File data:" );            logger.DEBUG( "		path: " + fi.getFilePath() );            logger.DEBUG( "		save on client side?: " + saveOnClientSide );        }        if( fi.getData() != null ) {            if( saveOnClientSide ) {                File file = new File( fi.getFilePath() );                response.setHeader( "Content-Disposition", "attachment; filename=" + file.getName() );            }            response.setContentLength( fi.getData().length );            response.getOutputStream().write( fi.getData() );        } else {            response.setStatus( HttpServletResponse.SC_NO_CONTENT );        }    }    // Output file content.    protected void outFile( byte[] data,                            HttpServletResponse response )        throws ServletException, IOException {        if( data != null ) {            response.setContentLength( data.length );            response.getOutputStream().write( data );        } else {            response.setStatus( HttpServletResponse.SC_NO_CONTENT );        }    }    // Get the file items from thePOST HTTP request.    protected List getPostItems( HttpServletRequest request )        throws ServletException {        List items = null;        try {            DiskFileUpload fileUpload = new DiskFileUpload();            fileUpload.setSizeMax( maxUploadSize );            items = fileUpload.parseRequest( request );        } catch( FileUploadException ex ) {            throw new ServletException( ex );        }        return items;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -