请教如何使用JSP浏览服务器上文件夹下的文件 今天使用JSP把上传的功能搞定了,但是考虑到下载就头痛。如何使用JSP浏览服务器上的一个文件夹下的文件,比如说我上传到了/upload文件夹了,怎么才能用JSP浏览到/upload下的文件呢,再进行下载呢。请教高手。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 忙活了老长时间,还是有点问题,不知道如何解决?想用ajax来做。a.jsp:<%@ page contentType="text/html;charset=gbk" import="java.io.*"%><%response.setDateHeader("Expires",0);response.setHeader("Cache-Control","no-cache");response.setHeader("Pragma","no-cache");%><style type="text/css">.like_a_href{ text-decoration:underline; color:#CC3333; cursor:hand}</style><table id="fileTable"><tr><td><span class="like_a_href" path="/upload" onclick="list(this.path)">/upload</span></td></tr></table><script language="javascript" src="ajax.js"></script><script language="javascript">function list(path){ //alert(path); getResult("listFiles.jsp?dir="+path,callback)}function callback(){ if(req.readyState==4){ if(req.status==200){ var files=req.responseXML.getElementsByTagName("file"); var dirs=req.responseXML.getElementsByTagName("dir"); var tbody=document.createElement("tbody"); addToTbody(tbody,files,"file"); addToTbody(tbody,dirs,"dir"); var table=document.getElementById("fileTable"); table.removeChild(table.firstChild); table.appendChild(tbody); } } }function addToTbody(tbody,files,type){ for(var i=0;i<files.length;i++){ var span=document.createElement("span"); span.className="like_a_href"; span.path=files[i].firstChild.nodeValue; var spanText=document.createTextNode(span.path); span.appendChild(spanText); span.onclick=new function(){//这里有问题,为啥呢,相当的郁闷? if(type=="file"){ //alert("做下载"); }else{ //list(span.path) } } var td=document.createElement("td"); td.appendChild(span); var tr=document.createElement("tr"); tr.appendChild(td); tbody.appendChild(tr); }}</script>ajax.js:var req; function getResult(url,callback) { //url为目的页面 //只需要在调用的页面指定一个callback回调函数就OK了! if(window.XMLHttpRequest)req=new XMLHttpRequest(); else if(window.ActiveXObject){ try{ req=new new ActiveXObject("MSxml2.XMLHTTP"); }catch(e){ try{ req=new ActiveXObject("Microsoft.XMLHttp"); }catch(e){} } }else{ alert("创建HttpRequest失败!") return; } req.open("GET",url,true); req.setRequestHeader("ContentType","application/x-www-form-urlencoded"); req.onreadystatechange=callback; req.send(null); }listFiles.jsp:<%@ page contentType="text/xml;charset=gbk" import="java.io.*"%><%response.setDateHeader("Expires",0);response.setHeader("Cache-Control","no-cache");response.setHeader("Pragma","no-cache");out.print("<root>");String dir=request.getParameter("dir");String path=request.getRealPath(dir);File file=new File(path);String subfiles[]=file.list();for(int i=0;i<subfiles.length;i++){ File subfile=new File(path+"/"+subfiles[i]); if(subfile.isDirectory())out.print("<dir>"+dir+"/"+subfiles[i]+"</dir>"); else out.print("<file>"+dir+"/"+subfiles[i]+"</file>");}out.print("</root>");%> 楼上的思路是正确的。你现在服务器端,写一个类,能够读出上传文件夹的所有文件和层次,将结果以字符串(XML或JSON)等相应到前台,前台能够解析这个字符串就OK了。 之前 写网站时 写过 文件上传下载的,好像要在web.xml 配置下 能下载的类型 上传的类网上有,下载就是一个链接,点链接就能下载了 写个类来读取public static String getObjName(String key) { /* 取得文件对象 */ File file = new File(Iconst.FILE_PATH); try { /* 取得文件缓冲流对象 */ BufferedReader reader = new BufferedReader(new FileReader(file)); /* 按行读取文件 */ String line = reader.readLine(); /* 循环读取文件,直到文件结尾 */ while (null != line) { /* 判断当前key值所对应的行是否找到 */ String[] arr = line.split("="); if (arr.length == 2 && arr[0].equals(key)) { /* 如果找到,就返回当前key所对应的value值 */ return arr[1]; } /* 读取下一行 */ line = reader.readLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /* 未找到的情况 */ return ""; } File file = new File(Iconst.FILE_PATH)中的Iconst.FILE_PATH我是在接口中定义常量,来保存文件路径的!~ 求推荐 table 控件 LazyLoad加载问题 js正反页打印 js 操作DOM 用正则表达式检测ASP提交数据的合法性的困惑? 求一个完美的日历控件 跨域iframe问题? 请教正则切分问题[match] javascript怎样知道某天的农历? 怎样做一个可编辑且可多选的Table? 求高人解释一下这个js. js播放从数据库中取出的视频
忙活了老长时间,还是有点问题,不知道如何解决?
想用ajax来做。
a.jsp:
<%@ page contentType="text/html;charset=gbk" import="java.io.*"%>
<%
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
%>
<style type="text/css">
.like_a_href{
text-decoration:underline;
color:#CC3333;
cursor:hand
}
</style>
<table id="fileTable">
<tr><td><span class="like_a_href" path="/upload" onclick="list(this.path)">/upload</span></td></tr>
</table>
<script language="javascript" src="ajax.js"></script>
<script language="javascript">
function list(path){
//alert(path);
getResult("listFiles.jsp?dir="+path,callback)
}
function callback(){
if(req.readyState==4){
if(req.status==200){
var files=req.responseXML.getElementsByTagName("file");
var dirs=req.responseXML.getElementsByTagName("dir");
var tbody=document.createElement("tbody");
addToTbody(tbody,files,"file");
addToTbody(tbody,dirs,"dir");
var table=document.getElementById("fileTable");
table.removeChild(table.firstChild);
table.appendChild(tbody);
}
}
}
function addToTbody(tbody,files,type){
for(var i=0;i<files.length;i++){
var span=document.createElement("span");
span.className="like_a_href";
span.path=files[i].firstChild.nodeValue;
var spanText=document.createTextNode(span.path);
span.appendChild(spanText);
span.onclick=new function(){//这里有问题,为啥呢,相当的郁闷?
if(type=="file"){
//alert("做下载");
}else{
//list(span.path)
}
}
var td=document.createElement("td");
td.appendChild(span);
var tr=document.createElement("tr");
tr.appendChild(td);
tbody.appendChild(tr);
}
}
</script>ajax.js:
var req;
function getResult(url,callback)
{
//url为目的页面
//只需要在调用的页面指定一个callback回调函数就OK了!
if(window.XMLHttpRequest)req=new XMLHttpRequest();
else if(window.ActiveXObject){
try{
req=new new ActiveXObject("MSxml2.XMLHTTP");
}catch(e){
try{
req=new ActiveXObject("Microsoft.XMLHttp");
}catch(e){}
}
}else{
alert("创建HttpRequest失败!")
return;
}
req.open("GET",url,true);
req.setRequestHeader("ContentType","application/x-www-form-urlencoded");
req.onreadystatechange=callback;
req.send(null);
}
listFiles.jsp:
<%@ page contentType="text/xml;charset=gbk" import="java.io.*"%>
<%
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
out.print("<root>");
String dir=request.getParameter("dir");
String path=request.getRealPath(dir);File file=new File(path);
String subfiles[]=file.list();
for(int i=0;i<subfiles.length;i++){
File subfile=new File(path+"/"+subfiles[i]);
if(subfile.isDirectory())out.print("<dir>"+dir+"/"+subfiles[i]+"</dir>");
else out.print("<file>"+dir+"/"+subfiles[i]+"</file>");
}out.print("</root>");
%>
你现在服务器端,写一个类,能够读出上传文件夹的所有文件和层次,将结果以字符串(XML或JSON)等相应到前台,前台能够解析这个字符串就OK了。
/* 取得文件对象 */
File file = new File(Iconst.FILE_PATH);
try {
/* 取得文件缓冲流对象 */
BufferedReader reader = new BufferedReader(new FileReader(file));
/* 按行读取文件 */
String line = reader.readLine();
/* 循环读取文件,直到文件结尾 */
while (null != line) {
/* 判断当前key值所对应的行是否找到 */
String[] arr = line.split("=");
if (arr.length == 2 && arr[0].equals(key)) {
/* 如果找到,就返回当前key所对应的value值 */
return arr[1];
}
/* 读取下一行 */
line = reader.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/* 未找到的情况 */
return "";
}
中的Iconst.FILE_PATH我是在接口中定义常量,来保存文件路径的!~