我遇到了一个需要解决的问题。我必须上传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("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();
}
但问题是,该文件已正确上传,但内容不完整,我的意思是,<engine>
存在,但</engine>
不存在。但我在我的本地系统中重新检查它,它正在创建一个临时文件,它显示完整的xml文件。为什么整个内容没有上传到服务器!?
有任何想法吗?
这就是我在服务器中看到的,
<engine>
<eid>1</eid>
<sometextelement
答案 0 :(得分:2)
如果您自己编写了客户端代码,则看起来客户端流未正确刷新。
尝试刷新或关闭(close()
也刷新流)客户端流。