客戶端服務端url中文亂碼解決
阿新 • • 發佈:2019-02-08
你需要把中文轉碼,然後在伺服器進行解碼操作
因為請求連線是不支援中文的
例子如下:
String string = "你好";
String eStr = URLEncoder.encode(string, "utf-8");
System.out.println(eStr);
System.out.println(URLDecoder.decode(eStr, "utf-8"));
上面那個是url用的編碼格式,引數帶那個 然後伺服器解碼:
new String(user.getName().getBytes("iso-8859-1"),"utf-8")
解碼不用URLDecoder,直接得到引數的bytes,然後根據加碼格式解碼
下載檔案只需把檔案寫入response的輸出流即可:
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ new String(sss.getBytes("iso-8859-1"), "utf-8") + "\"");
OutputStream os = response.getOutputStream();
String pathString = request.getRealPath("/")
+ new String(sss.getBytes("iso-8859-1"), "utf-8");
InputStream is = new FileInputStream(new File(pathString));
byte[] buffer = new byte[1024*4];
int len = 0;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
is.close();
os.flush();
os.close();
這樣返回的就是個檔案了
因為請求連線是不支援中文的
例子如下:
String string = "你好";
String eStr = URLEncoder.encode(string, "utf-8");
System.out.println(eStr);
System.out.println(URLDecoder.decode(eStr, "utf-8"));
上面那個是url用的編碼格式,引數帶那個 然後伺服器解碼:
new String(user.getName().getBytes("iso-8859-1"),"utf-8")
解碼不用URLDecoder,直接得到引數的bytes,然後根據加碼格式解碼
下載檔案只需把檔案寫入response的輸出流即可:
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ new String(sss.getBytes("iso-8859-1"), "utf-8") + "\"");
OutputStream os = response.getOutputStream();
String pathString = request.getRealPath("/")
+ new String(sss.getBytes("iso-8859-1"), "utf-8");
InputStream is = new FileInputStream(new File(pathString));
byte[] buffer = new byte[1024*4];
int len = 0;
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
is.close();
os.flush();
os.close();
這樣返回的就是個檔案了