1.我是在GAE上用的Struts2+Spring 框架 (这可能跟这个问题没关系,保险起见我先说一下)
2.并非所有的中文都出现乱码了!
3.我的form表单在没有上传文件(file)控件时,其他的textfield都是中文的!可是一旦form上加了  enctype="multipart/form-data" 属性和 表单里添加了 <s:file> 控件后, action中接受到的 textfield 字段的值就变成乱码了!
4.<constant name="struts.i18n.encoding" value="utf-8"></constant> 我设置了!没用<s:form action="webCaseManagement!addWebCase" method="POST"  enctype="multipart/form-data">
<s:hidden name="webCase.cid"></s:hidden>
<s:textfield name="webCase.caseName" label="案例名称:"></s:textfield>
<s:textarea name="webCase.caseInfo" label="案例介绍:" cols="15"></s:textarea>
<s:textfield name="webCase.caseHref" label="案例链接:"></s:textfield>
<s:file name="imageFile" label="案例图片:"></s:file>
<s:submit value="保存"></s:submit>
</s:form>
以上是我的页面代码!
package com.liyu.bs.action;import java.io.InputStream;
import java.util.Map;import org.apache.commons.io.IOUtils;
import org.apache.struts2.interceptor.RequestAware;import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Text;
import com.liyu.bs.pojo.Img;
import com.liyu.bs.pojo.WebCase;
import com.liyu.bs.service.IImgService;
import com.liyu.bs.service.IWebCaseService;
import com.opensymphony.xwork2.ActionSupport;public class WebCaseManageAction extends ActionSupport implements RequestAware {
/**
 * 
 */
private static final long serialVersionUID = 3919448271882560040L;
private IWebCaseService webCaseService;
private Map<String, Object> request;
private WebCase webCase;
private IImgService imgService;
private String imageFile;
private InputStream photoStream;
private Img img=new Img(); public void setImgService(IImgService imgService) {
this.imgService = imgService;
} public void setWebCaseService(IWebCaseService webCaseService) {
this.webCaseService = webCaseService;
} /**
 * 查询现有所有的技能
 * 
 * @return
 */
public String getAllWebCase() {
try {
request.put("webCaseList", webCaseService.findAll());
} catch (Exception e) {
e.printStackTrace();
return INPUT;
}
return SUCCESS;
} public String webCaseShow() {
// for(int i=1;i<40;i++){
// webCase.setCid(new Long(i));
// deleteWebCase();
// }
return getAllWebCase();
} /**
 * 添加新技能
 * 
 * @return
 */
public String addWebCase()  throws Exception{
img.setImage(new Text(imageFile));
imgService.save(img);
webCase.getImgList().add(img.getId());
String flagString = webCaseService.save(webCase);
getAllWebCase();
this.webCase = null;
this.img=new Img();
// photoStream = IOUtils.toInputStream(imageFile, "ISO-8859-1");
return flagString;
} public String getWebCaseById() {
getAllWebCase();
try {
this.webCase = (WebCase) webCaseService.get(this.webCase.getCid());
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
}
return INPUT;
} public String deleteWebCase() {
String flagString = INPUT;
try {
flagString = webCaseService.delete(this.webCase.getCid());
this.webCase = null;
} catch (Exception e) {
e.printStackTrace();
}
getAllWebCase();
return flagString;
} public void setRequest(Map<String, Object> request) {
this.request = request;
} public WebCase getWebCase() {
return webCase;
} public void setWebCase(WebCase webCase) {
this.webCase = webCase;
} public void setImageFile(String imageFile) {
this.imageFile = imageFile;
} public InputStream getPhotoStream() {
return photoStream;
} public void setPhotoStream(InputStream photoStream) {
this.photoStream = photoStream;
}
}这是我的action代码
有高手给解答一下么?

解决方案 »

  1.   

    GAE好东西,最近也准备搞一下,加了 enctype="multipart/form-data" 属性后,字段要在服务区端取出来后再编码
      

  2.   

    "multipart/form-data以二進制形式提交,
    在取值的時候貌似可以XXX.getString("Utf-8");
      

  3.   

    request.setCharacterEncoding("gb2312");
            response.setCharacterEncoding("gb2312");
    new String(str.getBytes("ISO8859_1"), "UTF8"),进行转码,因为传递的时候,中文默人的是ISO8859_1
    struts2内部传递参数的编码格式要统一下。
      

  4.   

    设置个过滤器继承struts2的核心包然后在xml中直接配置个这个过滤器代替struts2
      

  5.   

    完全不会有这类问题阿.!  没遇到过还.!  不知道LZ是怎么碰到这个的.!
    我以前的项目蛮好, 还是一个文件对应一个描述那一种的.完全不存在中文乱码问题.!
    你看看是不是这里的问题
    private String imageFile;
        private InputStream photoStream;
        private Img img=new Img();

    文件上传对应的三个属性好像我没找到.!  String fileName, String fileContentType, File file
    先看看是否是这个问题