Java檔案下載

來源:互聯網
上載者:User

標籤:檔案下載

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檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.