Friday, December 31, 2004

basic upload servlet

well any web developer worth thier salt knows this little bit:


courtesy servlets.com

<HTML>
<p>Demo MultipartParser Upload Servlet</p>
<FORM ACTION="/tomcat/servlet/parserupload" METHOD=POST ENCTYPE="multipart/form-data">
What is your name? <INPUT TYPE=TEXT NAME=submitter> <BR>
Which file to upload? <INPUT TYPE=FILE NAME=file1> <BR>
Which file to upload? <INPUT TYPE=FILE NAME=file2> <BR>
Which file to upload? <INPUT TYPE=FILE NAME=file3> <BR>
<INPUT TYPE=SUBMIT>
</FORM>

/*
* DemoParserUploadServlet.java
*
* Example servlet to handle file uploads using MultipartParser for
* decoding the incoming multipart/form-data stream
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import com.oreilly.servlet.multipart.*;

public class DemoParserUploadServlet extends HttpServlet {
private File dir;

public void init(ServletConfig config) throws ServletException {
super.init(config);
// Read the uploadDir from the servlet parameters
String dirName = config.getInitParameter("uploadDir");
if (dirName == null) {
throw new ServletException("Please supply uploadDir parameter");
}
dir = new File(dirName);
if (! dir.isDirectory()) {
throw new ServletException("Supplied uploadDir " + dirName +
" is invalid");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
out.println("Demo Parser Upload Servlet");

try {
MultipartParser mp = new MultipartParser(request, 10*1024*1024)!
; // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) {
// it's a parameter part
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue();
out.println("param; name=" + name + ", value=" + value);
}
else if (part.isFile()) {
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
out.println("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePart.getFilePath() +
", content type=" + filePart.getContentType() +
", size=" + size);
}
else {
!
// the field did not contain a file
out.println("file; name=" + name + "; EMPTY");
}
out.flush();
}
}
}
catch (IOException lEx) {
this.getServletContext().log(lEx, "error reading or saving file");
}
}
}


The issue with this is that you cant parse the request twice, which you may want to do if you want to encode the directions to the action in the result header, then process the request during the action itself in a standard MVC approach. This isn't a problem when using request.getParameter("action"), because you can get parameters out of the request as many times as you want, but since you have to parse the request (or so it seems) when you try to parse it a second time it gives an exception that it has unexpected end of header.

My solution is to create a generic form handler class, that reads the request at the beginning of processRequest(),!
then is made available as a request attribute. It will handel both regular and multipart forms.

If anyone knows a better way please email me!


cgraham@newobjectivity.com





No comments: