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

More problems with file uploads

Tony Kemp

Joined: Oct 15, 2002
Posts: 14
Hi all,
I'm haveing trouble uploading files using files with the com.oreilly.servlet package. It fails to recognize any files that I try to upload. The servlet runs without any errors, it just doesn't do anything with the files. The file handling section is identical to the provided example, which does work. My form is also like the example, even using the same field names.
Any ideas? Anybody had the same trouble and fixed it?
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
This is the code for uplodaing and retreving gis and jpgs ..
if u want text files just replce
res.setContentType()/whatever u want either text/plain or text/application..

import java.sql.*;
/* If you are compiling and executing file in c:\
* the file rao.gif should exist in c:\ (same directory)...
public class ImageJDBC {
public static void main(String args []) throws SQLException, ClassNotFoundException,Exception {
try {

Class.forName ("oracle.jdbc.driver.OracleDriver");
String url = "jdbc racle:thin:@rao:1521:amica";
Connection conn = DriverManager.getConnection(url,"mail","mail");
// method to insert Image into database
ImageJDBC obj = new ImageJDBC();
// method to retreive Image from database
}catch(Exception e){
public void insertImage(Connection conn){

try {

Statement stmt = conn.createStatement ();
// execute CREATE for webimages table
boolean flag = stmt.execute("CREATE TABLE webimages (img_ident VARCHAR2 (64), img_fmt CHAR (5), img_data LONG RAW)");
// Insert data into the img_data column
File file = new File ("rao.gif");
InputStream istr = new FileInputStream ("rao.gif");
// prepare the INSERT into webimages using parameters
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO webimages (img_ident, img_fmt, img_data) VALUES (?, ?, ?)");
pstmt.setString (1, "Rao Logo");
pstmt.setString (2, "GIF");
pstmt.setBinaryStream (3, istr, (int)file.length ());
pstmt.execute ();
}catch(Exception e){
public void getImage(Connection conn){
try {
Statement stmt = conn.createStatement ();
// retrieve image from row when the ID is 'Rao Logo'
ResultSet rs = stmt.executeQuery ("SELECT img_data FROM webimages WHERE img_ident='Rao Logo'");
// process the ResultSet data
if ( ()) {
// get binary stream data from result set
InputStream web_image = rs.getBinaryStream(1);
// open operating system file for storing the image
FileOutputStream ostr = new FileOutputStream("backlogo.gif");
// fetch-write loop, fetch image data from rs, write to file
int i;
while ((i = != -1) {
ostr.write (i);
ostr.close ();
}catch(Exception e) {
Tony Kemp

Joined: Oct 15, 2002
Posts: 14
This is not the kind of uploading I wasnt to do, I want to upload files across a network using a web browser. I have set everything up, but com.oreilly.servlet.MultiPartParser is not fining the files I try to upload - it just thinks that the form is empty. Here's my processing code:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
out = response.getWriter();
dirName = "/tmp/vlis/";
System.err.println("Setting up...");
try {
multi = new MultipartRequest(request, dirName); // 1 Mb upload limit
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
System.out.println("Processing file...");
String name = (String)files.nextElement();
String filename = multi.getFilesystemName(name);
String type = multi.getContentType(name);
File f = multi.getFile(name);
FileReader fs = new FileReader(f);
BufferedReader in = new BufferedReader(fs);
String s, filecontent = new String();
while((s = in.readLine())!= null) {
filecontent += s + "\n";
//session.setAttribute("fileContent", fileContent);
out.println("name: " + name);
out.println("filename: " + filename);
out.println("type: " + type);
if (f != null) {
out.println("f.toString(): " + f.toString());
out.println("f.getName(): " + f.getName());
out.println("f.exists(): " + f.exists());
out.println("f.length(): " + f.length());
out.println("fileContent: " + filecontent);
} catch (IOException ioe) {
System.out.println("Error processing file: " + ioe.getMessage());
Tony Kemp

Joined: Oct 15, 2002
Posts: 14
Sorry about that, let me try again with code tages:
Bill Wang
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
As far as I know, multipartRequest is for the Http form posting using the enctype="multipart/form-data". It looks like you try to load and print the local files on the servlet server. If that's the case, regular file IO package can hanlde it. Otherwise, you need to have a form like the following to submit your request:
<form method="post" enctype="multipart/form-data"
<input type=file name="">
Hope this helps
Originally posted by Tony Kemp:
Sorry about that, let me try again with code tages:

SCJP<br />SCWCD<br />SCEA Part I
Tony Kemp

Joined: Oct 15, 2002
Posts: 14
That's what I'm doing. I have an HTML page with a multipart/formdata form on it, with a file element (called "file1") as the only input, and a submit button, with the action pointing to the servlet containing the code included above, using the POST method. And it just doesn't work
Matthew X. Brown
Ranch Hand

Joined: Nov 08, 2000
Posts: 165
Tony- I know what you're going through- but I did get mine working- a question that might I think you need to ask is whether its deployed properly- can you post your deployment descriptor(web.xml entries), as well as the HTMl Post tag where you are posting to? I guess the first questions is- does it even get to the servlet(ie. is it deployed properly). Forgive me if I didn't gleam this from the other posts.
Tony Kemp

Joined: Oct 15, 2002
Posts: 14
Yes, it does get to the servlet OK, it prints out "Files:" as per the first print statements, but it doesn't access the file that I upload, it just sails on through as though there were no files specified.
Here is the form:

And the relevant section of web.xml:

Does this give you any ideas?
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
multi.getFileNames() method returns nothing: it can not detect the name of the file as provided by the form:
You forgot to set the NAME of the file parameter in your form, try <input type=FILE NAME=file1> instead of <input type=FILE value=file1>.
Sean Sullivan
Ranch Hand

Joined: Sep 09, 2001
Posts: 427
Your html needs to look like this:
<FORM ACTION="/servlet/MyServlet"
What is your name? <INPUT TYPE=TEXT NAME=submitter><br>
What file are you sending?
<INPUT TYPE=FILE NAME=secretDocument><br>
<input type=hidden name="foo" value="bar">
<input type="submit" value=Submit><br>
Tony Kemp

Joined: Oct 15, 2002
Posts: 14
Thank you all. Damn, I can't beleive I did that. I hate it when I overlook silly little things like that .
Thank you everyone for your help.
I agree. Here's the link:
subject: More problems with file uploads
It's not a secret anymore!