did some extra instrumentation of these files (with LOG4J), at the request of
Amos and Laurie at Narex
This commit is contained in:
@@ -4,10 +4,13 @@ import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
import org.apache.log4j.*;
|
||||
import com.silverwrist.util.*;
|
||||
|
||||
public class FormDataTest extends HttpServlet
|
||||
{
|
||||
private static Category logger = Category.getInstance(FormDataTest.class.getName());
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
@@ -22,41 +25,6 @@ public class FormDataTest extends HttpServlet
|
||||
|
||||
} // end doGet
|
||||
|
||||
private void old_doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
TestMultiHandler tmh = null;
|
||||
String errmsg = null;
|
||||
int count = -1;
|
||||
|
||||
try
|
||||
{ // attempt to launch the multi handler
|
||||
tmh = new TestMultiHandler(request);
|
||||
count = tmh.getCount();
|
||||
|
||||
} // end try
|
||||
catch (IOException e)
|
||||
{ // we got an error from the parser
|
||||
errmsg = e.getMessage();
|
||||
|
||||
} // end catch
|
||||
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<HTML><HEAD><TITLE>Form Test Results</TITLE></HEAD><BODY>");
|
||||
out.println("<H1>Form Test Results</H1>");
|
||||
if (errmsg==null)
|
||||
out.println("Success!<P>");
|
||||
else
|
||||
out.println("ERROR: " + errmsg + "<P>");
|
||||
out.println("Data count was: " + String.valueOf(count) + "<P>");
|
||||
out.println("<PRE>");
|
||||
tmh.dump(out);
|
||||
out.println("</PRE>");
|
||||
out.println("<A HREF=\"/venice/testformdata\">Go back.</A>");
|
||||
out.println("</BODY></HTML>");
|
||||
|
||||
} // end old_doPost
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
@@ -67,40 +35,70 @@ public class FormDataTest extends HttpServlet
|
||||
String text_value = null;
|
||||
String file_name = null;
|
||||
String file_type = null;
|
||||
int file_size = -1;
|
||||
|
||||
try
|
||||
{ // create the multipart handler
|
||||
smh = new ServletMultipartHandler(request);
|
||||
|
||||
// retrieve the text parameter value
|
||||
text_value = smh.getValue("text");
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("text param value = \"" + text_value + "\"");
|
||||
|
||||
// check to make sure the file parameter is really a file!
|
||||
file_check = smh.isFileParam("file");
|
||||
if (file_check)
|
||||
{ // copy out to file
|
||||
{ // get the file name and type
|
||||
file_name = smh.getValue("file");
|
||||
file_type = smh.getContentType("file");
|
||||
file_size = smh.getContentSize("file");
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("file param = \"" + file_name + "\", ctype = " + file_type + ", size = " + file_size);
|
||||
|
||||
if ((file_name!=null) && (file_name.length()>0))
|
||||
{ // build the file name and save it
|
||||
FileOutputStream stm = new FileOutputStream(save_dir + "/" + file_name);
|
||||
{ // build the file name and open a stream to save it
|
||||
String real_name = save_dir + "/" + file_name;
|
||||
FileOutputStream stm = new FileOutputStream(real_name);
|
||||
InputStream in = smh.getFileContentStream("file");
|
||||
byte[] copybuf = new byte[1024];
|
||||
|
||||
// go into a copy loop
|
||||
int ct = in.read(copybuf);
|
||||
int nbytes = 0;
|
||||
while (ct>=0)
|
||||
{ // copy loop...
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("read blksize = " + ct);
|
||||
if (ct>0)
|
||||
{ // write out copy buffer data
|
||||
stm.write(copybuf,0,ct);
|
||||
nbytes += ct;
|
||||
|
||||
} // end if
|
||||
|
||||
ct = in.read(copybuf);
|
||||
|
||||
} // end while
|
||||
|
||||
// all done - file saved
|
||||
stm.close();
|
||||
in.close();
|
||||
if (nbytes!=file_size)
|
||||
logger.warn("YIKES! expected " + file_size + " bytes but got " + nbytes + "!");
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("saved " + nbytes + " bytes to file " + real_name);
|
||||
|
||||
} // end if
|
||||
|
||||
} // end if
|
||||
else // this shouldn't happen
|
||||
logger.warn("file param was not a file?!?!?!?");
|
||||
|
||||
} // end try
|
||||
catch (ServletMultipartException e)
|
||||
{ // load error message
|
||||
logger.error("caught ServletMultipartException: " + e.getMessage(),e);
|
||||
errmsg = e.getMessage();
|
||||
|
||||
} // end catch
|
||||
@@ -115,6 +113,7 @@ public class FormDataTest extends HttpServlet
|
||||
{ // get file name and type
|
||||
out.println("File name: " + file_name + "<P>");
|
||||
out.println("File type: " + file_type + "<P>");
|
||||
out.println("File size: " + file_size + " bytes<P>");
|
||||
out.println("Saved to directory: " + save_dir + "<P>");
|
||||
|
||||
} // end if
|
||||
@@ -124,7 +123,7 @@ public class FormDataTest extends HttpServlet
|
||||
} // end if
|
||||
else
|
||||
out.println("ERROR: " + errmsg + "<P>");
|
||||
out.println("<A HREF=\"/venice/testformdata\">Go back.</A>");
|
||||
out.println("<A HREF=\"testformdata\">Go back.</A>");
|
||||
out.println("</BODY></HTML>");
|
||||
|
||||
} // end doPost
|
||||
|
||||
Reference in New Issue
Block a user