您的位置:首页 > 编程语言 > Java开发

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;


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐