0
我在這裏有一個我需要解決的情況。我必須上傳xml文件的特定元素才能將其上傳到服務器,並設法做到這一點,並且我創建了一個演示方法來檢查文件是否正在上傳到服務器。java xml上傳問題!
我的XML文件結構,
<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>
我的servlet代碼是:
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 = "";
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("[email protected]",
"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();
}
但問題是,該文件得到正確上傳,但內容是不完整的,有什麼我的意思是說,<engine>
在那裏,但</engine>
不存在。但我在我的本地系統中重新檢查了它,它創建了一個臨時文件,並且它顯示了完整的xml文件。爲什麼整個內容沒有被上傳到服務器!? 有什麼想法? 這是我所看到的服務器,
<engine>
<eid>1</eid>
<sometextelement
我沒有得到你Vivien!沒有方法fis.flush()。 – Maverick 2011-05-17 16:53:50
該怎麼做?!這個你能幫我嗎! – Maverick 2011-05-17 16:57:16
在客戶端,當您上傳文件時。你在用什麼?可能是某種OutputStream,帶''flush()''方法。 – 2011-05-17 16:57:31