JavaWeb response物件常用操作

2021-09-25 04:53:15 字數 1407 閱讀 9663

方式一

response.setcontenttype(contenttype);

方式二

response.setheader("content-type", contenttype);

response.setcontenttype(mime)的作用:

讓伺服器告訴瀏覽器它傳送的資料屬於什麼檔案型別,使客戶端瀏覽器,區分不同種類的資料,並根據不同的mime呼叫瀏覽器內不同的程式嵌入模組來處理相應的資料。

例如:當content-type 的值設定為text/html和text/plain時,前者會讓瀏覽器把接收到的實體內容以html格式解析,後者會讓瀏覽器以普通文字解析。

response.setcharacterencoding("utf-8");

解決中文亂碼問題

上面的2個可以合併到一起

方式一

response.setcontenttype(contenttype + ";charset=utf-8");

方式二

response.setheader("content-type", contenttype + ";charset=utf-8");

方式一

response.getwriter().print("string字串");

方式二

response.getwriter().println("響應資料");

注意:使用print(strparam)方法返回的資料只能是字串格式。 

response.setheader("content-disposition", "attachment;filename=" + formatfilename);

作用:告訴瀏覽器這個檔案的名字和型別(指定返回的檔案的名稱及副檔名);

注意:1.在設定content-disposition頭欄位之前,一定要設定content-type頭欄位;

2.返回的檔名為中文時,需要對中文進行編碼,否則會造成亂碼。

解決方案:

//

對中文以utf-8格式進行編碼

string formatfilename = urlencoder.encode(filename, "utf-8");

方法二:(ie不相容)

//

為什麼不適用這種方式?因為不相容ie瀏覽器

//string formatfilename = new string(filename.getbytes("utf-8"), "iso-8859-1");

JavaWeb response物件常用操作

方式一response.setcontenttype contenttype 方式二response.setheader content type contenttype response.setcontenttype mime 的作用 讓伺服器告訴瀏覽器它傳送的資料屬於什麼檔案型別,使客戶端瀏覽器...

Oracle data guard常用維護操作命令

data guard是oracle提供的一種高可用性解決方案,用於資料保護和容災,通過日誌同步來把資料及時傳送到備用節點,現總結一下data guard環境下常用的維護命令 1 在生產庫停止data guard操作 sql show parameter log archive dest sql al...

pyspark dataframe的常用操作

1 列重新命名 train data train data.todf imei pkgname timestamp 2 刪除某一列 df df.drop col 3 選取某些列 train data train data.filter train data.date 2021 03 01 4 自定義...