热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jettyjava文件下载代码_Jetty+Servlet实现文件下载

Jetty非常适合做嵌入式web开发,正如Jetty的口号Don’tdeployyourapplicationinJetty,deployJettyinyourap

Jetty非常适合做嵌入式web开发,正如Jetty的口号"Don’t deploy your application in Jetty, deploy Jetty in your application!"。

这个简单的例子是使用Jetty提供文件下载:

首先pom.xml配置文件如下

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

4.0.0

com.luangeng

jettydownload

1.0-SNAPSHOT

jar

jettydownload

http://maven.apache.org

UTF-8

8.1.9.v20130131

org.eclipse.jetty.aggregate

jetty-webapp

${jetty.version}

org.eclipse.jetty

jetty-jsp

${jetty.version}

junit

junit

3.8.1

test

View Code

---

main方法启动Jetty:

package com.luangeng;

import org.eclipse.jetty.server.Connector;

import org.eclipse.jetty.server.Handler;

import org.eclipse.jetty.server.Server;

import org.eclipse.jetty.server.handler.DefaultHandler;

import org.eclipse.jetty.server.handler.HandlerCollection;

import org.eclipse.jetty.server.nio.SelectChannelConnector;

import org.eclipse.jetty.servlet.ServletContextHandler;

import org.eclipse.jetty.webapp.WebAppContext;

public class App

{

public static void main( String[] args ) throws Exception {

Server server = new Server();

server.setStopAtShutdown(true);

SelectChannelConnector connector = new SelectChannelConnector();

connector.setPort(8080);

connector.setReuseAddress(true);

server.setConnectors(new Connector[] { connector });

ServletContextHandler context = new ServletContextHandler();

context.setContextPath("/lg");

context.addServlet(DownLoadServlet.class, "/download");

context.addServlet(TestServlet.class, "/test/*");

HandlerCollection handlers = new HandlerCollection();

handlers.setHandlers(new Handler[] { context, new DefaultHandler() });

server.setHandler(handlers);

server.start();

server.join();

}

}

---

提供简单的测试 TestServlet.java

packagecom.luangeng;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.PrintWriter;/*** Created by LG.*/

public class TestServlet extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();out.println("");

out.println("

test");

out.println("

");out.println("GET method");

out.println("");

out.println("");

out.flush();

out.close();

}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();out.println("");

out.println("

test");

out.println("

");out.println("POST method");

out.println("");

out.println("");

out.flush();

out.close();

}

}

---

完成下载功能的Servlet:

DownloadServlet.java

packagecom.luangeng;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import java.io.*;importjava.net.URLEncoder;/*** Created by LG.*/

public class DownLoadServlet extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response) {//PrintWriter pw = null;

FileInputStream in = null;

OutputStream outs= null;//String path = request.getParameter("filepath");

String path = "/Users/LG/test.txt";//path = new String(fileName.getBytes("iso8859-1"),"UTF-8");

try{

File file= newFile(path);if(!file.exists())

{

response.setStatus(404);//pw.println("file not found!");//request.getRequestDispatcher("/err.jsp").forward(request, response);

return;

}

response.setContentType("text/html");

String realname= path.substring(path.indexOf("/")+1);

response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));

in= newFileInputStream(path);

outs=response.getOutputStream();byte buffer[] = new byte[1024];int len = 0;while ((len = in.read(buffer)) > 0) {

outs.write(buffer,0, len);

}

System.out.println(response.getStatus());

}catch(Exception e)

{//pw.println(e.getMessage());

response.setStatus(300);//log

}finally{

response.setStatus(200);try{if(outs != null) {

outs.flush();

outs.close();

}if(in != null) {

in.close();

}

}catch(IOException e) {

}//if(pw!=null) {//pw.flush();//pw.close();//}

}

}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

doGet(request, response);

}

}

---

end



推荐阅读
author-avatar
罗群霞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有