標籤:檔案下載
csv格式檔案實際為txt文字檔,可以右鍵選擇用notepad++查看
//HttpServletResponse response...try {StringBuilder sb = new StringBuilder();//sb為常值內容...byte[] b = sb.toString().getBytes(); response.setCharacterEncoding("utf-8"); String filename = "";if (operation_type != null && operation_type.equals("RELEASE")) {filename = "應用發布記錄";} else if (operation_type != null && operation_type.equals("OPS")) {filename = "應用營運記錄";}filename=URLEncoder.encode(filename,"utf-8"); //解決中文檔案名稱下載後亂碼的問題 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss"); response.setHeader("Content-Disposition","attachment; filename=" + filename+ sdf.format(new Date())+".csv"); //擷取響應報文輸出資料流對象 //javax.servlet.ServletOutputStreamServletOutputStream out =response.getOutputStream(); //輸出 out.write(b); out.flush(); out.close(); } catch (IOException e) {}
這裡以下載文字檔為樣本,實際上下載的檔案可以是任何格式的。只要將要下載的資料轉成byte數組即可下載。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/71/05/wKiom1XDTcvi36wWAAHiOS1a67Y217.jpg" title="a.PNG" alt="wKiom1XDTcvi36wWAAHiOS1a67Y217.jpg" />
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1682386
Java檔案下載