*** empty log message ***
This commit is contained in:
132
src/com/silverwrist/util/test/FormDataTest.java
Normal file
132
src/com/silverwrist/util/test/FormDataTest.java
Normal file
@@ -0,0 +1,132 @@
|
||||
package com.silverwrist.util.test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
import com.silverwrist.util.*;
|
||||
|
||||
public class FormDataTest extends HttpServlet
|
||||
{
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<HTML><HEAD><TITLE>Form Test</TITLE></HEAD><BODY>");
|
||||
out.println("<H1>Form Test</H1>");
|
||||
out.println("<FORM METHOD=POST ENCTYPE=\"multipart/form-data\" ACTION=\"/venice/testformdata\">");
|
||||
out.println("Text field: <INPUT TYPE=TEXT NAME=\"text\" VALUE=\"foo\" SIZE=32 MAXLENGTH=128><P>");
|
||||
out.println("File field: <INPUT TYPE=FILE NAME=\"file\" SIZE=32 MAXLENGTH=255><P>");
|
||||
out.println("<INPUT TYPE=SUBMIT VALUE=\"sumbit\">");
|
||||
out.println("</FORM></BODY></HTML>");
|
||||
|
||||
} // 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
|
||||
{
|
||||
final String save_dir = "/home/erbo";
|
||||
String errmsg = null;
|
||||
ServletMultipartHandler smh = null;
|
||||
boolean file_check = false;
|
||||
String text_value = null;
|
||||
String file_name = null;
|
||||
String file_type = null;
|
||||
|
||||
try
|
||||
{ // create the multipart handler
|
||||
smh = new ServletMultipartHandler(request);
|
||||
text_value = smh.getValue("text");
|
||||
file_check = smh.isFileParam("file");
|
||||
if (file_check)
|
||||
{ // copy out to file
|
||||
file_name = smh.getValue("file");
|
||||
file_type = smh.getContentType("file");
|
||||
if ((file_name!=null) && (file_name.length()>0))
|
||||
{ // build the file name and save it
|
||||
FileOutputStream stm = new FileOutputStream(save_dir + "/" + file_name);
|
||||
InputStream in = smh.getFileContentStream("file");
|
||||
byte[] copybuf = new byte[1024];
|
||||
int ct = in.read(copybuf);
|
||||
while (ct>=0)
|
||||
{ // copy loop...
|
||||
if (ct>0)
|
||||
stm.write(copybuf,0,ct);
|
||||
ct = in.read(copybuf);
|
||||
|
||||
} // end while
|
||||
|
||||
stm.close();
|
||||
in.close();
|
||||
|
||||
} // end if
|
||||
|
||||
} // end if
|
||||
|
||||
} // end try
|
||||
catch (ServletMultipartException e)
|
||||
{ // load error message
|
||||
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)
|
||||
{ // print data
|
||||
out.println("Text value: " + text_value + "<P>");
|
||||
if (file_check)
|
||||
{ // get file name and type
|
||||
out.println("File name: " + file_name + "<P>");
|
||||
out.println("File type: " + file_type + "<P>");
|
||||
out.println("Saved to directory: " + save_dir + "<P>");
|
||||
|
||||
} // end if
|
||||
else
|
||||
out.println("Not a file.<P>");
|
||||
|
||||
} // end if
|
||||
else
|
||||
out.println("ERROR: " + errmsg + "<P>");
|
||||
out.println("<A HREF=\"/venice/testformdata\">Go back.</A>");
|
||||
out.println("</BODY></HTML>");
|
||||
|
||||
} // end doPost
|
||||
|
||||
} // end class FormDataTest
|
||||
121
src/com/silverwrist/util/test/TestMultiHandler.java
Normal file
121
src/com/silverwrist/util/test/TestMultiHandler.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.silverwrist.util.test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.activation.DataSource;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.servlet.*;
|
||||
|
||||
public class TestMultiHandler
|
||||
{
|
||||
private MimeMultipart multipart;
|
||||
private int count;
|
||||
private Vector params;
|
||||
|
||||
static class ServletDataSource implements DataSource
|
||||
{
|
||||
private ServletRequest request;
|
||||
private InputStream istm = null;
|
||||
|
||||
public ServletDataSource(ServletRequest request)
|
||||
{
|
||||
this.request = request;
|
||||
|
||||
} // end constructor
|
||||
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
if (istm==null)
|
||||
istm = request.getInputStream();
|
||||
return istm;
|
||||
|
||||
} // end getInputStream
|
||||
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
throw new IOException("tried to get OutputStream on servlet input?!?!?");
|
||||
|
||||
} // end getOutputStream
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
return request.getContentType();
|
||||
|
||||
} // end getContentType
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return "servlet";
|
||||
|
||||
} // end getName
|
||||
|
||||
} // end class ServletDataSource
|
||||
|
||||
public TestMultiHandler(ServletRequest request) throws IOException
|
||||
{
|
||||
if (!canHandle(request))
|
||||
throw new UnsupportedOperationException("data type doesn't match");
|
||||
|
||||
try
|
||||
{ // OK, this is the tricky part...
|
||||
ServletDataSource ds = new ServletDataSource(request);
|
||||
multipart = new MimeMultipart(ds);
|
||||
count = multipart.getCount();
|
||||
params = new Vector(count);
|
||||
// OK, that's enough for now
|
||||
|
||||
} // end try
|
||||
catch (MessagingException e)
|
||||
{ // better run like hell, 'cos this may not work
|
||||
throw new IOException("Got MessagingException: " + e.getMessage());
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end constructor
|
||||
|
||||
public static boolean canHandle(ServletRequest request)
|
||||
{
|
||||
String ctype = request.getContentType();
|
||||
return (ctype.startsWith("multipart/form-data"));
|
||||
|
||||
} // end canHandle
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
|
||||
} // end getCount
|
||||
|
||||
public void dump(PrintWriter out)
|
||||
{
|
||||
try
|
||||
{ // print out all parts
|
||||
int ct = multipart.getCount();
|
||||
out.println("Number of parts: " + String.valueOf(ct));
|
||||
|
||||
for (int i=0; i<ct; i++)
|
||||
{ // loop through all the parts
|
||||
out.println("--- PART " + String.valueOf(i) + " ---");
|
||||
MimeBodyPart part = (MimeBodyPart)(multipart.getBodyPart(i));
|
||||
out.println("got it");
|
||||
String foo = part.getContentType();
|
||||
out.println("Content type: " + foo);
|
||||
foo = part.getDisposition();
|
||||
out.println("Content disposition: " + foo);
|
||||
String[] disphdr = part.getHeader("Content-Disposition");
|
||||
for (int j=0; j<disphdr.length; j++)
|
||||
out.println("Content-Disposition header[" + String.valueOf(j) + "]: " + disphdr[j]);
|
||||
|
||||
} // end for
|
||||
|
||||
} // end try
|
||||
catch (MessagingException e)
|
||||
{ // whoops!
|
||||
out.println("THREW MessagingException: " + e.getMessage());
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end dump
|
||||
|
||||
} // end class TestMultiHandler
|
||||
Reference in New Issue
Block a user