1. 程式人生 > >關於《JavaWeb開發實戰經典》中接收封裝表單的文字資料出現中文亂碼問題的解決方法

關於《JavaWeb開發實戰經典》中接收封裝表單的文字資料出現中文亂碼問題的解決方法

smartupload亂碼問題

今天在寫smartupload的demo時,在接收表單資料時出現了中文亂碼,從網上綜合了各種方法才得以解決。

1.smartupload.html中可見對form使用enctype進行表單封裝

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>www.mldnjava.cn</title>
</head>
<body>
<form action="smartupload_demo02.jsp" method=
"post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上傳"> <input type="reset" value="重置"> </form> </body> </html>

2.如下對request設定統一編碼格式為UTF-8或GBK等都無用

<
%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.lxh.smart.*,java.io.File" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1"> <title>www.mldnjava.cn</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); %> <% SmartUpload smart = new SmartUpload(); smart.initialize(pageContext); smart.upload(); String name=new String(smart.getRequest().getParameter("uname")); smart.save("D:"+File.separator+"上傳的檔案"); %> <h2><%=name %></h2> <h2>request無法取得:<%=request.getParameter("uname") %></h2> </body> </html>

由於對form設定enctype導致資料採用純二進位制傳輸,而SmartUpload的API無解決方案。可直接修改SmartUpload原始碼統一內部編碼,這裡不過多詳解,自尋百度即可。

其餘解決方案 1.修改SmartUpload原始碼 2.使用其他上傳元件,如:FileUpload 3.使用其他資料傳輸方式