This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes com.oreilly.servlet.multipart writeTo() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "com.oreilly.servlet.multipart writeTo() question" Watch "com.oreilly.servlet.multipart writeTo() question" New topic
Author

com.oreilly.servlet.multipart writeTo() question

Sheldon Plankton
Ranch Hand

Joined: Feb 11, 2004
Posts: 33
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 ]
Sheldon Plankton
Ranch Hand

Joined: Feb 11, 2004
Posts: 33
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: com.oreilly.servlet.multipart writeTo() question