struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)

我的前台页是这样的:

<body>
      <form action="test.action" method="post">
          测试文件:<input type="file" id="doc" name="path" value=""/>
          <input type="submit" value="提交" onclick=""/>
      </form>
  </body>

Action:

package com;

import java.io.UnsupportedEncodingException;


import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TestAction extends ActionSupport
{
    private String path;

    public String getPath()
    {
        return path;
    }

    public void setPath(String path)
    {
        this.path = path;
    }


    public String test() throws Exception
    {
        System.out.println(path.replace("\", "\\"));
        return SUCCESS;
    }

}


刚开始的时候一选中文路径就输出???.
后来终于找到解决方法.
在struts.xml文件中加上:
为了解决form提交到action中的中文参数乱码问题。
 
1.在struts2-core-2.0.0-SNAPSHOT.jar包中路径为struts2-core-2.0.6orgapache struts2
有一个default.properties 文件,把struts.i18n.encoding=UTF-8改为

struts.i18n.encoding=GBK 


2.或者在struts.xml文件内添加常量: 

<constant name="struts.i18n.encoding" value="GBK"/>

我设置value="GB2312"


我当然是用的第二种方法,简单方便.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="com" extends="struts-default">
        <action name="test" class="com.TestAction" method="test">
            <result>/ok.jsp</result>
        </action>
    </package>
</struts>


呵呵,终于解决了.希望对大家有些帮助. 

http://www.blogjava.net/supercrsky/articles/170549.html

原文地址:https://www.cnblogs.com/yuhuameng/p/5448402.html