file open in java

I have created a small application to upload any file and save it in a temporary directory.

Then display all the file names which are in that directory.here's my code ....

MultipartRequest mr = null;

try {

mr = new MultipartRequest(req, ".");

BufferedReader br = new BufferedReader(new FileReader(fileName = (String)mr.getFilesystemName("importFile")));

} catch(Exception e) { System.out.println("Error uploading from file '"+fileName+"': "+e); e.printStackTrace();

} finally { new File(fileName).delete(); }

String dir = "C:\\asta-timesheet\\temp\\";

Vector fileVector = new Vector();

boolean result = false;

DataInputStream dis;

FileInputStream fin;

try {

String fileContent = "";

Enumeration files = mr.getFileNames();

String name = (String)files.nextElement();

String filename = mr.getFilesystemName(name);

String type = mr.getContentType(name);

File f = mr.getFile(name);

FileReader fs = new FileReader(f);

BufferedReader in = new BufferedReader(fs);

String s, s2 = new String();

while((s = in.readLine())!= null) {

s2 += s + "\n";

}

fileContent = s2;

in.close();

File file = new File(dir, fileName);

// Create file if it does not exist

boolean success = file.createNewFile();

if (success) {

InputStream inn = new FileInputStream(f);

OutputStream out = new FileOutputStream(file);

// Transfer bytes from in to out

byte[] buf = new byte[1024];

int len;

while ((len = inn.read(buf)) > 0) {

out.write(buf, 0, len);

}

inn.close();

out.close();

} else {

// File already exists

req.setAttribute("fileStatus", "exists");

}

} catch(Exception e) { System.out.println("Error uploading from file '"+fileName+"': "+e); e.printStackTrace();}

File dirr = new File(dir);

String[] children = dirr.list();

if (children == null) {

} else {

for (int i=0; i<children.length; i++) {

String filename = children;

}

}

// The list of files can also be retrieved as File objects

File[] files = dirr.listFiles();

for (int i = 0; i >< files.length; i++) {

File f = files;

}

req.setAttribute("fileVector", files);

return returnPage;

*************************************

I need help with opening up the listed files.i.e if its a word doc.i need it to be open on ms word.I have file types like excel,jpg,txt....Can someone pls explain or provide code for this.

thx in advance.

[2679 byte] By [kuhasaa] at [2007-11-26 18:38:15]
# 1

All you could use to detect the correct type would be the extension which might or might not be accurate. We'll assume for a moment that the extensions will be accurate. What you need to look into running native code. Follow the bouncing link.

http://www.google.com/search?hl=en&q=Java+%2B+native+code&btnG=Google+Search

Hope this helps,

PS.

One more thing, please use code tags when you're posting code, it makes it much easier to read.

Message was edited by:

puckstopper31

puckstopper31a at 2007-7-9 6:12:19 > top of Java-index,Java Essentials,Java Programming...