我做了很多关于使用java代码导入和导出shapefile到postgis的研究。我尝试使用shp2pgsql和processBuilder,但它对我不起作用。 当我运行代码时,它没有给我任何错误。
他是我一直在使用的代码:
String userName="postgres";
String path="A:\\DATA\\Provinces.shp";
String table="test";
String txtDB="test";
String cmd = " shp2pgsql –I –D -W \\LATIN1\\ "+path+" "+table+" | psql - p 5432 "+txtDB+" "+ userName;
String[] command = { "cmd","/c","C:\\Program Files\\PostgreSQL\\9.2\\bin\\shp2pgsql.exe",cmd };
ProcessBuilder probuilder = new ProcessBuilder(command);
final Process process = probuilder.start();
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
ProcessBuilder pb= new ProcessBuilder();
pb.command("shp2pgsql ","<filepath>|psql -U postgres");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
String temp="";
while ((line = reader.readLine()) != null)
{
temp=temp.concat(line);
System.out.println("shp2pgsql: " + line);
}
if(!temp.isEmpty())
{
PreparedStatement preStmt=con.prepareStatement(temp);
preStmt.executeUpdate();
return true;
}