Friends, I am trying to write a upload servlet with com.oreilly.servlet.multipart. The problem I am having is specifiying the name of file on the server's side. When I looked at the documentation for FilePart.writeTo() ... com.oreilly.servlet.multipart Class FilePart java.lang.Object | +--com.oreilly.servlet.multipart.Part | +--com.oreilly.servlet.multipart.FilePart
writeTo public long writeTo(java.io.File fileOrDirectory) throws java.io.IOException Write this file part to a file or directory. If the user supplied a file, we write it to that file, and if they supplied a directory, we write it to that directory with the filename that accompanied it. If this part doesn't contain a file this method does nothing. Returns: number of bytes written Throws: java.io.IOException - if an input or output exception has occurred.
... I figured all I had to do was supply a file to writeTo(), so that writeTo() would write to that file.
So I modified the example servlet code (DemoParserUploadServlet.java) to do this ...
... and I get the error ... javax.servlet.ServletException: Supplied uploadDir ./TEST001/FILE_1_20040220085207.dat is invalid I took a look at DemoRequestUploadServlet.java but that only shows me would to write files to the /tmp directory not how to change the file's name from what the user supplied in the form. Please help! [ February 20, 2004: Message edited by: Sheldon Plankton ]
Joined: Feb 11, 2004
I just resorted to doing something like this ... public void init ( ... dir = new File ( dirName ); } . . . public void doPost ( ... . . . String oldFileName = filePart.getFileName(); . . . String newFileName = dirName + "/" + xyz_UpLoadUser + "/" + name + "_" + getTS() + ".dat"; size = filePart.writeTo(dir); File oldFile = new File( oldFileName ); File newFile = new File( newFileName ); boolean rc = oldFile.renameTo( newFileName ); . . . }
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com