<%@ page language="java" contentType="text/html;charset=gbk"%>
<%@ page import="java.io.*,java.text.SimpleDateFormat,java.util.Vector,java.net.URLEncoder,java.util.Arrays;"%>
<%@ include file = "login.jsp"%>
<%!//页面名称
private static final String LUCY = "index.jsp";/*
pj_unicodeToChinese(String s),将iso8859-1转换为gbk
参数s:要转换的字符串(iso8859-1)
返回:转换后的字符串(gbk)
错误:(1),如果s为空,返回"",(2)当捕获错误时,返回s原样
PJ 2008.2
*/
private static String pj_unicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=new String(s.getBytes("ISO8859_1"),"GBK");
return newstring;
}catch(Exception e){return s;}
}/*
pj_convertFileSize(long fileSize),将byte为单位的文件大小转换windows的文件大小格式
参数fileSize:要转换的文件大小
返回:转换后的字符串,如10KB
PJ 2008.2
*/
private static String pj_convertFileSize(long fileSize) {
String fileSizeOut=null;//size
if(fileSize<1024){
fileSizeOut=fileSize+"byte";
}else if(fileSize>=(1024*1024*1024)){
fileSize=fileSize/(1024*1024*1024);
fileSizeOut=fileSize +"GB";
}else if(fileSize>=(1024*1024)){
fileSize=fileSize/(1024*1024);
fileSizeOut=fileSize +"MB";
}else if(fileSize>=1024){
fileSize=fileSize/1024;
fileSizeOut=fileSize+"KB";
}
return fileSizeOut;
}
/*
getReqValue(String s,String reqName),用于解析上传文件时,获得头部某参数的值
原理是通过先查找两个双引号来获得中间的值
s=字符串
reqName=要查找的文件头字符串,如filename
getReqValue("filename=\"C:\\Documents and Settings\\jun\\桌面\\mainwindowb4\\table_all.txt\"","filename")
PJ 2008.2
*/
private String getReqValue(String s,String reqName){
String reqValue = null; //此请求的值,将返回此值
int reqNameStartIndex = -1; //此请求参数开始部分出现的位置,即filename="的字母f出现的位置
int reqValueStartIndex = -1;//开始"出现的位置
int reqValueEndIndex = -1;//最后"出现的位置
reqName = reqName + "=" + "\"";//reqName=reqName加上等号和双引号即变成filename="
reqNameStartIndex = s.indexOf(reqName);//出现filename="的位置
if(reqNameStartIndex > -1){//如果出现filename="
reqValueStartIndex = reqNameStartIndex + reqName.length();//req值的开始位置=req参数名出现位置+req参数长度
reqValueEndIndex = s.indexOf("\"",reqValueStartIndex);//从参数值开始处即\"开始处查找另外一个\"
  if(reqValueStartIndex > -1 && reqValueEndIndex > -1){ //如果这个参数值有开始有结尾,即有两个双引号都有
    reqValue = s.substring(reqValueStartIndex,reqValueEndIndex);//获得两个双引号之间的内容
  }
}
return reqValue;//返回reqValue
}
%>
<% 
request.setCharacterEncoding("gbk");
response.setHeader("Pragma","No-cache");//HTTP 1.1
response.setHeader("Cache-Control","no-cache");//HTTP 1.0
response.setHeader("Expires","0");//防止被proxy
%>
<html>
<head>
<title>媒体文件下载系统</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head><body>
<%
String resultInfo="";
String URLpath=null;
String path=request.getParameter("path");
boolean error=false;
//if path==null
if(path==null||path.length()<1){
path=request.getRealPath("/media/");
//path=application.getRealPath(".");
}
//path=URLDecoder.decode();
path=pj_unicodeToChinese(path);
if(request.getParameter("errorPath")!=null){
path=request.getParameter("errorPath");
}
//download
if (request.getParameter("download") != null) {
String filePath = request.getParameter("download");
filePath=pj_unicodeToChinese(filePath);
File file=null;
FileInputStream fis=null;
BufferedOutputStream os=null;
try{
file = new File(filePath);
fis = new FileInputStream(file); 
os = new BufferedOutputStream(response.getOutputStream());
fis = new java.io.FileInputStream(filePath); 
response.setContentType("application/octet-stream");
response.setContentLength((int)file.length()); 
response.setHeader("Content-Disposition","attachment; filename=\""+new String(file.getName().getBytes("GBK"),"iso8859-1")+"\"");
byte[] buff=new byte[1024*10];
int i = 0; 
while( (i = fis.read(buff)) > 0 ){ 
os.write(buff, 0, i); 
}
fis.close(); 
os.flush(); 
os.close(); 
}catch(Exception ex){
out.print("<div class='error'>"+new String(ex.getMessage())+"</div>");
if(fis != null)fis.close(); 
if(os != null)os.close(); 
}
}//end
//start view filelist
int objFileError=0;
File objFile=new File(path);
URLpath=URLEncoder.encode(path);
String fileAbsolutePath=null;
String encodeFileAbsolutePath=null;
%>
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td colspan="2"><DIV align="center" class="STYLE1">欢迎进入媒体文件下载系统 </DIV></td>
</tr>
<tr><td>
<%
if(objFile.getParent()==null){out.print("<a href='#' disabled>已是最顶层目录</a> |");}
else{out.print("<a href='"+LUCY+"?path="+URLEncoder.encode(objFile.getParent())+"'></a>  |");}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-d H:mm");
int a=0,b=0;
%><a href="<%=LUCY%>?path=<%=URLpath%>">刷新</a> |
| 媒体版本号:<%
 BufferedReader reader = new BufferedReader(new FileReader("/home/data/media-fuzhu/play_version.txt"));
String str = reader.readLine();
while(str!=null)
{
out.println(str);
str = reader.readLine();
}
 %></td>
  <td align="right"> </td>
</tr>
</table>
<form action="<%=LUCY%>" method="POST" name="FileList">
<div style="background-color:#DEDEDE">
<table width="100%" cellpadding="1" cellspacing="1" id="Filetable">
<thead>
<tr bgcolor="#CCCCCC">
<td width="49%" align="center">文件名称:</td>
<td width="9%" align="center">文件大小:</td>
<td width="17%" align="center">修改日期:</td>
<td width="10%" align="center">媒体下载:</td>
</tr>
</thead>
<tbody>
<%
File fileList[]=objFile.listFiles();
if(fileList!=null) Arrays.sort(fileList);
{
for(int i=0;i<fileList.length;i++){
if(fileList[i].isFile()){ 
b++;
fileAbsolutePath=fileList[i].getAbsolutePath();
encodeFileAbsolutePath=URLEncoder.encode(fileAbsolutePath);
%>
<tr style="background-color:#FFFFFF" onMouseOver="selectRow(this,0);" onMouseOut="selectRow(this,1);" onMouseUp="selectRow(this,2);">
<td>
<img src='file.gif' align="absmiddle" style="margin-left:4px;">
<%=fileList[i].getName()%></td>
<td align="center"><%=pj_convertFileSize(fileList[i].length())%></td>
<td align="center"><%=formatter.format(fileList[i].lastModified())%></td>
<td align="center">
<a href="<%=LUCY%>?download=<%=encodeFileAbsolutePath%>" onMouseUp="dis();">
点击下载</a></td></tr>
<%
}//end if
}//end for
%>
</tbody></table>
</div>
<%
}
%>
<div style="background-color:#CCCCCC;">文件数:<%=b%>
</div><br>
</form>
</body></html>