> 1-is it possible to upload a file from a jsp file which has some more other 
> fields too ?
> I mean for example a registration form which must have a file item to be 
> uploaded ?

Absolutely - it's just a standard HTTP request form after all.

> 2-which method should be used to send the jsp fields to action ?
> Post or GET ?

Definitely POST.  Get has a maximum length according to the specification, I 
think it's 
256 or 2048 bytes or something.  Your file is likely to be longer than this.

> 3-is it manedatory to use Struts html tags to have file upload ?
> I used a plain jsp to gather myregistration dara and a
> <input type="file" name="myFile" >
> to upload the file.

Hmmm, not sure.  I just used the Struts stuff so that everything is consistent. 
 
Remember you want the Struts servlet to manage the fields so it is probably 
better to let 
Struts know about it from the beginning of your form creation through to the 
end.

Remember you need the enctype= parameter in your HTML:FORM tag.

> 4-how i should retrive the file data in action ?

Here's a snippet from my code, 'df' is the form returned and I was uploading an 
image 
file:
          // Get information about the uploaded file
          FormFile uploaded = df.getImage();
          df.setSize(uploaded.getFileSize());
          df.setFilename(uploaded.getFileName());
          File remote = new File(df.getFilename());
          
          // Insert a record into the database, the IID field is 
AUTO_INCREMENTing
          pstmt = conn.prepareStatement(sql1.toString());
          pstmt.setString(1, df.getTitle()); 
          pstmt.setString(2, df.getFilename()); 
          pstmt.setInt(3, df.getSize()); 
          rc = (pstmt.executeUpdate() == 1);
          
          // Retrieve the IID (unique number) so we can name and store the file 
locally
          if (rc) {
              // Close and open a new statement
              pstmt.close();
              pstmt = conn.prepareStatement("SELECT LAST_INSERT_ID()");
              rs = pstmt.executeQuery();
              if (rs.next()) {
                  
                  // Give this image a local name and write the data to this 
file
                  // Here I'm creating "/fixedPathToImages/27_originalName.jpg" 
                  // "27" is the unique number from the database so users can 
                  // repeatedly load the same filename and it won't overwrite
                  InputStream is = uploaded.getInputStream();
                  df.setImageid(rs.getInt(1));
                  df.setLocal(df.getImageid() + IMAGESEP + remote.getName());
                  File local = new File(Manager.getImageDirectory() + "/" + 
df.getLocal());
                  FileOutputStream fos = new FileOutputStream(local);
                  byte[] buffer = new byte[32768];
                  int n = 0;
                  while ((n = is.read(buffer)) != -1) 
                      fos.write(buffer, 0, n);
                  is.close();
                  fos.close();
                  
                  // Now store the LOCAL  name in the record
                  pstmt.close();
                  pstmt = conn.prepareStatement("UPDATE Doc SET " + LOCAL + " = 
? 
WHERE " + IMAGEID + " = ? ");
                  pstmt.setString(1, df.getLocal());
                  pstmt.setInt(2, df.getImageid());
                  rc = (pstmt.executeUpdate() == 1);
              }
              
          }


> 5-how i can save the file into a specefic folder in my web application -as 
> you know host provider restrict the access to getRealPath() method-

This is an interesting question.  You probably don't want to store your images 
in your 
web directory, is the answer I came up with.  Have you ever used the "undeploy" 
option 
in the Tomcat Manager/Administrator? When you use "undeploy" it removes the 
entire 
directory where your WAR file was extracted.  If you stored any data there then 
it's all 
history.

I tried storing the files as Blobs in my database but you don't want to go here 
either, it's 
a performance nightmare (8 seconds to download a 140k file).

You need a fixed directory somewhere on your server to write these files.  My 
preference is to keep them near the database files so that they get backed up 
together. 
However, I think there is some performance issue with MySQL (or SQL in general) 
if it 
can't handle these files being stored as Blobs.  What's the point in defining a 
Blob type 
that handles 32mb if it doesn't perform in practice.

Kind regards
mc


FOCUS Computing
Mob: 0415 24 26 24
[EMAIL PROTECTED]
http://www.focus-computing.com.au



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.10.20/95 - Release Date: 9/09/2005


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to