使用smartUpload实现文件的上传与下载
2015-11-25 15:18
363 查看
文件的下载
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="删除" onclick="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.在项目中引入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="删除" onclick="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>");
}
}
}
%>
相关文章推荐
- 基于servlet+smartUpload的文件上传
- SmartUpload使用和简介
- 文件下载上传
- Java文件下载和上传源代码
- 上传头像的例子—smartUpload的使用
- JSP表单含有上传功能时出现不能获取表单的其他内容
- 使用smartupload组件上传图片(jsp+servlet)
- SmartUpload 的乱码处理
- 文件上传与下载
- smartUpload控件使用
- 文件上传--Smartupload上传组件【上】
- 文件上传--Smartupload上传组件【中】
- 文件上传--Smartupload上传组件【下】
- 学习SmartUpload组件时,出现文件不能保存的异常
- SmartUpload实现文件的上传与下载
- smartupload实现文件上传时获取表单数据
- 使用jspsmartupload完成简单的文件上传系统
- smartupload下载
- smartupload 的简单使用(上传)
- 在handler中使用SmartUpload上传组件失败