hi!
i was working on a program to upload files in struts. i was trying to store the uploaded files in a directory. the files were being created there but they were of 0 kb. can somebody tell me why was it happening.
here is the source code
****************************************************
/**
*
*/
package file.load.dao;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.struts.upload.FormFile;
import com.miracle.common.components.connectivity.ConnectivityException;
import com.miracle.common.components.connectivity.DBConnectionManager;
import file.load.web.TheFileForm;
/**
* @author Als
*
*/
public class FileDataManager {
private FileDataManager(){
}
private static FileDataManager cInstance = new FileDataManager();
public static FileDataManager getInstance(){
if(cInstance == null){
cInstance = new FileDataManager();
}
return cInstance;
}
public void addNewFile(TheFileForm aForm){
FormFile formFile = aForm.getContentFile();
try {
InputStream istream = formFile.getInputStream();
String fileName = formFile.getFileName();
String fileType = formFile.getContentType();
String dirName = "filesUploaded";
File dir = new File(dirName);
if(!dir.exists()){
dir.mkdir();
}
else{
File newFile = new File(dir, fileName);
newFile.createNewFile();
FileOutputStream fout = new FileOutputStream(fileName);
//BufferedOutputStream bos = new BufferedOutputStream(fout);
String filePath = newFile.getAbsolutePath();
int c = 0;
while((c=istream.read())!= -1){
fout.write(c);
}
fout.flush();
fout.close();
String strSQL = "INSERT INTO FILE ("+"FILE_NAME,FILE_PATH,FILE_TYPE"+") VALUES(?,?,?)";
PreparedStatement pstmt = DBConnectionManager.getInstance().getConnection().prepareStatement(strSQL);
int i = 1;
pstmt.setString(i++,fileName);
pstmt.setString(i++,filePath);
pstmt.setString(i++,fileType);
pstmt.executeUpdate();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ConnectivityException e) {
e.printStackTrace();
}
}
}
-
why 0 kb files are being created (0 messages)
- Posted by: umar ali
- Posted on: November 06 2005 13:09 EST