中文乱码问题

appx 2008-12-16 09:13:48
我在实现文件下载过程中涉及到了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);
}
}
...全文
222 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinlu7662032 2008-12-21
  • 打赏
  • 举报
回复
第二行 写错了 参考3楼的
qinlu7662032 2008-12-21
  • 打赏
  • 举报
回复
表单 POST提交 request.setCharacterEncoding("GBK");
GET url提交 String str=new String("ISO8859-1","GBK");

response.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");

GTJin 2008-12-21
  • 打赏
  • 举报
回复
建议使用过滤器,
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中进行设置相应的设置。
GTJin 2008-12-21
  • 打赏
  • 举报
回复
建议使用过滤器,
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中进行设置相应的设置。
youzi530 2008-12-21
  • 打赏
  • 举报
回复
实在不行的话.用过滤器吧.
冬天程序员 2008-12-21
  • 打赏
  • 举报
回复
写个filter吧
ldxfsh 2008-12-16
  • 打赏
  • 举报
回复
java.net.URLEncoder.encode("JSP编程.pdf","GB2312")
改成这样,应该对了
jumpheightway 2008-12-16
  • 打赏
  • 举报
回复
用response设置返回的字符编码
问题可以解决
设置最后和页面一样
或者小于页面编码
nicholasmars 2008-12-16
  • 打赏
  • 举报
回复
String str=new String(str.getBytes("ISO-8859-1"),"gbk")
FoxLovel 2008-12-16
  • 打赏
  • 举报
回复
请把错误贴出来。。

可以确定,你的乱码,是在return new FileStreamInfo(contentType,file);发生,的
还是在这之前就已经乱码了。。。

网上的乱码解决很多。。你可以查一下。。

你的问题,最主要的是,他是在什么时候,开始乱码的。。你改起来就方便了。。
因为,就只是一个编码方式设置,写在哪里的事。。。

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧