Web Dev Notes - 16 - 显示FTP服务器上的源代码

自己封装的给网站实验使用的显示FTP上源代码的api,自己觉得挺简单使用的,贴上来记着,以防以后用到。


package api.com.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowSourceCode extends HttpServlet {

	/**
	 * 
	 */
	private static final String server_ip = "***.***.***.***";
	private static final String user = "***";
	private static final String password = "***";

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("gbk");
		response.setCharacterEncoding("gbk");
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();

		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		
		Cookie[] cookies = request.getCookies();
		if (validateUser(cookies)) {
			try{
				printSourceCode(out, request.getParameter("filename"));
			}catch(Exception e){
				out.println("File not found");
			}
		} 
		else {
			//RequestDispatcher rd = request.getRequestDispatcher("relogin.jsp");
			//rd.forward(request,response);
			response.sendRedirect("relogin.jsp");

		}
		out.println("<br/><a href=\"../index.jsp\">返回</a>");		
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	private void printSourceCode(PrintWriter out, String filename) throws Exception{
		
		URL url = new URL(getBaseDir() + filename);		
		InputStream is = url.openStream();
		
		byte[] buffer = new byte[1024];
		int length = is.read(buffer);	
		
//		out.println("<pre>");
		out.println("<xmp>");

		while (length != -1) {
			out.print(new String(buffer, 0, length));
			length = is.read(buffer);
		}

//		out.println("</pre>");
		out.println("</xmp>");
		
	}

	private boolean validateUser(Cookie[] cookies) {
		int length = cookies.length;
		String username = null;
		String userpwd = null;
		for (int i = 0; i < length; i++) {
			if (cookies[i].getName().equals("username")) {
				username = cookies[i].getValue();
				continue;

			}
			if (cookies[i].getName().equals("userpwd")) {
				userpwd = cookies[i].getValue();

			}
		}
//		System.out.println("---ShowSourceCode.java---");
//		System.out.println(length);
//		System.out.println(username);
//		System.out.println(userpwd);
		
		if(username==null || userpwd==null) return false;
		
		if(username.equals(user) && userpwd.equals(password)){
			return true;
		}
		else{
			return false;
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	private String getBaseDir() {
		StringBuilder sb = new StringBuilder();
		sb.append("ftp://");
		sb.append(user);
		sb.append(":");
		sb.append(password);
		sb.append("@");
		sb.append(server_ip);
		sb.append("/javaweb/");
		return sb.toString();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值