📄 attachmentservlet.java
字号:
if( in != null ) in.close(); if( out != null ) out.close(); } } } /** * Grabs mime/multipart data and stores it into the temporary area. * Uses other parameters to determine which name to store as. * * <p>The input to this servlet is generated by an HTML FORM with * two parts. The first, named 'page', is the WikiName identifier * for the parent file. The second, named 'content', is the binary * content of the file. */ public void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException { try { String nextPage = upload( req ); req.getSession().removeAttribute("msg"); res.sendRedirect( nextPage ); } catch( RedirectException e ) { req.getSession().setAttribute("msg", e.getMessage()); res.sendRedirect( e.getRedirect() ); } } /** * Uploads a specific mime multipart input set, intercepts exceptions. * * @return The page to which we should go next. */ protected String upload( HttpServletRequest req ) throws RedirectException, IOException { String msg = ""; String attName = "(unknown)"; String errorPage = m_engine.getURL( WikiContext.ERROR, "", null, false ); // If something bad happened, Upload should be able to take care of most stuff String nextPage = errorPage; try { MultipartRequest multi; multi = new MultipartRequest( null, // no debugging req.getContentType(), req.getContentLength(), req.getInputStream(), m_tmpDir, Integer.MAX_VALUE, m_engine.getContentEncoding() ); nextPage = multi.getURLParameter( "nextpage" ); String wikipage = multi.getURLParameter( "page" ); WikiContext context = m_engine.createContext( req, WikiContext.UPLOAD ); errorPage = context.getURL( WikiContext.UPLOAD, wikipage ); // // FIXME: This has the unfortunate side effect that it will receive the // contents. But we can't figure out the page to redirect to // before we receive the file, due to the stupid constructor of MultipartRequest. // if( req.getContentLength() > m_maxSize ) { // FIXME: Does not delete the received files. throw new RedirectException( "File exceeds maximum size ("+m_maxSize+" bytes)", errorPage ); } UserProfile user = context.getCurrentUser(); // // Go through all files being uploaded. // Enumeration files = multi.getFileParameterNames(); while( files.hasMoreElements() ) { String part = (String) files.nextElement(); File f = multi.getFile( part ); AttachmentManager mgr = m_engine.getAttachmentManager(); InputStream in; try { // // Is a file to be uploaded. // String filename = multi.getFileSystemName( part ); if( filename == null || filename.trim().length() == 0 ) { log.error("Empty file name given."); throw new RedirectException("Empty file name given.", errorPage); } // // Should help with IE 5.22 on OSX // filename = filename.trim(); // // Remove any characters that might be a problem. Most // importantly - characters that might stop processing // of the URL. // filename = StringUtils.replaceChars( filename, "#?\"'", "____" ); log.debug("file="+filename); // // Attempt to open the input stream // if( f != null ) { in = new FileInputStream( f ); } else { // // This happens onl when the size of the // file is small enough to be cached in memory // in = multi.getFileContents( part ); } if( in == null ) { log.error("File could not be opened."); throw new RedirectException("File could not be opened.", errorPage); } // // Check whether we already have this kind of a page. // If the "page" parameter already defines an attachment // name for an update, then we just use that file. // Otherwise we create a new attachment, and use the // filename given. Incidentally, this will also mean // that if the user uploads a file with the exact // same name than some other previous attachment, // then that attachment gains a new version. // Attachment att = mgr.getAttachmentInfo( wikipage ); if( att == null ) { att = new Attachment( wikipage, filename ); } if( !m_engine.pageExists(att.getParentName()) ) { throw new RedirectException("Parent page does not exist.", errorPage ); } // // Check if we're allowed to do this? // if( m_engine.getAuthorizationManager().checkPermission( att, user, "upload" ) ) { if( user != null ) { att.setAuthor( user.getName() ); } m_engine.getAttachmentManager().storeAttachment( att, in ); log.info( "User " + user + " uploaded attachment to " + wikipage + " called "+filename+", size " + multi.getFileSize(part) ); } else { throw new RedirectException("No permission to upload a file", errorPage); } } finally { if( f != null ) f.delete(); } } // Inform the JSP page of which file we are handling: // req.setAttribute( ATTR_ATTACHMENT, wikiname ); } catch( ProviderException e ) { msg = "Upload failed because the provider failed: "+e.getMessage(); log.warn( msg + " (attachment: " + attName + ")", e ); throw new IOException(msg); } catch( IOException e ) { // Show the submit page again, but with a bit more // intimidating output. msg = "Upload failure: " + e.getMessage(); log.warn( msg + " (attachment: " + attName + ")", e ); throw e; } finally { // FIXME: In case of exceptions should absolutely // remove the uploaded file. } return nextPage; } /** * Produces debug output listing parameters and files. */ /* private void debugContentList( MultipartRequest multi ) { StringBuffer sb = new StringBuffer(); sb.append( "Upload information: parameters: [" ); Enumeration params = multi.getParameterNames(); while( params.hasMoreElements() ) { String name = (String)params.nextElement(); String value = multi.getURLParameter( name ); sb.append( "[" + name + " = " + value + "]" ); } sb.append( " files: [" ); Enumeration files = multi.getFileParameterNames(); while( files.hasMoreElements() ) { String name = (String)files.nextElement(); String filename = multi.getFileSystemName( name ); String type = multi.getContentType( name ); File f = multi.getFile( name ); sb.append( "[name: " + name ); sb.append( " temp_file: " + filename ); sb.append( " type: " + type ); if (f != null) { sb.append( " abs: " + f.getPath() ); sb.append( " size: " + f.length() ); } sb.append( "]" ); } sb.append( "]" ); log.debug( sb.toString() ); } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -