本文目錄一覽:
Java中如何設置編碼格式
如果是修改文件的編碼方式,可以按以下步驟:
選中文件右鍵–Properties–Resource–Text file encoding–選中Other,然後選擇自己想要的編碼格式就可以了。
如果是修改MyEclipse的編碼方式,可以按以下步驟:
菜單欄的Window–Perferences–General–Work Space–Text file encoding–選中Other,然後選擇自己想要的編碼格式就可以了。
Java 修改編碼格式的幾種方式
主要分response的字節字符輸出流和request接受中文參數doGet(),doPost()的設置四種.以及從服務器下載文件到瀏覽器的編碼問題.
都是我學習java時總結的,希望能幫到你.
response的字節輸出流:
// 設置瀏覽器默認打開的時候採用的字符集編碼
response.setHeader(“Content-Type”, “text/html;charset=UTF-8”);
// 設置中文轉成字節數組的時候取出的編碼
response.getOutputStream().write(“如果不設置編碼,這裡就是亂碼”.getBytes(“UTF-8”));
response的字符輸出流:
//設置瀏覽器默認打開的時候採用的字符集編碼,response的字符流的緩衝區的編碼.
response.setContentType(“text/html;charset=UTF-8”);
response.getWriter().println(“中文”);
request的doGet()編碼解決:
String name = new String(request.getParameter(“name”).getBytes(“ISO-8859-1″),”UTF-8”);
System.out.println(“GET方式:”+name);
request的doPost()編碼解決:
request.setCharacterEncoding(“UTF-8”);
String name = request.getParameter(“name”);
System.out.println(“POST方式:”+name);
下載文件時瀏覽器編碼問題:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.接收參數
String filename = new String(request.getParameter(“filename”).getBytes(“ISO-8859-1″),”UTF-8”);
System.out.println(filename);
// 2.完成文件下載:
// 2.1設置Content-Type頭(獲取文件的mime類型)
String type = this.getServletContext().getMimeType(filename);
//設置文件的mime類型
response.setHeader(“Content-Type”, type);
// 2.3web項目文件的絕對路徑
String realPath = this.getServletContext().getRealPath(“/download/”+filename);
// 獲得瀏覽器的類型處理中文文件的亂碼問題.(User-Agent:服務器收到客戶端版本之類的一些信息)
String agent = request.getHeader(“User-Agent”);
System.out.println(agent);
if(agent.contains(“Firefox”)){
filename = base64EncodeFileName(filename);
}else{
//IE谷歌編碼
filename = URLEncoder.encode(filename,”UTF-8″);
}
// 2.2設置Content-Disposition頭(固定寫法,讓瀏覽器必須下載,不能直接打開)
response.setHeader(“Content-Disposition”, “attachment;filename=”+filename);
//獲得文件
InputStream is = new FileInputStream(realPath);
// 獲得response指定的方法獲取輸出流:如果用其他流是直接拷貝而不是下載
OutputStream os = response.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b))!= -1){
os.write(b, 0, len);
}
//響應流可以不關,在服務器做出相應後服務器會自動把response獲得的流關閉
is.close();
}
//火狐
public static String base64EncodeFileName(String fileName) {
BASE64Encoder base64Encoder = new BASE64Encoder();
try {
return “=?UTF-8?B?”
+ new String(base64Encoder.encode(fileName
.getBytes(“UTF-8”))) + “?=”;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
java怎麼設置為utf-8的編碼
要讓一個
Java
源文件打開時編碼格式為
UTF-8,需要做2件事情:
1)設置Java
源文件的默認編碼格式為UTF-8;
2)設置workspace的編碼格式為UTF-8。
相應設置如下:
設置
Java
源文件的默認編碼格式為
UTF-8
在
Windows-Preference
頁面中,選擇
General-Content
Types
在右邊的框中,選擇
Text-Java
Source
File,將下面的
Default
Encoding
改為
UTF-8
設置
workspace
的編碼格式為
UTF-8
在
Windows-Preference
頁面中,選擇
General-WorkSpace
選擇右側框中的
Text
File
encoding,改為
UTF-8
設置完這2處,默認打開
Java
源文件就是UTF-8編碼了。
怎麼 知道 java字符串 編碼格式
這樣的測試方法是不正確的。getBytes(charset)是解碼,new
String(byte[],
charset)是編碼。new
String(str.getBytes(charset),charset)是解碼再編碼,無論charset是什麼編碼格式,返回的字符串內容原始str是一致,因此equals方法都是返回true,達不到測試字符串編碼格式的目的。個人觀點:正確的測試姿勢應該是這樣的:
String charset =”xxx”; //假定編碼格式
String str = “中文”;
boolean flag = str.equals(new String(str.getBytes(),charset));flag為true則表明str的編碼格式是假定的編碼格式。其中說明str.getBytes()該方法就是按其自身編碼格式去解碼。其自身編碼格式跟你的操作系統編碼格式或你使用的IDE設置的文件的Text
file
encoding有關。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/309618.html