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

📄 classfileserver.java

📁 身份认证和数字签名在实际应用中是通过以数字证书为核心的公开密钥基础结构(PKI)来实现的
💻 JAVA
字号:
package com.zsusoft.zfl;/***   China Zhongshan University Software Research Institute fszfl borrow and revise.*					     02/05/16*   中国中山大学软件研究所 佛山张峰岭借用和修改部分代码*                                            2002-5***//* * @(#)ClassFileServer.java	1.3 00/06/21 * * Copyright 1995-1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */import java.io.*;import java.net.*;import java.security.KeyStore;import javax.net.*;import javax.net.ssl.*;import javax.security.cert.X509Certificate;//import com.sun.net.ssl.*;/* ClassFileServer.java -- a simple file server that can server * Http get request in both clear and secure channel * * The ClassFileServer implements a ClassServer that * reads files from the file system. See the * doc for the "Main" method for how to run this * server. */public class ClassFileServer extends ClassServer {    private String docroot;    private static int DefaultServerPort = 2001;    /**     * Constructs a ClassFileServer.     *     * @param path the path where the server locates files     */    public ClassFileServer(ServerSocket ss, String docroot) throws IOException    {	super(ss);	this.docroot = docroot;    }    public ClassFileServer(ServerSocket ss, String docroot, SSL_with_signature sign) throws IOException    {    	super(ss, sign);	this.docroot = docroot;    }    /**     * This Constructs     /**     * Returns an array of bytes containing the bytes for     * the file represented by the argument <b>path</b>.     *     * @return the bytes for the file     * @exception FileNotFoundException if the file corresponding     * to <b>path</b> could not be loaded.     */    public byte[] getBytes(String path)	throws IOException    {	System.out.println("reading: " + path);	File f = new File(docroot + File.separator + path);	int length = (int)(f.length());	if (length == 0) {	    //throw new IOException("File length is zero: " + path);	    System.err.println("File length is zero: " + path);	    return null;	} else {	    FileInputStream fin = new FileInputStream(f);	    DataInputStream in = new DataInputStream(fin);	    byte[] bytecodes = new byte[length];	    in.readFully(bytecodes);	    return bytecodes;	}    }    /**     * Main method to create the class server that reads     * files. This takes two command line arguments, the     * port on which the server accepts requests and the     * root of the path. To start up the server: <br><br>     *     * <code>   java ClassFileServer <port> <path>     * </code><br><br>     *     * <code>   new ClassFileServer(port, docroot);     * </code>     */    public static void main(String args[])    {	System.out.println(	    "USAGE: java ClassFileServer port docroot [TLS [true]]");	System.out.println("");	System.out.println(	    "If the third argument is TLS, it will start as\n" +	    "a TLS/SSL file server, otherwise, it will be\n" +	    "an ordinary file server. \n" +	    "If the fourth argument is true,it will require\n" +	    "client authentication as well.");	int port = DefaultServerPort;	String docroot = "";	if (args.length >= 1) {	    port = Integer.parseInt(args[0]);	}	if (args.length >= 2) {	    docroot = args[1];	}	String type = "PlainSocket";	if (args.length >= 3) {	    type = args[2];	}	try {	    ServerSocketFactory ssf =		ClassFileServer.getServerSocketFactory(type);	    ServerSocket ss = ssf.createServerSocket(port);	    if (args.length >= 4 && args[3].equals("true")) {		((SSLServerSocket)ss).setNeedClientAuth(true);	    }	    new ClassFileServer(ss, docroot);	} catch (IOException e) {	    System.out.println("Unable to start ClassServer: " +			       e.getMessage());	    e.printStackTrace();	}    }    private static ServerSocketFactory getServerSocketFactory(String type) {	if (type.equals("TLS")) {	    SSLServerSocketFactory ssf = null;	    try {		// set up key manager to do server authentication		SSLContext ctx;		KeyManagerFactory kmf;		KeyStore ks;		char[] passphrase = "passphrase".toCharArray();		ctx = SSLContext.getInstance("TLS");		kmf = KeyManagerFactory.getInstance("SunX509");		ks = KeyStore.getInstance("JKS");		ks.load(new FileInputStream("testkeys"), passphrase);		kmf.init(ks, passphrase);		ctx.init(kmf.getKeyManagers(), null, null);		ssf = ctx.getServerSocketFactory();		return ssf;	    } catch (Exception e) {		e.printStackTrace();	    }	} else {	    return ServerSocketFactory.getDefault();	}	return null;    }}

⌨️ 快捷键说明

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