首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JSP页面中直接操作资料

2012-11-16 
JSP页面中直接操作文件%@ taglib prefixmatrix urimatrix_dojo% %@page importjava.util.*%

JSP页面中直接操作文件
<%@ taglib prefix="matrix" uri="matrix_dojo"%>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<head>
<matrix:theme />
</head>
<%!
//用于删除文件夹
boolean delFile(String delFilesUrl)
{
try
{
File delFiles = new File(delFilesUrl);
File[] files = delFiles.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
delFile(delFiles + "\" + files[i].getName());
}
else
{
files[i].delete();
}
}
delFiles.delete();
return true;
}
catch (Exception ex)
{
return false;
}
}%>
<%
String message = "";
%>
<%
//得到要删除的文件的文件名字和路径
String delFile = request.getParameter("delFile");
if (delFile != null && !delFile.equals(""))
{
delFile = new String(delFile.getBytes("UTF-8"), "UTF-8");
try
{
File file = new File(delFile);
if (file.delete())
{
message = message + "<font color=green>删除文件成功!<b>";
}
else
{
message = message + "<font color=red>删除文件失败<b>";
}
}
catch (Exception ex)
{
message = message + "<font color=red>异常!<b>";
}
}
%>
<%
//文件下载
String downFile = request.getParameter("file");
if (downFile != null && !downFile.equals(""))
{
String filename = downFile
.substring(downFile.lastIndexOf("\") + 1);
downFile = new String(downFile.getBytes("UTF-8"), "UTF-8");
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(downFile));
byte[] buf = new byte[1024];
int len = 0;
OutputStream os = response.getOutputStream();
response.reset();

//纯下载方式
response.setHeader("Content-Disposition",
"attachment;filename="" + filename + """);
response.setContentType("bin;charset=UTF-8");

while ((len = bis.read(buf)) > 0)
os.write(buf, 0, len);
bis.close();
os.close();
}
%>
<div id="fg_body">
<div + 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("<tbody><tr class='searchListTable' align='center'><td>");
sbFile.append("" +(i+1));
sbFile.append("</td><td>");
sbFile.append("" + list[i].getName());
sbFile.append("</td><td>");
sbFile.append("" + strLen);
sbFile.append("</td><td>");
sbFile.append("" + strDT);
sbFile.append("</td><td align='center'>");
sbFile
.append("<a href='?path="
+ strDir
+ "&delFile="
+ strDir
+ "\"
+ list[i].getName()
+ "' onclick="javascript:return confirm('真的要删除文件"
+ list[i].getName()
+ "吗?')">删除</a>??");
sbFile
.append("<a href='?file=" + strDir + "\"
+ list[i].getName()
+ "'>下载到本地</a>??");
sbFile.append("</td></tr></tbody>\r\n");
}
}
}
catch (Exception e)
{
out.println("<font color=red>" + "</font>");
}
%>
</td>
</tr>
</table>
</div>
<div align="center" valign="top">
<table
style="behavior:url('/TRAMS/pbac/css/sorttable.htc')" width="100%"
border="1">
<thead>
<tr style="position: relative">
<th width="5%" align="center">
序号
</th>
<th width="35%" align="center">
文件名称
</th>
<th width="20%" align="center">
文件大小
</th>
<th width="20%" align="center">
修改时间
</th>
<th width="20%" align="center">
文件操作
</th>
</tr>
<%=sbFile%>
</table>
</td>
</tr>
</table>
</div>
</div>

?

热点排行