当前位置: 代码迷 >> JavaScript >> JSP页面中直接操作资料
  详细解决方案

JSP页面中直接操作资料

热度:153   发布时间:2012-11-16 14:12:15.0
JSP页面中直接操作文件
写道
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ 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 class="fg_body" id="fg_body">
<div class="fg_showMessage">
<%=message%>
</div>
<div class="dbfile">
<table width="100%">
<tr>
<td width="100%">
<%
//页面
request.setCharacterEncoding("UTF-8");
String strDir="D:/NCC/ExpDbFile"+"/"+request.getAttribute("uf");
if (strDir != null && !strDir.equals(""))
{
strDir = new String(strDir.getBytes("UTF-8"), "UTF-8");
strDir = strDir.replace('/', '\\');
}

StringBuffer sbFile = new StringBuffer("");
try
{
File objFile = new File(strDir);
File list[] = objFile.listFiles();
for (int i = 0; i < list.length; i++)
{
if (list[i].isDirectory())
{
}
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("<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 class="">
<table width="100%">
<tr>
<td width="100%" align="center" valign="top">
<table class="rmb_tab"
style="behavior:url('/TRAMS/pbac/css/sorttable.htc')" width="100%"
border="1">
<thead>
<tr class="rmb_tab_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>

?

  相关解决方案