java xml uploading problem!
I am having a situation here which I need to resolve. I have to upload particular elements of an xml file to upload it to a server, managed to do that, and I created a demo method to check if the file is being uploaded to the server or not.
My xml file has the structure,
<config>
<engine>
<eid>1</eid>
<sometextelement>text</sometextelement>
</engine>
<engine>
<eid>2</eid>
<sometextelement>text</sometextelement>
</engine>
<engine>
<eid>3</eid>
<sometextelement>text</sometextelement>
</engine>
</config>
My servlet code is:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/json");
//response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
JSONObject obj = new JSONObject();
String value = request.getParameter("value");
String message = "";
开发者_如何学C String update = "";
Element element = null;
Element root = null;
XMLOutputter xmlOutputter = new XMLOutputter();
try{
doc = saxBuilder.build("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml");
}catch(Exception e){
e.printStackTrace();
}
root = doc.getRootElement();
List list = doc.getRootElement().getChildren();
Iterator itr = list.iterator();
int i = 0;
while(itr.hasNext()){
element = (Element)itr.next();
System.out.println("Entered 1");
File f = File.createTempFile("engine_",".xml");
System.out.println(f);
xmlOutputter.output(element, new FileWriter(f));
i += 1;
putFile(f,i);
}
// xmlOutputter.output(doc, new FileWriter("E:/workbench j2ee/cPEP_UI/WebContent/engine.xml"));
// System.out.println("hello from system");
// out.println("hello");
}
public void putFile(File f, int i) throws SocketException, IOException{
FTPClient client = new FTPClient();
FileInputStream fis = null;
client.connect("ftp.someserver.co.uk",21);
boolean login = client.login("webmaster@someserver.co.uk",
"mypassword");
fis = new FileInputStream(f);
if(client.storeFile("engine_"+i+".xml", fis)){
System.out.println("yes");
}else{
System.out.println("no");
}
client.logout();
fis.close();
}
But the problem is, the file is getting uploaded properly, but the content is not complete, what I mean to say is, the <engine>
is there but </engine>
is not there. But I rechecked it in my local system, which is creating a temporary file, and it shows the complete xml file. Why the whole content is not being uploaded to the server!?
any ideas?
This is what I am seeing in the server,
<engine>
<eid>1</eid>
<sometextelement
If you wrote the client code yourself, this looks like the client-side stream isn't flushed correctly.
Try flushing or closing (close()
flushes a stream as well) the client stream.
精彩评论