使用smartUpload实现文件的上传与下载

文件的下载

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>");
    }
    }
    }

    %>

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值