判断用户来源、浏览资源的jsp代码以及实现FTP下载的java代码
判断用户来源、浏览资源的jsp代码以及实现FTP下载的java代码
配套资料:
1.获取用户IP的程序函数jsp代码如下:
<%!public String getIpAddr(HttpServletRequest request){
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("Proxy-Client-IP"); }
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getHeader("WL-Proxy-Client-IP"); }
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getRemoteAddr(); }
return ip;
}
public static long ipToLong(String strIp){
String[]ip = strIp.split("\\.");
return (Long.parseLong(ip[0]) << 24) + (Long.parseLong(ip[1]) << 16) + (Long.parseLong(ip[2]) << 8) + Long.parseLong(ip[3]);
}
%>
2.主页面的HTML代码
<table border='1' width='100%' bgcolor='#F' cellspacing=0 cellpadding=5 bordercolorlight=#0 bordercolordark=#F>
<tr><td width='20%'><img src=images/home.png width=26 height=26>
当前路径:<br/> <br/> (根目录)/<%=rel_ftpPath.replace("/","<span style='color:red'> →</span>") %><span style='color:red'> >></span><br />
</td>
<td width='70%' align='center'><img src=images/banner.png width=800 height=120></td></tr>
</table>
<table border='1' width='100%' bgcolor='#F' cellspacing=0 cellpadding=5 bordercolorlight=#0 bordercolordark=#F>
<tr> <td width='20%'valign="top"> <table > <tr><td><%=sb%></td> </tr> </table> </td> <td width='70%'align="center" valign="top">
<table width="100%"border="1" cellspacing="1" cellpadding="4" bordercolorlight="#0" bordercolordark="#F">
<tr><td width="40%" align="center">点击下载</td>
<td width="10%" align="center">资源文件大小</td>
<td width="15%" align="center">最后修改时间</td>
<td width="10%" align="center">文件下载操作</td></tr>
<tr><td width="100%" colspan="4" align="center"><%=sbFile %></td></tr>
</table>
</td> </tr>
</table>
3.主程序jsp代码
完整的代码,见第五点。
主程序jsp代码:
<% request.setCharacterEncoding("UTF-8");
String strDir =toChinese(request.getParameter("path"));
if(strDir==null||strDir.length()<1){strDir = "/";}
rel_ftpPath=strDir;//相对路径
real_fPath=abs_ftpRoot+rel_ftpPath;//获得真实路径
real_fPath=real_fPath.replace("/","\\");
StringBuffer sb=new StringBuffer("");
StringBuffer sbFile=new StringBuffer("");
try
{ File objFile = new File(real_fPath);
File list[] = objFile.listFiles();
if(objFile.getAbsolutePath().length()>3)
{ String str1=objFile.getParentFile().getAbsolutePath();
if (str1.contains(abs_ftpRoot))
{
String sfp=get_rel_ftpPath(formatPath(objFile.getParentFile().getAbsolutePath()),abs_ftpRoot);
if(sfp==null||sfp.length()<1){
sb.append("<tr><td > </td><td><a href='/demo'>"); //必改
sb.append("<img src=images/Parent.png width=26 height=26>转上级目录(To ParentFile)</a></td><tr>\r\n");
}
else{
sb.append("<tr><td > </td><td><a href='?path="+get_rel_ftpPath(formatPath(objFile.getParentFile().getAbsolutePath()),abs_ftpRoot)+"'>");
sb.append("<img src=images/Parent.png width=26 height=26>转上级目录(To ParentFile)</a></td><tr>\r\n");
}
}
else
{
sb.append("<tr><td > </td><td>");
sb.append("<img src=images/home.png width=26 height=26>FTP根目录(Root)</td><tr>\r\n"); // /r/n
}
}
for(int i=0;i<list.length;i++)
{
if(list[i].isDirectory())
{
sb.append("<tr><td> </td><td>");
String ps=list[i].getAbsolutePath();
sb.append("<img src=images/Dir.png width=26 height=26><a href='?path="+get_rel_ftpPath(formatPath(ps),abs_ftpRoot)+"'>"+list[i].getName()+"</a>");
sb.append("</td></tr>");
}
else
{String strLen="";String strDT="";long lFile=0;lFile=list[i].length();
if(lFile>1000000){lFile=lFile/1000000;strLen="" + lFile + " M";}
else if(lFile>1000){lFile=lFile/1000;strLen="" + lFile + " K";}
else{strLen="" + lFile + " Byte";}
Date dt=new Date(list[i].lastModified());
strDT=dt.toLocaleString();
sbFile.append("<tr><td><img src=images/File.png width=26 height=26>");
sbFile.append(""+list[i].getName());
sbFile.append("</td><td>");
sbFile.append(""+strLen);
sbFile.append("</td><td>");
sbFile.append(""+strDT);
sbFile.append("</td><td>");
sbFile.append(" <input type=\"image\" src=\"images/down.png\" width=26 height=26 title=\" 点击下载 \" onclick=\"ftpDown('"+list[i].getName()+"');\" />");
sbFile.append(" 点击下载</a> ");
sbFile.append("</td></tr>\r\n");
}
}
}
catch(Exception e)
{out.println("<font color=red>error: "+e.toString()+""); }
%>
4.ftpDownFiles函数
请参考:
奋斗的清风. java程序-从FTP服务器上下载文件(支持中文文件名下载)〔DB/OL〕.https://blog.csdn.net/u012461090/article/details/80148263
5.完整代码下载
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.net.*"%>
<%
//gb2312 GBK utf-8 UTF-8 gb18030
//配置参数
String ftpRoot="/";//ftp根或者虚拟目录,如:/,<abc>,<xxxx>,<虚拟目录n>,
String abs_ftpRoot="E:\\Download\\SharedFolders";//"E:\\E_Pan";//ftpRoot对应的ftp绝对路径
String rel_ftpPath="";//相对路径Relative Path,相对于ftpRoot根(目录)的路径,不包括文件
String real_fPath="";//实际的文件路径,real_fPath=abs_ftpRoot+rel_ftpPath,即:strDir;
String ftpFile="";//ftp文件
String real_fName="";//实际的文件名,real_fName=ftpFile
//函数to_abs_ftpRoot(ftpRoot),ftpRoot==>>转变为:abs_ftpRoot
//函数to_ftpRoot(abs_ftpRoot),abs_ftpRoot==>>转变为:ftpRoot
//函数String get_rel_ftpPath(String s_real_fPath,String sftpRoot);根据实际路径获得相对ftpRoot的相对路径
//html网页传递的get参数 path=ftpRoot+rel_ftpPath
String strFTPServerIP_Port="172.16.101.52";//:21";//ftp服务器的IP与端口,如果端口为21,可以省略“:21”
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>JeeFTP教育资源管理系统Demo</title>
<style type="text/css"> td,select,input,body{font-size:9pt;} A { text-decoration: none } #tablist{ padding: 5px 0; margin-left: 0; margin-bottom: 0; margin-top: 0.1em; font:9pt; } #tablist li{ list-style: none; display: inline; margin: 0; } #tablist li a{ padding: 3px 0.5em; margin-left: 3px; border: 1px solid ; background: #FFFFFF; } #tablist li a:link, #tablist li a:visited{ color: navy; } #tablist li a.current{ background: #FFFFFF; } #tabcontentcontainer{ width: 100%; padding: 5px; border: 1px solid black; } .tabcontent{ display:none; }
</style>
</head>
<body>
<%! private final static int languageNo=0; //语言版本,0 : 中文; 1:英文
String[] strCurrentFolder= {"当前目录","Current Folder"};
String[] strDrivers = {"驱动器","Drivers"};
String[] strFileName = {"资源文件名称","File Name"};
String[] strFileSize = {"资源文件大小","File Size"};
String[] strLastModified = {"最后修改时间","Last Modified"};
String[] strFileOperation= {"文件下载操作","Operations"};
String[] strFileEdit = {"修改","Edit"};
String[] strFileDown = {"点击下载","Click to Download"};
String[] strFileCopy = {"复制","Move"};
String[] strFileDel = {"删除","Delete"};
String getDrivers()
{
StringBuffer sb=new StringBuffer(": ");
File roots[]=File.listRoots();
for(int i=0;i<roots.length;i++)
{
sb.append("<a href='?path="+roots[i]+"'>");
sb.append(roots[i]+"</a> "); }
return sb.toString();
}
String formatPath(String p)
{
/*//String s_iso88591 = new String(p.getBytes("UTF-8"),"ISO-8859-1");8859_1
String s_iso88591 = new String(p.getBytes("ISO-8859-1"));
return s_iso88591;
StringBuffer sb=new StringBuffer(new String(p.getBytes("UTF-8"),"ISO-8859-1"));
//sb=,"ISO-8859-1"
//String p_iso88591 = new String(sb,"ISO-8859-1");
//UTF-8 gb2312 gb18030 GBK iso8859-1 ISO-8859-1
return sb.toString();//p_iso88591;
*/
StringBuffer sb=new StringBuffer();
for(int i = 0; i < p.length(); i++)
{
//if(p.charAt(i)=='//'){sb.append("////");}
if(p.charAt(i)=='/'){ sb.append("//"); }
else
{
sb.append(p.charAt(i));
}
}
return sb.toString();
}
public static String toChinese(String str)
{
return str;
}
String get_rel_ftpPath(String s_real_fPath,String sftpRoot)
{
String s_rel="";
//if(ftpRoot在real_fPath,则剔除)
s_rel=s_real_fPath.replace(sftpRoot,"");
s_rel=s_rel.replace("\\","/");
//将\变换为/
return s_rel;
}
%>
<% request.setCharacterEncoding("UTF-8");// ("UTF-8");gb2312 GBK UTF-8 gb18030
String strDir =toChinese(request.getParameter("path"));
//String strDir =request.getParameter("path");
if(strDir==null||strDir.length()<1)
{
strDir = "/";//request.getRealPath("/");
}
rel_ftpPath=strDir;//这里strDir已修改为相对路径
real_fPath=abs_ftpRoot+rel_ftpPath;//即:原来的strDir:real_fPath=strDir;//万江波加,获得真实路径
//rel_ftpPath=get_rel_ftpPath(real_fPath,abs_ftpRoot);//万江波加 根据实际路径获得相对ftpRoot的相对路径
real_fPath=real_fPath.replace("/","\\");
StringBuffer sb=new StringBuffer("");
StringBuffer sbFile=new StringBuffer("");
try
{
//File objFile = new File(strDir);
File objFile = new File(real_fPath);
File list[] = objFile.listFiles();
if(objFile.getAbsolutePath().length()>3)
{
String str1=objFile.getParentFile().getAbsolutePath();
if (str1.contains(abs_ftpRoot))
{
String sfp=get_rel_ftpPath(formatPath(objFile.getParentFile().getAbsolutePath()),abs_ftpRoot);
if(sfp==null||sfp.length()<1){
sb.append("<tr><td > </td><td><a href='/demo'>"); //必改
sb.append("<img src=images/Parent.png width=26 height=26>转上级目录(To ParentFile)</a></td><tr>\r\n"); // /r/n
}
else{
sb.append("<tr><td > </td><td><a href='?path="+get_rel_ftpPath(formatPath(objFile.getParentFile().getAbsolutePath()),abs_ftpRoot)+"'>");
sb.append("<img src=images/Parent.png width=26 height=26>转上级目录(To ParentFile)</a></td><tr>\r\n"); // /r/n
}
}
else
{
sb.append("<tr><td > </td><td><!--a href='?path="+formatPath(objFile.getParentFile().getAbsolutePath())+"'-->");
sb.append("<img src=images/home.png width=26 height=26>FTP根目录(Root)<!--/a--></td><tr>\r\n"); // /r/n
}
}
for(int i=0;i<list.length;i++)
{
if(list[i].isDirectory())
{
sb.append("<tr><td> </td><td>");
String ps=list[i].getAbsolutePath();
//sb.append("<a href='?path="+formatPath(ps)+"'>"+list[i].getName()+"("+formatPath(ps)+"):"+toChinese(formatPath(ps))+"</a>");
//sb.append("<img src=images/Dir.png width=26 height=26><a href='?path="+rel_ftpPath.replace(formatPath(ps),abs_ftpRoot)+"'>"+formatPath(ps)+list[i].getName()+"</a>");
sb.append("<img src=images/Dir.png width=26 height=26><a href='?path="+get_rel_ftpPath(formatPath(ps),abs_ftpRoot)+"'>"+list[i].getName()+"</a>");
sb.append("</td></tr>");
}
else
{
String strLen="";
String strDT="";
long lFile=0;
lFile=list[i].length();
if(lFile>1000000)
{
lFile=lFile/1000000;
strLen="" + lFile + " M";
}
else if(lFile>1000)
{
lFile=lFile/1000;
strLen="" + lFile + " K";
}
else
{
strLen="" + lFile + " Byte";
}
Date dt=new Date(list[i].lastModified());
strDT=dt.toLocaleString();
sbFile.append("<tr><td><img src=images/File.png width=26 height=26>");
sbFile.append(""+list[i].getName());
sbFile.append("</td><td>");
sbFile.append(""+strLen);
sbFile.append("</td><td>");
sbFile.append(""+strDT);
sbFile.append("</td><td>");
sbFile.append(" <input type=\"image\" src=\"images/down.png\" width=26 height=26 title=\" "+strFileDown[languageNo]+" \" onclick=\"ftpDown('"+list[i].getName()+"');\" />");
sbFile.append(strFileDown[languageNo]+"</a> ");
sbFile.append("</td></tr>\r\n");// /r/n
}
}
}
catch(Exception e)
{
out.println("<font color=red>error: "+e.toString()+""); }
%>
<table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF>
<tr><td width='20%'><!--img src=images/curFolder.png width=26 height=26--><img src=images/home.png width=26 height=26>
当前路径:<br/> <br/>      (根目录)/<%=rel_ftpPath.replace("/","<span style='color:red'> →</span>") %><span style='color:red'> >></span><br />
<!--<%=strCurrentFolder[languageNo]%>:<%=strDir %>-->
</td>
<td width='70%' align='center'><img src=images/banner.png width=800 height=120><!--<%=strDrivers[languageNo] %><%=getDrivers()%>--> </td></tr>
</table>
<table border='1' width='100%' bgcolor='#FFFFFF' cellspacing=0 cellpadding=5 bordercolorlight=#000000 bordercolordark=#FFFFFF>
<tr> <td width='20%'valign="top"> <table > <tr><td><%=sb%></td> </tr> </table> </td> <td width='70%'align="center" valign="top">
<table width="100%"border="1" cellspacing="1" cellpadding="4" bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr><td width="40%" align="center"><%=strFileName[languageNo] %></td>
<td width="10%" align="center"><%=strFileSize[languageNo] %></td>
<td width="15%" align="center"><%=strLastModified[languageNo] %></td>
<td width="10%" align="center"><%=strFileOperation[languageNo] %></td></tr>
<tr><td width="100%" colspan="4" align="center"><%=sbFile %></td></tr>
</table>
</td> </tr>
</table>
</body>
<script type="text/javascript" language="javascript">
function ftpDown(sPath)
{
var url='ftp://' +'<%=strFTPServerIP_Port %>'+'/<%=get_rel_ftpPath(real_fPath,abs_ftpRoot) %>/'+sPath;
window.location.href=url;// alert("下载文件:"+url);
}
</script>
<!--作者:一策书-->
</html>
6.完整代码及附属文件下载地址:
链接:https://pan.baidu.com/s/1Ipi4RoYRxj37laQ5G70xyQ
提取码:gqwo
作者:一策书(湘岳阳万江波)
元·刘因《村居杂诗》
芳芬皆可籍,缓步即吾车[jū];
乘兴三杯酒,随行一策书[xū]。
——智慧人生,始自田园美景。 (一策书(湘岳阳万江波))。
蒲公《咏史》
良马非不骏,盐坂徒悲鸣。
美玉非不贵,抱璞为世轻。
高士卧隆中,畴乃知其名?
从容起南阳,谈笑魏吴惊。
男儿事蚕桑,后世有何称?
——智慧世界,源自码农寒士。(一策书(湘岳阳万江波))。