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
+ "\\"