학교에서 교수가 강의하기 귀찮았는지 이런걸 시키고는 가버리는-_-;;
자바를 알지도 못하는데 자바로 하라고 -_- ㅆ ㅑ 0
아까워서 여따올린다.
import java.net.*;
import java.io.*;
import java.util.*;
class webserver
{
public static void main( String args[] )
{
System.out.println( "Web server start..." );
try
{
ServerSocket sSocket = new ServerSocket( 80 );
while ( true )
{
Socket cSocket = sSocket.accept();
BufferedReader br = ( new BufferedReader( ( new InputStreamReader( ( new BufferedInputStream( cSocket.getInputStream() ) ) ) ) ) );
// Get File name and extension
StringTokenizer st = new StringTokenizer( br.readLine() ); st.nextToken();
String FileName = st.nextToken();
if ( FileName.equalsIgnoreCase( "/" ) )
{
FileName = "/index.html";
}
FileName = FileName.substring( 1 );
st = new StringTokenizer( FileName, "." ); st.nextToken();
String FileExt = st.nextToken();
// Initialize Header
File file = new File( FileName );
int FileSize = FileSize = ( int )( file.length() );
String Header = "HTTP/1.0 ";
byte[] buffer = new byte[ FileSize ];
if ( file.exists() )
{
Header += ( "200 OK\r\nContent-Type: " + getContentType( FileExt ) + "\r\n" +
"Content-Length: " + FileSize + "\r\n" );
FileInputStream fis = new FileInputStream( FileName );
BufferedInputStream bis = new BufferedInputStream( fis );
bis.read( buffer, 0, FileSize );
bis.close(); fis.close();
}
else
{
Header += "404 Object Not Found\r\n";
}
Header += "Server: Tinyweb/0.1\r\n";
Header += "\r\n";
System.out.println( "Request-File: " + FileName );
System.out.println( Header );
OutputStream os = cSocket.getOutputStream();
os.write( Header.getBytes() );
os.write( buffer );
os.close();
cSocket.close();
}
}
catch ( Exception e )
{
System.out.println( e );
}
}
public static String getContentType( String Ext )
{
String cType = "text/plain";
if ( Ext.equalsIgnoreCase( "htm" ) )
{
Ext = "html";
}
if ( Ext.equalsIgnoreCase( "js" ) )
{
cType = "application/x-javascript";
}
else if ( Ext.equalsIgnoreCase( "exe" ) || Ext.equalsIgnoreCase( "vbs" ) || Ext.equalsIgnoreCase( "bat" ) || Ext.equalsIgnoreCase( "sys" ) )
{
cType = "application/octet-stream";
}
else if ( Ext.equalsIgnoreCase( "html" ) || Ext.equalsIgnoreCase( "css" ) )
{
cType = "text/" + Ext;
}
else if ( Ext.equalsIgnoreCase( "gif" ) || Ext.equalsIgnoreCase( "png" ) ||
Ext.equalsIgnoreCase( "jpeg" ) || Ext.equalsIgnoreCase( "jpg" ) ||
Ext.equalsIgnoreCase( "bmp" ) )
{
cType = "image/" + Ext;
}
return cType;
}
};
'Windows > 기타개발' 카테고리의 다른 글
[한글] PlaySound function (0) | 2016.06.24 |
---|---|
[CSS] input 태그 type 별 개별 적용 (0) | 2013.06.12 |
[Web] Script 로 @ url 구현하기 (0) | 2003.03.04 |
[소스] Object 의 이동을 아무 각도로나.. (0) | 2003.01.06 |