首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

手机端利用html上载程序的实现

2012-12-26 
手机端利用html下载程序的实现web.xmlservletservlet-nameDownLoadServlet/servlet-nameservlet-cl

手机端利用html下载程序的实现
web.xml

<servlet><servlet-name>DownLoadServlet</servlet-name><servlet-class>com.file.DownLoadServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DownLoadServlet</servlet-name><url-pattern>/DownLoadServlet</url-pattern></servlet-mapping>


servlet:
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;public class DownLoadServlet extends HttpServlet {Logger logger = Logger.getLogger(DownLoadServlet.class);public DownLoadServlet() {super();}public void destroy() {super.destroy();}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {logger.info("下载开始");String root = getServletContext().getRealPath("/");String path = "download/";String name = java.net.URLEncoder.encode("物联健身私教 1.0.apk", "UTF-8");name = name.replaceAll("\\+", "%20");name = new String(name.getBytes("iso8859-1"), "gb2312");response.setContentType("application/x-msdownload");response.setHeader("Content-disposition", "attachment; filename="+ name);// 处理请求// 读取要下载的文件File f = new File(root + path + name);if (f.exists()) {FileInputStream fis = new FileInputStream(f);String filename = URLEncoder.encode(f.getName(), "utf-8"); // 解决中文文件名下载后乱码的问题byte[] b = new byte[fis.available()];fis.read(b);response.setCharacterEncoding("utf-8");response.setHeader("Content-Disposition", "attachment; filename="+ filename + "");// 获取响应报文输出流对象ServletOutputStream out = response.getOutputStream();// 输出out.write(b);out.flush();out.close();}}/** * Initialization of the servlet. <br> *  * @throws ServletException *             if an error occurs */public void init() throws ServletException {}}


热点排行