File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Help needed with MultipartRequest from O'Reilly. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help needed with MultipartRequest from O Watch "Help needed with MultipartRequest from O New topic

Help needed with MultipartRequest from O'Reilly.

Jacek Nabywaniec

Joined: Aug 16, 2003
Posts: 4
Hi, I searched this forum and other ones but did not come across a solution to my problem and reading source code for the class did not help me either... I hope someone will help me figure this one out.
I have an application which uses the MultipartRequest class to upload files. To avoid name conflits I simply use the DefaultFileRenamePolicy object which appends 1, 2, 3 to file names... That works fine and I want to keep it this way. But my users will submit files with names containing non-ascii characters which are a problem for me. They also submit files with names as long as 100 chars.
I would like to intercept the upload process and rename the file myself somewhere along this logic:
1. Get file name submitted by user
2. Replace all non ascii chars to "_"
3. Cut file name to 25 chars
4. Force upload process to use the new file name as destination name.
5. Let DefaultFileRenamePolicy rename the file if one by that name exists.
I need help understanding where this interception should take place. I am not asking how to replace a character in string or how to cut the file to 25 chars.
Can anyone help?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi jacek
u can probably put that interception before u use MultipartRequest object and pass the request to it.
now, here u'll have to modify the request object so to say (because MultipartRequest object will expect HttpRequest object) which i'm having little knowledge about how to do but i guess u can play w/ RequestWrapper or something...
i know MultipartRequest object internally use RequestWrapper but i've never used it so i fear i cant guide further..
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

From the MultipartRequest API:
To avoid collisions and have fine control over file placement, there's a constructor variety that takes a pluggable FileRenamePolicy implementation. A particular policy can choose to rename or change the location of the file before it's written

So that sounds like you can implement FileRenamePolicy with your own class, and this is where I'd implement your file renaming logic.

Another option, is to extend the DefaultFileRenamePolicy. You could do step #2 and #3 as described in your logic, and then submit *that* name to the DefaultFileRenamePolicy (accomplishing #4) class (through a call to super()). This class will do #5 for you.

Re-use is cool
[ August 17, 2003: Message edited by: Mike Curwen ]
I agree. Here's the link:
subject: Help needed with MultipartRequest from O'Reilly.
It's not a secret anymore!