Java Reference
In-Depth Information
tds.setUser( dbUserName );
tds.setPassword( dbPassword );
DataSource ds = tds;
Connection con = ds.getConnection(dbUserName,dbPassword);
PreparedStatement pstmt = con.prepareStatement(cmd);
pstmt.setInt(1, memberID);
pstmt.setString(2, title);
pstmt.setCharacterStream(3, new FileReader(doc),
(int)doc.length());
pstmt.executeUpdate();
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
Uploading Images and Documents from a Browser
A common requirement in Web applications is to upload images and documents from a client machine
over the Internet. Uploading files using an HTML form is part of the HTML standard and is supported by
all major browsers. However, in spite of being a standard capability, HTML file upload isn't very well
documented elsewhere, so it is worth reviewing how to create a servlet to handle uploads.
HTML file uploads use the multipart message format defined by the Multipurpose Internet Mail
Extensions (MIME) standard, sending each field of the form as a separate MIME part. The main points
to notice about creating the HTML upload form are as follows:
 
The "method" attribute of the FORM is set to "post".
 
The attribute "enctype = multipart/form-data" is added to the FORM element.
 
An INPUT element with the type "file" is used to specifiy the file to upload.
When the form is set up like this, the browser creates a file select control that lets you select the file to
upload. Listing 14-3 shows an example of a simple HTML upload form.
Listing 14-3: HTML file-upload form
<HTML>
<BODY>
<FORM action="servlet/BlobUploadServlet"
Search WWH ::




Custom Search