0
我正在登錄頁面上有一個ententType multipart/formdata的形式,其中包含用戶輸入任何語言的數據的輸入字段。我正在使用ajax(FormData)將數據發送到服務器,在服務器我沒有正確獲取數據,因爲我無法轉換爲unicode。我已將頁面編碼設置爲utf-8。我想這有問題的標題。 有人請幫助我。如何通過Ajax發送unicode字符到servlet?
這裏是我的代碼:
客戶:
var formdata=new FormData();
formdata.append("servname",servname);
formdata.append("disptype1",disptype1);
formdata.append("text",text);
formdata.append("back",back);
formdata.append("servtype",servtype);
formdata.append("caption",caption);
formdata.append("selsubservices",selsubservices);
formdata.append("action","addinfo");
formdata.append("iconfile",iconfile);
$.ajax({
type: "POST",
url: 'settingServlet',
data: formdata,
//dataType: 'json',
//contentType: "multipart/form-data",
//processData: false,
contentType:false,
processData:false,
success: function (response) {
processAddResponse(response);
},
error: function (error) {
alert("Error occured at" +error);
}
});
服務器:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload uploads = new ServletFileUpload(factory);
List<FileItem> formItems = uploads.parseRequest(request);
if (formItems != null && formItems.size() > 0)
{
for (FileItem item : formItems)
{
if (item.isFormField())
{
//upload formdata except files
String fieldname = item.getFieldName();
if (fieldname.equals("roomname"))
{
name = item.getString();//data corrupts here.
name = new String(roomname.getBytes("iso-8859-1"),"UTF-8");
name = convert(roomname);
name = JUtility.toHTML(roomname);
}
}
}
我不得不設置的ContentType:假的,因爲我不能正確處理多數據。 – Krish