中文乱码问题
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);
}
}