注册 登录
编程论坛 J2EE论坛

struts文件下载问题

fujinliang 发布于 2008-12-01 22:42, 1667 次点击
ACTION中:
public InputStream getTargetFile() throws Exception{
        return ServletActionContext.getServletContext().getResourceAsStream("upload\\"+getDownloadFileName());
    }
    public String getDownloadFileName(){
        
         String downFileName=ServletActionContext.getRequest().getParameter("name");
        try{
            downFileName=new String(downFileName.getBytes(),"ISO8859-1");
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return downFileName;
    }
怎么name得到的参数是中文就有问题呢,,,拔出异常
3 回复
#2
linwu_20062008-12-02 20:33
回复 楼主 fujinliang 的帖子
有跟楼主同样的问题。。。。本人用JSP写个下载,求高手帮帮忙
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ page import="*" %>
<%
request.setCharacterEncoding("GBK");
String tempFileName="1.txt";//文件名字

byte b[]=new byte[10240];
//建立文件
File fileLoad=new File(application.getRealPath("/Upload"),tempFileName);

response.setHeader("Content-disposition","attachment;filename="+tempFileName);
response.setContentType("application/x-msdownload");

long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);

//读取文件并发送到客户端下载
OutputStream o=response.getOutputStream();
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1){
    o.write(b,0,n);
}
in.close();
o.close();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>下载</title>
</head>
<body>
</body>
</html>

为什么那个tempFileName是中文的时候就不能下载呢?
#3
5981564122008-12-04 14:16
1楼:
 downFileName=new String(downFileName.getBytes(),"gbk");
或者 downFileName=new String(downFileName.getBytes(),"gb2312");
 downFileName=new String(downFileName.getBytes(),"ISO8859-1");这种只支持英语。
1