我正在使用servlet和基本html來獲取xml文件並通過tomcat apache將其上傳到本地服務器。但是我需要的是在實際提交表單之前(我在輸入文件類型標記1中選擇的xml文件)彈出窗口應該顯示xml重要項目的摘要。這將使用戶更加自信,他/她正在上傳哪個文件。願有人引導我朝一個方向發展嗎?在上傳xml文件之前,html頁面應該解析並顯示xml文件的摘要
我使用apache commons-file文件上傳API,jquery-ajax用於彈出窗口和其他效果,html用於基本客戶端和servlet,用於處理服務器端。
`PrintWriter out = response.getWriter();
response.setContentType("text/plain");
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setSizeThreshold(1*1024*1024);
fileItemFactory.setRepository(tmpDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
/*
* Parse the request
*/
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()) {
FileItem item = (FileItem) itr.next();
out.println(item.isInMemory());
/*
* Handle Form Fields.
*/
if(item.isFormField()) {
out.println("Field Name = "+item.getFieldName()+", Field Value = "+item.getString());
}
if(item!=null){
//Handle Uploaded files.
out.println("Field Name = "+item.getFieldName()+
", File Name = "+item.getName()+
", Content type = "+item.getContentType()+
", File Size = "+item.getSize());
/*
* Write file to the ultimate location.
*/
out.println("Hello ");
File file = new File(destinationDir,item.getName());
item.write(file);
}
out.close();
}
}catch(FileUploadException ex) {
log("Error encountered while parsing the request",ex);
} catch(Exception ex) {
log("Error encountered while uploading file",ex);
}`
這是HTML輸出我得到,爲什麼不顯示文件名,內容類型和尺寸。文件名是「test.txt」,裏面有「Hello」,正在上傳到我的目標目錄。
真正
字段名稱= confname,字段值=
字段名稱= confname,文件名= NULL,內容類型= NULL,文件大小= 0
你好
@ g19fanatic:這是代碼,我在哪裏使用臨時位置'DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); fileItemFactory.setSizeThreshold(1 * 1024 * 1024); \t \t fileItemFactory.setRepository(tmpDir);'那麼現在我怎麼回發內容解析,請你詳細說明一下。謝謝你的時間。 – abi1964 2011-04-20 10:57:35
使用jquery-ajax GET將文件臨時上傳到tmpDir後,使用您的servlet(?)打開該文件,讀取內容並將它們發送回客戶端。然後,jquery可以直接解析這個XML數據並按照你的意思顯示它。然後,您可以選擇'接受'或'拒絕'按鈕,只需發佈一篇文章即可刪除臨時文件,也可以將其移至您感興趣的實際路徑中。爲避免有人在上傳臨時文件後才離開文件,有東西,將定期掃描和清空舊文件的臨時目錄 – g19fanatic 2011-04-20 15:12:11
@ g19fanatic:您可以告訴我一個示例或任何鏈接_using a jquery-ajax GET,使用您的servlet(?)打開該文件,閱讀內容並將它們發回給客戶_對不起,我最近開始遇到麻煩並面臨問題。 – abi1964 2011-04-20 15:48:47