import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class fUp extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html;charset=gb2312";
  /**Initialize global variables*/
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  /**Process the HTTP Get request*/
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Servlet1</title></head>");
    out.println("<body>");
    out.println("<p>Upload Servlet</p>");
    out.println("<form name=f1 method=post enctype=multipart/form-data "
     //+"action=\"http://localhost:7801\""
     //+" action=\"http://test10.cnuol.com:7001/examples/servlet/fUp\""
     //+"action=\"http://test10.cnuol.com:7801\""
     +">"
     +"<input type=text name=hihi value=hehe><br>"
     +"&Icirc;&Auml;&frac14;&thorn;&Acirc;·&frac34;&para;&pound;&ordm;<input type=text name=path value='c:\'><br>"
     +"<input type=file name=abc><br>\n"
     +"<input type=text name=h value=hehe><br>"
     +"<input type=text name=hih value=hehe><br>"     +"<input type=submit name=sub1 value=UPLOAD>\n"
    );
    out.println("</form>");
    out.println("</body></html>");
  }
  /**Process the HTTP Post request*/
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /*String var0 = "";
    try {
      var0 = request.getParameter("abc");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
ServletOutputStream out=null;
DataInputStream in=null;
FileOutputStream fileOut=null;
//*/
FileOutputStream fileOut=null;
ServletInputStream in=null;
ServletOutputStream oo=null;
    response.setContentType(CONTENT_TYPE);
    //out=response.getOutputStream();
PrintWriter out = response.getWriter();
//String bound=
try{
in=request.getInputStream();
}
catch(Exception e)
{
e.printStackTrace();
}
byte bb[]=new byte[request.getContentLength()];
try
{
int allSize=request.getContentLength();
int totalRead=0;
int currentRead=0;
while(totalRead<allSize)
{
//currentRead=totalRead+in.available();
currentRead=in.read(bb,totalRead,allSize);
totalRead += currentRead; }
}
catch(Exception e)
{
e.printStackTrace();
}
String ss=new String(bb,"ISO8859_1");
//out.println(request.getContentType());
//out.println(request.getParameter("hihi")+"<br>"); //oo.println(in);
String contentType = request.getContentType();
String bound = contentType.substring(contentType.lastIndexOf('=')+1);
bound = "--"+bound;
//out.println("bound is "+bound+"<br><hr>");
if(contentType != null && contentType.indexOf("multipart/form-data") == -1)
{
out.println("oh,Your POST Form is not encoding we needed.sososo,i fuck multipart/form-data");
return;
}
String tmp=ss.substring(ss.indexOf('\n')+1); //out.print(tmp);
String title=new String("&Icirc;&Auml;&frac14;&thorn;&Otilde;&yacute;&Icirc;&Auml;".getBytes("gb2312"),"ISO8859_1");
int bbb=tmp.indexOf(bound);
while(bbb!=-1)
{
String t=tmp.substring(0,bbb);
//out.print(t+"<hr>");
tmp=tmp.substring(bbb);
if(t.indexOf("filename=")!=-1)
{
String fn=t.substring(0,t.indexOf('\n'));
fn=fn.substring(fn.indexOf("\"")+1,fn.lastIndexOf("\""));
fn=fn.substring(fn.lastIndexOf("\\")+1);
File fs=new File("c:\\"+fn);
//if(fn.exist
t=t.substring(t.indexOf('\n')+1);
//out.print(t.substring(0,t.indexOf('\n'))+"<BR>");
t=t.substring(t.indexOf('\n')+1);
t=t.substring(t.indexOf('\n')+1,t.lastIndexOf('\n')-1);
fileOut=new FileOutputStream("c:\\"+fn);
out.print(fn);
fileOut.write(t.getBytes("ISO8859_1"),0,t.length());
fileOut.close(); //out.print(title+":<hr>"+t+"<BR>");
}
tmp=tmp.substring(tmp.indexOf('\n')+1);
bbb=tmp.indexOf(bound);
t=null;
}
// String file=ss.substring(indexOf('\n')+1, /** append the bound to split the file data **/
/*for(int i=0;i<ss.length();i++)
{
if(ss.charAt(i)=='\n')
out.print("<br>");
else
out.print(ss.charAt(i)); }*/
/*out.println("<html>");
    out.println("<head><title>Servlet1</title></head>");
    out.println("<body>");
    out.println("<p>The servlet has received a POST. This is the reply.</p>"+var0);
    out.println("</body></html>");
    */
  }
 /**Clean up resources*/
  public void destroy() {
  }
}