java 上傳txt文檔成功之后,打開上傳成功之后的txt文檔是空的??
問題描述
上傳的代碼:
String filepath = filetxt.substring(12);//創建文件輸出流 FileOutputStream fos=null; try { //設置文件保存路徑 String savePath=request.getSession().getServletContext().getRealPath('/')+filepath; System.out.println(savePath); logger.debug('文件保存完整路徑:'+savePath); //創建文件 File saveFile=new File(savePath); if(!saveFile.exists()){//判斷文件是否存在 saveFile.createNewFile(); } fos=new FileOutputStream(saveFile); logger.debug('fieldName:'+filepath+',with filename='+filepath); } catch (Exception e) { logger.error('Exception:',e); e.printStackTrace(); }finally{ //關閉輸出流 fos.close(); } out.flush();out.close();
保存路徑:D:apache-tomcat-7.0.42webappssimfencewyp.txt但是打開之后,wyp.txt是空的,這是為什么????
問題解答
回答1:你沒有往fileoutputstream寫數據。
回答2:用fos.append()寫入內容,寫什么,你的wyp.txt才會有東西,最后flush一下
