文件的下载
1.在项目中引入smartupload.jar,放在WEB_INF的lib下面
2.在upload.jsp页面中
<form name="form1" id="form1" method="post" action="cs/toUploadFile.jsp" enctype="multipart/form-data">
<input type="file" name="file1"><br/>
<input type="submit" name="submit" value="提交">
</form>
3.在toUploadFile.jsp页面中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*,java.io.File"%>
<%
//SmartUpload类用于实现文件的上传与下载操作
SmartUpload su=new SmartUpload("UTF-8");
//初始化su对象
su.initialize(pageContext);
//准备文件的上传
su.upload();
//获取上传的文件
SmartFile sf=su.getFiles().getFile(0);
String newFileName=sf.getFileName();
//使用相对路径
String sitePath=request.getRealPath("/");
File f=new File(sitePath);
//使用物理路径
//File f=new File("D:\\FileUpload");
if(!f.exists()){
//如果不存在目录,创建一个目录
f.mkdir();
}
//String filePath="D:\\FileUpload\\"+newFileName;
String filePath=sitePath+"\\FileUpload\\"+newFileName;
try{
sf.saveAs(filePath,SmartFile.SAVEAS_PHYSICAL);
}
catch (Exception ex){
out.println("<script>alert('文件上传失败');location.href='../uploadFile.jsp';</script>");
}
out.println("<script>alert('文件上传成功');location.href='../uploadFile.jsp';</script>");
%>
下载文件
1.在downLoad.jsp页面中
<table id="table1" border="1px">
<tr><th>文件名</th><th>文件类型</th><th>文件大小</th><th>操作</th></tr>
<%
String filePath=request.getRealPath("/")+"\\FileUpload";
java.io.File f=new java.io.File(filePath);
//java.io.File f=new java.io.File("D:\\FileUpload");
if(f.isDirectory()){
java.io.File[] fArr=f.listFiles();
for(java.io.File fi: fArr){
//获取文件名字
String filename=fi.getName();
//获取文件的扩展名
String fileEx=fi.getName().substring(fi.getName().lastIndexOf("."));
//获取文件的大小
long fileSize=fi.length()/1024;
%>
<tr><td><%=filename %></td><td><%=fileEx %></td><td><%=fileSize %></td>
<td>
<form name="form2" id="form2" method="post" action="cs/toDownloadFile.jsp" >
<input type=hidden name="filename" value="<%=filename %>"><br/>
<input type=hidden name="eventTarget" id="eventTarget" value="down"><br/>
<input type="submit" name="submit1" value="下载">
<input type="button" name="button1" value="删除" οnclick="btnDel_click()">
</form>
</td>
</tr>
<%
}
}
%>
</table>
2.在toDownloadFile.jsp页面中
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*,java.io.File"%>
<%
request.setCharacterEncoding("UTF-8");
String et=request.getParameter("eventTarget");
if(et.equals("down")){
String downFileName=request.getParameter("filename");
String downPath=request.getRealPath("/")+"\\FileUpload\\"+downFileName;
//SmartUpload类用于实现文件的上传与下载操作
SmartUpload su=new SmartUpload("UTF-8");
//初始化su对象
su.initialize(pageContext);
su.setContentDisposition(null);
try{
su.downloadFile(downPath);
}
catch(Exception ex){
out.println("<script>alert('下载文件失败');location.href='../uploadFile.jsp';</script>");
}
//bug问题
out.clear();
out=pageContext.pushBody();
}
else{
String delFileName=request.getParameter("filename");
String delPath=request.getRealPath("/")+"\\FileUpload\\"+delFileName;
java.io.File f=new java.io.File(delPath);
if(f.exists()){
if(f.delete()){
out.println("<script>alert('删除成功');location.href='../uploadFile.jsp';</script>");
}
}
}
%>