Here one thing we need to observee carefully , when we want to upload a specific file in web application.
we should know about compatibility of browsers.
1 ) IE 7 and IE 8 :
Note : if we upload a file in web using below html tag
syn : <input type="file" name="fileupload" accept=""/>
for example : the file location is at D:/Folder1/Folder2/filename.csv
so when u upload a file from this location , IE browser will take the path as : D:\Folder1\Folder2\filename.csv
observe carefully , if we test with the file upload functionality in below browsers
2) firefox , opera , chrome , Netscape
here , these browsers will take the path as : filename.csv
these above browsers have not take complete path when we upload a file from specific directory.
conclusion :
IE : will take full path eg: D:\folder1\folder2\filename.csv
firefox,chrome..etc : the take only specific file name eg : filename.csv
so, if u are completely following IE compatibility web browser , i suggest everyone please
use java replace() method , for replacing the backword (\) slash with forward slash (/). otherwise java will not accept backword (\) charcter for reading the file .....
eg: String filename="D:\filename.csv ": we cant read this file in java.
correct way : filename=filename.replace("\\","/");
then the path will convert in to this format : D:/filename.csv , so we can easily read the file ..
for other browsers except IE,
we can confiure the path in static way .
for example : String dir="D:/";
path : "filename.csv";
String filename=dir+path;
Hope u may get some idea, how to make workaround solution for this..
it would be very grateful for your feedback, if u find any errors........
u can test sample example from below code snippet :
Step 1: please paste this code in html or jsp ..
<form action="servletClassName" method=post/ enctype="multipart/form-data>
<form action="servletClassName" method=post/ enctype="multipart/form-data>
<input type="hidden" name="_paramArgument" vaule="fileupload"/>
<input type="submit" value="upload" />
<input type="file" name="_PARAM_FILEUPLOAD" id="sno" size="20" accept="text/csv" />
</form>
Step 2:
Step 2:
Business logic: Here u can use servlet or jsp ?copy the below code either in servlet or jsp.
public class UploadServlet extends HttpServlet
{
String param=request.gerParameter("_paramArgument");
dospost(){ //dopost()method of servlet
try{
if(param.equals("fileupload"))
{
String data=request.getParameter("_PARAM_FILEUPLOAD");
data = data.replace("\\", "/");
if (data.endsWith(".csv") && data.contains("/")) {
File f = new File(data);
boolean path = f.exists(); // if path exits then true otherwise false
if(path)
{
//your Database logic to insert the file.csv in to database.....upload success
uploadCSVFile(data); // this method will insert the csv data in to database....
}
else {invalid upload}
}
}
catch(Exception e)
{}
}}
step 3:
DAO Logic :
public int uploadCSVFile(String path) {
int status = 0;
try {
String query = "LOAD DATA INFILE '" + path + "' IGNORE INTO TABLE tbl_voicetocrbt_content FIELDS TERMINATED BY
',' LINES TERMINATED BY '\n'";
PreparedStatement pst = getJDBCConnection().prepareStatement(query);
status= pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
:
Cheers and Fun
Dasari...
No comments:
Post a Comment