From: Chris v. S. <ch...@te...> - 2007-05-31 15:19:56
|
Hi David - You didn't say whether your code worked under releases of Jetty prior to 6.1.3... I'm using both Apache Commons FileUploader 1.1.1 and Commons IO 1.2 inside Jetty 6.0.1 with no problems. I have a base class that contains most of the uploader code from which I derive all of my servlets that handle uploads - as I recall (it's been a while) this is pretty much straight from the FileUpload docs: public class FileUploadServletBase extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init( config ); } public void destroy() { super.destroy(); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } protected boolean isMultipartData( HttpServletRequest req ) { ServletRequestContext reqCtx = new ServletRequestContext( req ); return ServletFileUpload.isMultipartContent( reqCtx ); } protected List getUploadedData( HttpServletRequest req ) throws FileUploadException, IOException { List<FileItem> items = null; // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints factory.setSizeThreshold( 4096 ); ServletContext servletCtx = getServletContext(); File tempFileDir = (File)servletCtx.getAttribute ( "javax.servlet.context.tempdir" ); factory.setRepository( tempFileDir ); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint (negative value indicates no limit) upload.setSizeMax( -1 ); // Parse the request items = upload.parseRequest( req ); return items; } } Then, I have a chunk of code in each servlet that extends this class that actually retrieves the data and processes it. Note that this code handles both form data and the uploaded file: if ( super.isMultipartData( req ) ) { try { // Call function in base servlet to process data List<FileItem> items = super.getUploadedData( req ); // Process the uploaded items Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem)iter.next(); if (item.isFormField()) { String name = item.getFieldName(); if ( name.equals( "fullPathName" ) ) { pathPart = item.getString(); } else if ( name.equals( "outputDir" ) ) { outputPath = item.getString(); } else if ( name.equals( "targetFormat" ) ) { targetFormat = item.getString(); } } else { String fieldName = item.getFieldName(); fileName = item.getName(); String contentType = item.getContentType(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); if ( isInMemory ) { iStream = new ByteArrayInputStream( item.get() ); } else { iStream = item.getInputStream(); } } } Please let me know if you need anything else. Cheers Chris On May 31, 2007, at 7:43 AM, David Harrigan wrote: > > Hi, > > Jetty 6.1.3 > Commons FileUploader 1.2 > Commons IO 1.3.1 > Java 6 (update 1) > > Got these packages, yet when I submit something, no data comes > thru. I'll > walk thru the code: > > First, my html page: > > <html> > <body> > <form action="http://localhost:8080/fubar-war/upload" > enctype="multipart/form-data" method="post"> > <input type="file"/> > <input type="submit"/> > </form> > </body> > </html> > > I'm uploading a jpg that is 50kb in size. > > Now the servlet (cropped): > > public void doPost(final HttpServletRequest request, final > HttpServletResponse response) { > if (ServletFileUpload.isMultipartContent(request)) { <--- THIS > RETURNS TRUE > final FileItemFactory fileItemFactory = new > DiskFileItemFactory(); > final ServletFileUpload upload = new > ServletFileUpload(fileItemFactory); > try { > final List<FileItem> fileItems = upload.parseRequest > (request); > <--- fileItems SIZE == 0 > processFileItems(fileItems); > } catch(final FileUploadException e) { > logger.error("Error whilst processing the upload", e); > } > } > } > > Since fileItems == 0, then nothing is processed. Using Eclipse to > debug, I > get this in the request object: > > Content-Type: multipart/form-data; > boundary=---------------------------3994049112139135631933251514 > Content-Length: 61 > > And nothing else. If I change the file, the Content-Length doesn't > change. > > This is leading me to suspect that Jetty is consuming the > inputstream for > the multipart/form-data before it hands off to the servlet? But, I > could be > wrong here. > > Does anyone know what I'm doing wrong or what is going on here? > > Thanks > > -=david=- > -- > View this message in context: http://www.nabble.com/Jetty-6.1.3-%2B- > Commons-FileUploader-doesn%27t-appear-to-work-tf3846924.html#a10895314 > Sent from the Jetty Support mailing list archive at Nabble.com. > > > ---------------------------------------------------------------------- > --- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Jetty-support mailing list > Jet...@li... > https://lists.sourceforge.net/lists/listinfo/jetty-support |