UNIX系统和Windows系统中读取.CSV文件的差异
我已经创建了一个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文件的我的阅读代码。
是的,当您从Windows机器传输到Unix机器时,即使它是文本文件,在读取.csv文件时也会有差异。有一些隐藏的空间字符可能会在unix机器上以不同的方式表示。
我怀疑它没有比较URL的原因是因为空格字符可能是不同的ASCII值,所以它认为它们是不同的,并将URL添加到数据库中。
一个建议是使用dos2unix命令。
http://kb.iu.edu/data/acux.html
希望它有帮助。
任何异常?其他消息?究竟是哪里失败 - 例如读取文件,连接到数据库,比较,更新数据库? – 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