请问各位,我在网上找到一段用JAVA写的CGI程序,不知道怎么用?
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoadFile extends HttpServlet
{
public LoadFile ()
{
}
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String workingpath = "c:\\upload\\";
javax.servlet.ServletInputStream DATA = req.getInputStream();
int FormSize = req.getContentLength() - 46;
byte buf[] = null;
int KB = 1024;
KB = KB <= FormSize ? KB : FormSize;
buf = new byte[KB];
DataInputStream is;
try
{
is = new DataInputStream(DATA);
is.read(buf);
}
catch(IOException e)
{
System.out.println(e.toString());
return;
}
String toke = "\"";
String temp = "";
int start1 = 0;
int start2 = 0;
int start3 = 0;
long i = 0L;
try
{
temp = new String(buf, "ISO8859_1");
}
catch(UnsupportedEncodingException _ex)
{
return;
}
start1 = temp.indexOf("filename");
temp = temp.substring(start1);
start3 = start1;
start1 = temp.indexOf(toke);
temp = temp.substring(start1 + 1);
start3 = start3 + start1 + 1;
start1 = temp.indexOf(toke);
String filename = temp.substring(0, start1);
filename = new String(filename.getBytes("ISO8859_1"));
FileOutputStream os;
try
{
File f1 = new File(workingpath + filename);
os = new FileOutputStream(f1);
}
catch(IOException _ex)
{
return;
}
start2 = temp.indexOf("stream");
start2 += 10;
start3 += start2;
FormSize -= start3;
KB *= 10;
buf = new byte[KB];
int iTimes = FormSize / KB;
int iMode = FormSize % KB;
is.mark(0);
is.reset();
is.skip(start3);
for(i = 0L; i < (long)iTimes; i++)
{
is.read(buf);
os.write(buf);
}
buf = new byte[iMode];
is.read(buf);
os.write(buf);
os.close();
ServletOutputStream out = res.getOutputStream();
out.println("");
}
}