Struts1.2文件下载(解决中文乱码)
2009-08-27 11:44
525 查看
JSP页面写的比较简单就是一个提交按纽,只为演示
download.jsp
<%...@ page language="java" pageEncoding="utf-8"%>
<%...@ taglib uri="http://struts.apache.org/tags-bean" divfix="bean" %>
<%...@ taglib uri="http://struts.apache.org/tags-html" divfix="html" %>
<%...@ taglib uri="http://struts.apache.org/tags-logic" divfix="logic" %>
<%...@ taglib uri="http://struts.apache.org/tags-tiles" divfix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>下载</title>
</head>
<body>
<html:form action="download.do" method="post">
<html:submit value="下载文件"></html:submit>
</html:form>
</body>
</html:html>
Action
/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
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 com.struts.form.DownloadForm;
/** *//**
* MyEclipse Struts
* Creation date: 03-29-2008
*
* XDoclet definition:
* @struts.action path="/download" name="downloadForm" input="/form/download.jsp" scope="request" validate="true"
*/
public class DownloadAction extends Action ...{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException ...{
DownloadForm d = (DownloadForm) form;
InputStream is=null;
OutputStream os=null;
String path = "JAVA问题.doc";
/**//*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/
String newpath=URLEncoder.encode(path,"utf-8");
byte[] b=new byte[1024];
int i=0;
try ...{
is = new FileInputStream("d:/"+path);
os = response.getOutputStream();
/**//*在页面上弹出一个下在窗口*/
response.setContentType("application/x-msdownload");
/**//*设置报头信息,弹出窗口中显示的文件名 newpath*/
response.setHeader("Content-Disposition", "Disposition; filename="+newpath);
/**//*具体的输入输出流操作*/
while((i=is.read(b))!=-1)...{
os.write(b, 0, i);
i=0;
}
os.flush();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}finally...{
try ...{
os.close();
is.close();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
download.jsp
<%...@ page language="java" pageEncoding="utf-8"%>
<%...@ taglib uri="http://struts.apache.org/tags-bean" divfix="bean" %>
<%...@ taglib uri="http://struts.apache.org/tags-html" divfix="html" %>
<%...@ taglib uri="http://struts.apache.org/tags-logic" divfix="logic" %>
<%...@ taglib uri="http://struts.apache.org/tags-tiles" divfix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>下载</title>
</head>
<body>
<html:form action="download.do" method="post">
<html:submit value="下载文件"></html:submit>
</html:form>
</body>
</html:html>
Action
/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
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 com.struts.form.DownloadForm;
/** *//**
* MyEclipse Struts
* Creation date: 03-29-2008
*
* XDoclet definition:
* @struts.action path="/download" name="downloadForm" input="/form/download.jsp" scope="request" validate="true"
*/
public class DownloadAction extends Action ...{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws UnsupportedEncodingException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException ...{
DownloadForm d = (DownloadForm) form;
InputStream is=null;
OutputStream os=null;
String path = "JAVA问题.doc";
/**//*解决中文乱码问题,设置后产生一个新的String对象此对象以改变了编码*/
String newpath=URLEncoder.encode(path,"utf-8");
byte[] b=new byte[1024];
int i=0;
try ...{
is = new FileInputStream("d:/"+path);
os = response.getOutputStream();
/**//*在页面上弹出一个下在窗口*/
response.setContentType("application/x-msdownload");
/**//*设置报头信息,弹出窗口中显示的文件名 newpath*/
response.setHeader("Content-Disposition", "Disposition; filename="+newpath);
/**//*具体的输入输出流操作*/
while((i=is.read(b))!=-1)...{
os.write(b, 0, i);
i=0;
}
os.flush();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}finally...{
try ...{
os.close();
is.close();
} catch (IOException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
相关文章推荐
- Struts1.2文件下载(解决中文乱码)
- struts2.2.1文件下载及中文乱码问题解决
- 用火狐浏览器下载中文文件名的文件乱码及其解决
- struts2解决下载中文文件乱码问题
- 解决Java下载文件,文件名包含中文在IE浏览器乱码问题
- 文件下载 response.setHeader()下载中文文件名乱码问题 解决办法
- 在Servlet中以下载的方式向浏览器写数据--中文文件乱码问题解决
- response常见应用(中文数据乱码解决,中文文件下载,随机图片等)
- plsql连接Oracle数据库、解决plsql中文乱码、免注册在Oracle官网下载文件、Oracel Database全套下载链接
- 解决Asp.net 实现文件下载时、输出文件时文件名的中文乱码和空格异常
- 文件下载解决中文乱码(十二)
- java下载文件在各个浏览器差异 以及中文乱码解决
- struts的文件下载用法及中文名乱码解决办法
- commons-FileUpload文件上传下载,及中文乱码、IE7路径错误问题解决
- java实现文件下载 中文文件名乱码的解决
- 使用Content-Disposition: attachment; filename=FILENAME,在Firefox浏览器中下载文件,文件名中文乱码问题解决。
- java实现中文文件下载,解决乱码问题,亲测无问题~~
- java下载文件中文乱码情况解决方法
- 从服务上下载下的日志文件,用UltraEdit打开时中文是乱码解决方法
- 解决Asp.net 实现文件下载时、输出文件时文件名的中文乱码和空格异常