当前位置: 代码迷 >> Java Web开发 >> struts文件下载问题
  详细解决方案

struts文件下载问题

热度:199   发布时间:2008-12-01 22:42:12.0
struts文件下载问题
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得到的参数是中文就有问题呢,,,拔出异常
搜索更多相关主题的帖子: struts  文件  

----------------解决方案--------------------------------------------------------
回复 楼主 fujinliang 的帖子
有跟楼主同样的问题。。。。本人用JSP写个下载,求高手帮帮忙
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ page import="java.io.*" %>
<%
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.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>下载</title>
</head>
<body>
</body>
</html>

为什么那个tempFileName是中文的时候就不能下载呢?
----------------解决方案--------------------------------------------------------
1楼:
downFileName=new String(downFileName.getBytes(),"gbk");
或者 downFileName=new String(downFileName.getBytes(),"gb2312");
downFileName=new String(downFileName.getBytes(),"ISO8859-1");这种只支持英语。
----------------解决方案--------------------------------------------------------
  相关解决方案