我在jsp中设计一个select 并在option自动导入d:\upload目录下的所有doc,xls,zip,rar格式的文件怎么实现,我现在只实现了一种格式的文件,代码如下:
<%@ page language="java" import="java.io.*,java.sql.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%!
public String codeToString(String str){
String s=str;
try{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}catch(Exception e)
{
return s;
}
}
%>
<%!
class filetxt implements FilenameFilter{ //格式String strstrstr=null;
filetxt(String s)
{
strstrstr="."+s;
}
public boolean accept(File dir,String name){
return name.endsWith(strstrstr);
}
}
%>
<html>
<head>
<title>上传文件</title>
</head>
<body >
<form action="accepteUploadFilejl.jsp" method="post" name="up" enctype="multipart/form-data" >
<p align="center">经理上传文件给指定员工 </p>
<hr>
<table border="1" width="200">
<tr >
<td align="left" width="25%"><font size=2>员工姓名</font> </td>
<td align="left" width="50%"><font size=2>指定文件</font> </td>
</tr>
<tr>
<td><select name="wenjian" size="1" >
<%
File file1=new File("D:/upload");
filetxt file_txt=new filetxt("doc"); //只有doc格式,还想要xls,zip,rar格式的文件怎么实现
File file[]=file1.listFiles(file_txt);
for(int i=0 ;i<file.length;i++)
{out.print(file[i].toString());
%>
<option value="<%file[i].toString();%>" selected><%=file[i].toString() %> </option>
<% }%>
</select>
</td>
</tr>
<tr align="center">
<td colspan=2>
<input type="submit" name ="zd" value="指定">
</td>
</tr>
</table>
</form>
</body>
</html>
------解决方案--------------------------------------------------------
JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the JDK.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
从上述代码你可以看到,需要使用ExampleFileFilter的对象进行设置!
------解决方案--------------------------------------------------------
<input type="file" accept="">
accept属性列表
1.accept="application/msexcel"
2.accept="application/msword"
3.accept="application/pdf"
4.accept="application/poscript"
5.accept="application/rtf"