Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
java
CouponDAO.java 1.0 kB 2 25-Aug-2011 08:38 122.174.94.204 dfgfd

This page (revision-12) was last changed on 21-Aug-2008 19:14 by JanneJalkanen  

This page was created on 26-Jan-2002 12:10 by 64.164.20.130

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Difference between version and

At line 1 added 7 lines
Prompted by Janne's download servlet, here's an example of
a servlet to load a file to the server. (I use it with a couple of
JSP pages. YMMV.)\\
(This code is still being refined - notice especially that you need to implement authentication and an incoming content handler.)
\\
\\
-- [ebu]
At line 9 added 198 lines
Also, the free [COS package|http://www.servlets.com/cos/index.html] from O'Reilly does this pretty easily. It does the obvious things easily, and is flexible enough to do strange things :).
-- [MahlenMorris]
COS is usable enough, but its license may not suit all needs.
Jason Pell's [Multipart Request|http://www.geocities.com/jasonpell/programs.html]
jar is a small, simple utility under the GPL.
--[ebu]
Note also that [Jakarta|http://jakarta.apache.org] now also have their own simple-to-use API for file uploads, the [Commons Fileupload|http://jakarta.apache.org/commons/fileupload/] project. Unfortunately, it still (09-Dev-2002) has far too much dependencies on other Commons projects that were split off [Struts]. For example, it requires the Commons Beanutils library, which in turn requires the entire Jakarta Logging package, which obviously is different from [log4j], so you end up with hundreds of K of code which you don't mostly use at all.
-- [JanneJalkanen].
----
{{{
package net.memecry.traveller;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
/**
Simple servlet for receiving a file and launching
a command, specified in settings, to run it.
*/
public class ReceptorServlet
extends HttpServlet
{
// Index of the one-time key code.
private static int m_ix = 0;
// Initialize a key set somehow:
private String[] m_keys = { "fee", "fie", "foe", "foo" };
public void init()
{
// read application init info
String storageDir = getInitParameter(INIT_STORAGE);
// ...and so on.
// In reality, you'd load authentication settings here, too.
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
ServletContext ctx = getServletContext();
RequestDispatcher rd = null;
String msg = null;
try
{
StreamDataSource sds = new StreamDataSource(req);
MimeMultipart multi = new MimeMultipart(sds);
// Check that an authentication key exists and is valid. We expect the key to
// be the first part of the MultiPart, the second the content.
// (This is pretty inflexible, tune it a bit...)
MimeBodyPart key = (MimeBodyPart)multi.getBodyPart(0);
MimeBodyPart content = (MimeBodyPart)multi.getBodyPart(1);
validateCode((String)(key.getContent())); // throws exception if failed
handleIncomingFile(content);
rd = ctx.getRequestDispatcher(successURLString);
msg = "The content has been handled and stored.";
}
catch(MessagingException e)
{
// We arrive here e.g. if the input was not a MultiPart.
// That's fine; add some output and show the submit page.
msg = "Welcome to the Traveller " +
"interface. Use the form to input content.";
// Maybe add some information to the context:
req.setAttribute(ATTR_CODEIX, "index: " + m_ix);
rd = ctx.getRequestDispatcher(submitURLString);
}
catch(Exception e)
{
// Show the submit page again, but with a bit more
// intimidating output.
msg = "Traveller failure: " + e.getMessage();
req.setAttribute(ATTR_CODEIX, "index: " + m_ix);
rd = ctx.getRequestDispatcher(submitURLString);
}
// add some messages to the request...
if(msg != null)
req.setAttribute(ATTR_MSG, msg);
// ...and forward it to the correct result page.
if(rd != null)
rd.forward(req, res);
}
private void handleIncomingFile(MimeBodyPart content)
throws MessagingException
{
// Implement your nice dynamic handling code here
}
/**
Trivial one-time key authentication example.
Compares the offered code string to the currently expected
passkey. Throws an IllegalAccessException if it is not valid.
The message contains information on the next key to send.
*/
private void validateCode(String code)
throws IllegalAccessException,
MessagingException
{
if(m_ix >= m_keys.length)
m_ix = 0;
if(m_keys[m_ix].equals(code))
m_ix++;
else
throw new IllegalAccessException("Next code index is " + m_ix + ".");
}
/**
* This class maps the request stream to the content parser that is
* able to pick files from it.
*/
private class StreamDataSource implements DataSource
{
private HttpServletRequest m_req;
public StreamDataSource(HttpServletRequest req)
{
m_req = req;
}
/**
* Returns the content type for the request stream.
*/
public String getContentType()
{
return m_req.getContentType();
}
/**
* Returns a stream from the request.
*/
public InputStream getInputStream()
throws IOException
{
return m_req.getInputStream();
}
/**
* This method is useless and it always returns a null.
*/
public String getName()
{
return null;
}
/**
Maps output to System.out. Do something more sensible here...
*/
public OutputStream getOutputStream()
{
return System.out;
}
}
}
}}}
----
To use the above, you need an HTML (well... I'm using JSP) page with something like this:
{{{<FORM ACTION="load" METHOD="POST" ENCTYPE="multipart/form-data">
Enter your key (<%= key %>):
<P>
<INPUT TYPE="text" name="<%= ReceptorServlet.PARAM_KEYCODE%>">
<P>
Upload your content:
<P>
<INPUT type="file" name="content">
<P>
<INPUT type="submit" name="enter" value="Ok">
</form>
}}}
Version Date Modified Size Author Changes ... Change note
12 21-Aug-2008 19:14 6.695 kB JanneJalkanen to previous
11 21-Aug-2008 12:33 6.706 kB 159.53.46.141 to previous | to last
10 10-May-2005 12:10 6.695 kB JanneJalkanen to previous | to last
9 27-Jun-2004 06:36 0.002 kB HappyJhkim to previous | to last
8 19-May-2004 17:13 6.697 kB JanneJalkanen to previous | to last
7 19-May-2004 15:32 6.711 kB 194.140.65.241 to previous | to last
6 08-Apr-2003 20:36 6.699 kB Ebu to previous | to last
5 08-Apr-2003 18:53 6.7 kB 64.69.191.231 to previous | to last
4 09-Dec-2002 00:24 6.701 kB JanneJalkanen to previous | to last
3 08-Dec-2002 22:23 6.116 kB JanneJalkanen to previous | to last
2 26-Jan-2002 12:10 5.908 kB 64.164.20.130 to previous | to last
1 26-Jan-2002 12:10 5.906 kB 64.164.20.130 to last
« This page (revision-12) was last changed on 21-Aug-2008 19:14 by JanneJalkanen