Originally posted by Sophia Choi: The file reads unexpected codes and runs without stop.
Well, if you expect our help, explaining what "unexpected codes" means would be a good start. If you are getting an exception, printing out the stack trace and using it to determine the offending line in your code would help. My wild guess is that you are expecting to read in a certain value from the CSV that will indicate end-of-file rather than checking for the end-of-file condition from whatever input stream you are using. CSV's are just text files. You can open it in your editor and seeif Excel exported what you think it did. Use System.out.println to print lines as your program reads them in. Sometimes you aren't getting the data that you think you are. Last but not least, you can step through the program using a debugger in your IDE or a seperate product like JSwat
Steps: 1. upload file, expected xxx.csv. In jsp, I can only check the extension. If not csv, prompt out an error message. 2. read upload file content and compare to my db 3. result comes out. The problem is someone changes the xx.xls to xx.cvs. My program does it work actually. But infinitively because they are machine codes. try FileReader fr= new FileReader(path + filename); BufferedReader br = new BufferedReader(fr); String Line = br.readLine(); <-- problem ..... catch exception..
You might try to find a way to verify that the file is valid CSV before you read any further. Maybe open the file as a byte stream and see if the first ten or twenty bytes contain printable letters and numbers. Or examine XLS files in debug and see if they start with a common "signature" pattern. Either way, if the file looks good, close it and open again with your buffered reader. That's definitely an extra step with some performance cost, but it sounds like it will be worth while to protect yourself from unreadable data.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi