java.io.FileNotFoundException after uploading a file

Hi developers, i got this java.io.FileNotFoundException after i attempt upload a file to my database? what could be the problem here? I'm using jsp, struts, db2, ibm rad6 for developing. Below is my action class.java file

****StrutsUploadAction.java****

import java.io.*;

import java.sql.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.upload.FormFile;

public class StrutsUploadAction extends Action

{

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

StrutsUploadForm myForm = (StrutsUploadForm)form;

// Process the FormFile

FormFile myFile = myForm.getTheFile();

String contentType = myFile.getContentType();

String fileName= myFile.getFileName();

int fileSize= myFile.getFileSize();

byte[] fileData= myFile.getFileData();

InputStream strem = myFile.getInputStream();

try

{// Prepare a Statement:

ConnectDB cn = new ConnectDB();

cn.init();

java.sql.Connection con = cn.connectdb();

PreparedStatement stmt = con.prepareStatement("INSERT INTO ADMINISTRATOR.TESTFILEUPLOAD (FILENAME) values(?)");

File file = new File(fileName);

FileInputStream inputStream = new FileInputStream(file);

stmt.setBinaryStream(1,inputStream,(int)(file.length()));

stmt.executeUpdate();

// Close resources

stmt.close();

}

catch(Exception ex)

{

System.out.println("Error occurred writing a BLOB: " + ex);

}

return mapping.findForward("success");

}

}

[1953 byte] By [NgSGa] at [2007-10-3 2:32:46]
# 1
>InputStream strem = myFile.getInputStream();Why cant you use this stream provided by the file upload functionality? Since the file hasnt yet been persisted, I dont see how you can create a FileInputStream by linking it to a non-existent file.ram.
Madathil_Prasada at 2007-7-14 19:31:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi Madathil_Prasad, as i'm not familiar with struts, and quite an amateur in java, i don't quite understand. How do i so call persist the file? Can you kindly how to persist it? Thanks alot... Btw, if i persist it, how do i link it to FileInputStream? I'm getting confused.. Hope you can guide me =)

NgSGa at 2007-7-14 19:31:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Erm, can some kind soul help me please? Really thanks alot...
NgSGa at 2007-7-14 19:31:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

I don't know anything about Struts and its file-uploading capabilities. But the file uploading code I have used doesn't put the uploaded data into a file -- as in a file on the hard drive of the server. So my guess is that Struts doesn't either, and hence using a File object doesn't make any sense.

But you don't need to assume that Struts does or doesn't use a file. You've got an InputStream, you know the length of the uploaded data. That's all you need for the database, isn't it? So I would just throw away a bunch of your code:// Process the FormFile

FormFile myFile = myForm.getTheFile();

int fileSize = myFile.getFileSize();

InputStream strem = myFile.getInputStream();

try

{ // Prepare a Statement:

ConnectDB cn = new ConnectDB();

cn.init();

java.sql.Connection con = cn.connectdb();

PreparedStatement stmt = con.prepareStatement("INSERT INTO ADMINISTRATOR.TESTFILEUPLOAD (FILENAME) values(?)");

stmt.setBinaryStream(1, strem, fileSize);

stmt.executeUpdate();

// Close resources

stmt.close();

DrClapa at 2007-7-14 19:31:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...