골치아픈 부분입니다. 특히 한글문제에 관해서요. 순수 자바쪽 기술을 써야하는데요 ^^; 저 식은 땀 납니다. 일단 1998년에 만들어진 소스를 봅시다.
FileUpload.java |
/***********************************************************
/* 화일 업로드 파라메터 골라내기, 업로드하기
/* 1998/02/10 제작자 : 김용대
/* copyright(c) JavaLand(http://javaland.cgiserver.net)
/* 사용시 출처를 밝혀주기 바랍니다.
/***********************************************************/
import java.io.*;
public class FileUpload{
private String fname;
private String boundary;
String param;
DataInputStream in;
public FileUpload(InputStream instream)
{
this.in=new DataInputStream(instream);
try{
boundary=in.readLine();
}catch(IOException e){
System.out.println(e.getMessage());
in=null;
}
}
// 델리미터를 넘긴다.
public String getDelimeter()
{
return boundary;
}
public String getDelimeter(InputStream instream)
{
this.in=new DataInputStream(instream);
try{
boundary=in.readLine();
}catch(IOException e){
System.out.println(e.getMessage());
in=null;
}
return boundary;
}
// 변수에 대한 값을 棘爭슈?
public String getParameter(String in_name) throws IOException
{
String str;
while((str=in.readLine())!=null){
if(str.indexOf("name=") != -1)
{
int nS=str.indexOf("name=");
int nE=str.indexOf(""",nS+6);
param=str.substring(nS+6,nE);
if(param.equals(in_name))
{
str=in.readLine();
str=in.readLine();
return str;
}
}
}
return null;
}
public boolean getParameter(OutputStream out,String in_name)
throws IOException
{
String str;
while((str=in.readLine())!=null){
if(str.indexOf("name=") != -1)
{
int nS=str.indexOf("name=");
int nE=str.indexOf(""",nS+6);
param=str.substring(nS+6,nE);
if(param.equals(in_name))
{
str=in.readLine();
if(readParameter(out))
return true;
}
}
}
return false;
}
public String getFileName() throws IOException
{
String str;
int nS;
int nE;
while((str=in.readLine())!=null){
if(str.indexOf("filename=""") != -1)
{
str=in.readLine();
return null;
}
if(str.indexOf("filename=") != -1)
{
nS=str.indexOf("filename=");
nE=str.indexOf(""",nS+10);
fname=str.substring(nS+10,nE);
if(fname.lastIndexOf("\") != -1){
fname=fname.substring(fname. lastIndexOf("\")+1);
return fname;
}
}
}
return null;
}
public boolean UpFile(OutputStream Out) throws IOException
{
String str;
while((str=in.readLine())!=null)
{
if(str.indexOf("Content-Type") != -1)
{
str=in.readLine();
if(readParameter(Out))
return true;
}
}
return false;
}
public boolean readParameter( OutputStream Out )
{
byte[] buffer=new byte[1024];
byte[] tbuffer=new byte[boundary.length()+1];
byte tm;
int x=0;
try{
for(;;)
{
buffer[x++]=tm=in.readByte();
if(x==boundary.length()+1)
{
int y=0;
String temp=new String(buffer,0,x);
if((y=temp.indexOf(boundary))!= -1)
{
x=y;
if(x!=0)
Out.write(buffer,0,x-1);
return true;
}
}
else{
if((x==1023) || (tm =='
')){
Out.write(buffer,0,x);
x=0;
}
} //end of if/else
} //end of for
}catch(Exception e){
System.out.println("Error : "+e.toString());
}
return false;
}
} |
이 소스를 보면서 이런 생각을 했습니다. 저 때 나는 뭘했나. 그때 시작했어도 지금 이 모양 이 꼴은 아닌데~ 라는 푸념섞인 넋두리, 이 때 필자는 뭐했냐면요. HTML좀 안다고 깝작댈 때였습니다. 그 때 자바를 팠어야 했는데... 꺼이꺼이... 다시 강좌로 들어가죠. 이 소스를 FileUpload.java 라는 이름으로 저장을 합니다. WEB-INF/classes 디렉토리에 저장합니다. 도스창에서
javac FileUpload.java
라고 치면 메시지가 2줄정도 뜨면서 컴파일이 됩니다. 3줄 이상 나오면 오타있는 것이고, 이 과정이 귀찮은 분들을 위해서 컴파일된 파일을 준비했습니다. [다운받기]
( 참고로 권장학습태도: 직접 타이핑하고, 컴파일하다가 에라보고 오타 수정하면서 어떻게 하면 에러가 뜨는지 공부하는 반 무대뽀형. ^^ 제가 이럽니다. copy & paste를 잘 해도 내가 다루는 소스가 어떤 구조로 돌아가는지 무진장 궁금해 합니다. 이거 싫으면 개발자 적성이 아니라고 감히 말씀드리고 싶네요. 퍽! 읔# 잔소리 하지 말구 강의나 하라구엽. T_T; 알거씀당~ 쉬불렁쉬불렁! 퍽. 크헉! 애구.)
일단 굵게 표시한 클래스와 메소드 이름을 유심히 보시고, 블럭별로 어떤 구조이고, 어떤 객체가 사용되었으며, 어떤 흐름이 있는지 개별적으로 보세요. 지금은 아까 저 때린 사람들 때문에 진도를 계속 나가야 되거든요. ^^#;
(모범생들을 위해: 위 소스는 jdk 1.1 버전에서 개발된 것이라 deprecated(걸레취급) 된 객체들이 있습니다. java api 를 뒤져보면 readLine() 객체는 java2(ver 1.2 이상)에서는 java.io.DataInputStream의 객체로 사용되는 것보다 java.io.BufferedReader 객체로 사용하는 것을 권장하는 것입니다. javac -deprecation FileUpload.java 해보면 소스에서 어떤 라인에 메소드가 deprecated 된 것인지 볼 수 있으며 api를 찾아보면 무엇으로 대체해야 하는지 설명이 '영어로' 친절하게 나와있습니다. 왜 안고치냐구요? 시도해봤죠. 한계땜시 안되는 걸 어떻합니까... 흑흑T.T 저도 초봅니다. 초보. 간큰 초보.) 작업화면
다음은 이 FileUpload.class 를 이용한 서블릿 소스입니다. 다음 페이지에서 보시죠.
다음으로 |