首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

中文乱码有关问题

2012-01-08 
中文乱码问题我在实现文件下载过程中涉及到了3个文件。但是其中的中文乱码始终无法解决。具体页面如下,希望

中文乱码问题
我在实现文件下载过程中涉及到了3个文件。但是其中的中文乱码始终无法解决。具体页面如下,希望各位帮忙解决一下啦。给一个具体的乱码处理程序,最好是救灾给的代码上面修改的。是用Struts技术实现哦。
(1)searchAction.jsva页面 
package com.scujcc.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.*;
import java.util.*;

public class SearchAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ArrayList<LabelValueBean> itemList = new ArrayList<LabelValueBean>();
itemList.add(new LabelValueBean("JSP编程.pdf",java.net.URLEncoder.encode("JSP编程.pdf")));
itemList.add(new LabelValueBean("Struts.pdf","Struts.pdf"));
itemList.add(new LabelValueBean("JAVA.txt","JAVA.txt"));
request.setAttribute("itemList", itemList);
return mapping.findForward("success");

}
}

(2)download.jsp页面、<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page isELIgnored ="true" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

  <head>
   
  <title>My JSP 'download.jsp' starting page</title>

  </head>
  
  <body> 
  This is download item.<br>
  <c:if test="${itemList!=null}">
  <table>
  <c:forEach var="item" items="${itemList}">
  <tr>
  <td><c:out value="${item}"/></td>
  <td><html:link action="/downloadFile" paramId="itemid" paramName="item">下载</html:link></td>
  </tr>
  </c:forEach>
  </table>
  </c:if>
  </body>
</html>

(3)downloadFileAction.java页面
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
import java.io.*;
public class DownloadFileAction extends DownloadAction {
public StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response){
// TODO Auto-generated method stub
String contentType="application/OCTET-STREAM;Charset=gb2312";
String filename=request.getParameter("itemid");
response.setHeader("Content-disposition", "attachment;filename="+filename);
File file=new File("D:\\upload\\"+filename);
return new FileStreamInfo(contentType,file);
}
}

[解决办法]
java.net.URLEncoder.encode("JSP编程.pdf","GB2312")


改成这样,应该对了
[解决办法]
建议使用过滤器,
package org.jinxf.mshop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain fc) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
fc.doFilter(request, response);

}

public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}

并在在web.xml中进行设置相应的设置。

热点排行