我已經創建了一個JSP代碼,我們可以在其中上傳.csv文件。 JSP代碼由讀取.csv文件的Java代碼支持,並將文件中的URL與數據庫進行比較,並在數據庫不存在的情況下將其添加到數據庫中。UNIX系統和Windows系統中讀取.CSV文件的差異
上面的場景在windows系統中執行時非常有效。
我將成功執行的Web應用程序文件夾上傳到unix系統。當我在UNIX系統中執行程序時,該工具不會將URL與數據庫進行比較並添加它。
我懷疑在UNIX系統中讀取.csv文件時應該會出現一些問題。
我使用fedora(linux)操作系統。請讓我知道在windows系統和unix系統之間讀取.csv文件是否有任何差異。
.csv文件我使用有如下內容,
http://www.topix.com,sdfasdf
http://rss.news.yahoo.com/rss/topstories,Apple
http://www.apple.com/354,sdfasdf
http://www.topix.com/rss/city/emporia-ks,sdfasdf
http://www.topix.com/rss/,sdfasdf
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml,sdfasdf
http://www.topix.com/rss/city/emp,sdfasdf
http://www.topix.com/rss/city/sandy-ut,dfgsdfg
http://www.apple.com,Yahoo
更新JEFF
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.isFormField()) {
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
} else {
File file = new File(destinationDir,item.getName());
item.write(file);
//String temp=item.getName();
String fileToBeRead = "C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/Readcsv/files/"+item.getName();
String urlcnt="";
String srccnt="";
String contentType="";
Connection con=null;
Statement stmt=null;
final String rssvar="Rss";
final String other="Other";
int i=0;
int j=0;
try {
BufferedReader br = new BufferedReader(new FileReader(fileToBeRead));
String strLine = "";
StringTokenizer st = null;
while((strLine = br.readLine()) != null)
{
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens()){
urlcnt=st.nextToken();
srccnt=st.nextToken();
}
if(con==null){
SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
con=SQLConnection.getNewConnection();
stmt=con.createStatement();
}
try{
ResultSet rs;
boolean hasRows=false;
rs=stmt.executeQuery("select url from urls_linkins where url='"+urlcnt+"'");
while(rs.next()){
hasRows=true;
i++;
}
if(!hasRows){
j++;
URL url = new URL(urlcnt);
URLConnection url1=url.openConnection();
contentType=url1.getContentType();
PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_linkins(url, source_name, is_active, is_periodic, Link_Type, New_Entry) VALUES(?, ?, ?, ?, ?, ?)");
if(contentType.contains("rss") || contentType.contains("xml"))
{
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, rssvar);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.executeUpdate();
insertUrlStatement.close();
}
else{
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, other);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.executeUpdate();
insertUrlStatement.close();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
out.println("<h2>"+j+" url has been added and "+i+" url already exists in the DB</h2>");
out.println("<a href=Addurl.jsp>Check URL</a>");
out.println("<a href=Addurl1.jsp>Add Single URL</a>");
out.println("<a href=uploadcsv.jsp>Add Multiple URL</a>");
}
}
out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}
這是.csv文件的我的閱讀代碼。
任何異常?其他消息?究竟是哪裏失敗 - 例如讀取文件,連接到數據庫,比較,更新數據庫? – Jeff 2010-11-02 20:23:46
沒有例外..沒有消息..連接到數據庫和更新數據庫工作正常。但問題是閱讀文件 – LGAP 2010-11-02 20:25:00
你可以添加文件閱讀代碼到你的問題?幾個想法:權限或Windows(CRLF)和Unix(LF) – Jeff 2010-11-02 20:28:15