📄 networkexplorer.java
字号:
package jcifs.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.ListIterator;
import jcifs.Config;
import jcifs.UniAddress;
import jcifs.netbios.NbtAddress;
import jcifs.smb.DfsReferral;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbAuthException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbSession;
import jcifs.util.Base64;
import jcifs.util.LogStream;
import jcifs.util.MimeMap;
public class NetworkExplorer extends HttpServlet {
private static LogStream log = LogStream.getInstance();
private MimeMap mimeMap;
private String style;
private NtlmSsp ntlmSsp;
private boolean credentialsSupplied;
private boolean enableBasic;
private boolean insecureBasic;
private String realm, defaultDomain;
public void init() throws ServletException {
InputStream is;
StringBuffer sb = new StringBuffer();
byte[] buf = new byte[1024];
int n, level;
String name;
Config.setProperty( "jcifs.smb.client.soTimeout", "600000" );
Config.setProperty( "jcifs.smb.client.attrExpirationPeriod", "300000" );
Enumeration e = getInitParameterNames();
while( e.hasMoreElements() ) {
name = (String)e.nextElement();
if( name.startsWith( "jcifs." )) {
Config.setProperty( name, getInitParameter( name ));
}
}
if( Config.getProperty( "jcifs.smb.client.username" ) == null ) {
ntlmSsp = new NtlmSsp();
} else {
credentialsSupplied = true;
}
try {
mimeMap = new MimeMap();
is = getClass().getClassLoader().getResourceAsStream( "jcifs/http/ne.css" );
while(( n = is.read( buf )) != -1 ) {
sb.append( new String( buf, 0, n, "ISO8859_1" ));
}
style = sb.toString();
} catch( IOException ioe ) {
throw new ServletException( ioe.getMessage() );
}
enableBasic = Config.getBoolean("jcifs.http.enableBasic", false );
insecureBasic = Config.getBoolean("jcifs.http.insecureBasic", false );
realm = Config.getProperty("jcifs.http.basicRealm");
if (realm == null) realm = "jCIFS";
defaultDomain = Config.getProperty("jcifs.smb.client.domain");
if(( level = Config.getInt( "jcifs.util.loglevel", -1 )) != -1 ) {
LogStream.setLevel( level );
}
if( log.level > 2 ) {
try {
Config.store( log, "JCIFS PROPERTIES" );
} catch( IOException ioe ) {
}
}
}
protected void doFile( HttpServletRequest req,
HttpServletResponse resp, SmbFile file ) throws IOException {
byte[] buf = new byte[8192];
SmbFileInputStream in;
ServletOutputStream out;
String url, type;
int n;
in = new SmbFileInputStream( file );
out = resp.getOutputStream();
url = file.getPath();
resp.setContentType( "text/plain" );
if(( n = url.lastIndexOf( '.' )) > 0 &&
( type = url.substring( n + 1 )) != null &&
type.length() > 1 && type.length() < 6 ) {
resp.setContentType( mimeMap.getMimeType( type ));
}
resp.setHeader( "Content-Length", file.length() + "" );
resp.setHeader( "Accept-Ranges", "Bytes" );
while(( n = in.read( buf )) != -1 ) {
out.write( buf, 0, n );
}
}
protected int compareNames( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
if( f1.isDirectory() != f2.isDirectory() ) {
return f1.isDirectory() ? -1 : 1;
}
return f1name.compareToIgnoreCase( f2.getName() );
}
protected int compareSizes( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
long diff;
if( f1.isDirectory() != f2.isDirectory() ) {
return f1.isDirectory() ? -1 : 1;
}
if( f1.isDirectory() ) {
return f1name.compareToIgnoreCase( f2.getName() );
}
diff = f1.length() - f2.length();
if( diff == 0 ) {
return f1name.compareToIgnoreCase( f2.getName() );
}
return diff > 0 ? -1 : 1;
}
protected int compareTypes( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
String f2name, t1, t2;
int i;
if( f1.isDirectory() != f2.isDirectory() ) {
return f1.isDirectory() ? -1 : 1;
}
f2name = f2.getName();
if( f1.isDirectory() ) {
return f1name.compareToIgnoreCase( f2name );
}
i = f1name.lastIndexOf( '.' );
t1 = i == -1 ? "" : f1name.substring( i + 1 );
i = f2name.lastIndexOf( '.' );
t2 = i == -1 ? "" : f2name.substring( i + 1 );
i = t1.compareToIgnoreCase( t2 );
if( i == 0 ) {
return f1name.compareToIgnoreCase( f2name );
}
return i;
}
protected int compareDates( SmbFile f1, String f1name, SmbFile f2 ) throws IOException {
if( f1.isDirectory() != f2.isDirectory() ) {
return f1.isDirectory() ? -1 : 1;
}
if( f1.isDirectory() ) {
return f1name.compareToIgnoreCase( f2.getName() );
}
return f1.lastModified() > f2.lastModified() ? -1 : 1;
}
protected void doDirectory( HttpServletRequest req, HttpServletResponse resp, SmbFile dir ) throws IOException {
PrintWriter out;
SmbFile[] dirents;
SmbFile f;
int i, j, len, maxLen, dirCount, fileCount, sort;
String str, name, path, fmt;
LinkedList sorted;
ListIterator iter;
SimpleDateFormat sdf = new SimpleDateFormat( "MM/d/yy h:mm a" );
GregorianCalendar cal = new GregorianCalendar();
sdf.setCalendar( cal );
dirents = dir.listFiles();
if( log.level > 2 )
log.println( dirents.length + " items listed" );
sorted = new LinkedList();
if(( fmt = req.getParameter( "fmt" )) == null ) {
fmt = "col";
}
sort = 0;
if(( str = req.getParameter( "sort" )) == null || str.equals( "name" )) {
sort = 0;
} else if( str.equals( "size" )) {
sort = 1;
} else if( str.equals( "type" )) {
sort = 2;
} else if( str.equals( "date" )) {
sort = 3;
}
dirCount = fileCount = 0;
maxLen = 28;
for( i = 0; i < dirents.length; i++ ) {
try {
if( dirents[i].getType() == SmbFile.TYPE_NAMED_PIPE ) {
continue;
}
} catch( SmbAuthException sae ) {
if( log.level > 2 )
sae.printStackTrace( log );
} catch( SmbException se ) {
if( log.level > 2 )
se.printStackTrace( log );
if( se.getNtStatus() != se.NT_STATUS_UNSUCCESSFUL ) {
throw se;
}
}
if( dirents[i].isDirectory() ) {
dirCount++;
} else {
fileCount++;
}
name = dirents[i].getName();
if( log.level > 3 )
log.println( i + ": " + name );
len = name.length();
if( len > maxLen ) {
maxLen = len;
}
iter = sorted.listIterator();
for( j = 0; iter.hasNext(); j++ ) {
if( sort == 0 ) {
if( compareNames( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 1 ) {
if( compareSizes( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 2 ) {
if( compareTypes( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
} else if( sort == 3 ) {
if( compareDates( dirents[i], name, (SmbFile)iter.next() ) < 0 ) {
break;
}
}
}
sorted.add( j, dirents[i] );
}
if( maxLen > 50 ) {
maxLen = 50;
}
maxLen *= 9; /* convert to px */
out = resp.getWriter();
resp.setContentType( "text/html" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -