客戶端服務端url中文亂碼解決

你需要把中文轉碼,然後在服務器進行解碼操作
因爲請求連接是不支持中文的
例子如下:
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();
這樣返回的就是個文件了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章