📄 attachmentservlet.java
字号:
} in.close(); out.close(); if(log.isDebugEnabled()) { msg = "Attachment "+att.getFileName()+" sent to "+req.getRemoteUser()+" on "+req.getRemoteHost(); log.debug( msg ); } if( nextPage != null ) res.sendRedirect( nextPage ); return; } else { msg = "Attachment '" + page + "', version " + ver + " does not exist."; } } catch( ProviderException pe ) { msg = "Provider error: "+pe.getMessage(); } catch( NumberFormatException nfe ) { msg = "Invalid version number (" + version + ")"; } catch( IOException ioe ) { msg = "Error: " + ioe.getMessage(); } } log.info( msg ); if( nextPage != null ) res.sendRedirect( nextPage ); } /** * 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 'wikiname', is the WikiName identifier * for the attachment. The second, named 'content', is the binary * content of the file. * * <p>After handling, the request is forwarded to m_resultPage. */ public void doPost( HttpServletRequest req, HttpServletResponse res ) throws IOException, ServletException { String nextPage = upload( req ); log.debug( "Forwarding to " + nextPage ); res.sendRedirect( nextPage ); } /** * Uploads a specific mime multipart input set, intercepts exceptions. * * @return The page to which we should go next. */ // FIXME: Error reporting is non-existent - the user gets no feedback whatsoever. protected String upload( HttpServletRequest req ) { String msg = ""; String attName = "(unknown)"; String nextPage = m_engine.getBaseURL()+"Error.jsp"; // If something bad happened. try { // MultipartRequest multi = new ServletMultipartRequest( req, m_tmpDir, Integer.MAX_VALUE ); MultipartRequest 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 ); 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; // // Is a file to be uploaded. // String filename = multi.getFileSystemName( part ); if( filename == null || filename.trim().length() == 0 ) { log.error("Empty file name given."); return nextPage; } // // Should help with IE 5.22 on OSX // filename = filename.trim(); // // Attempt to open the input stream // if( f != null ) { in = new FileInputStream( f ); } else { in = multi.getFileContents( part ); } if( in == null ) { log.error("File could not be opened."); return nextPage; } // // 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 ); } // // 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 { log.info("Upload failed due to missing permissions"); } 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 ); } 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 ); } 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 + -