自己封装的给网站实验使用的显示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();
}
}