- try{
- if(filename!=null){
- SmartUpload su=new SmartUpload();
- su.initialize(pageContext);
- su.setContentDisposition(null);
- su.downloadFile("/uploadfile/"+filename);
- out.clear(); //肇加
- out = pageContext.pushBody(); //肇加
- //不加上面两句就可能报错
- //java.lang.IllegalStateException:
- //getOutputStream() has already been called for this response
- }
- // response.sendRedirect("DownloadList.jsp?back=ok");
- }catch(Exception e){
- e.printStackTrace();
- response.sendRedirect("DownloadList.jsp?back=fail");
- }
jspsmart的使用
上传
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 限制每个上传文件的最大长度。
su.setMaxFileSize(10000);
// 限制总上传数据的长度。
su.setTotalMaxFileSize(20000);
// 设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
su.setAllowedFilesList("doc,txt");
// 设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
//jsp,htm,html扩展名的文件和没有扩展名的文件。
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
su.upload();
// 将上传文件全部保存到指定目录
int count = su.save("/upload");
下载
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();
// 初始化
su.initialize(pageContext);
// 设定contentDisposition为null以禁止浏览器自动打开文件,
//保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
//doc时,浏览器将自动用word打开它。扩展名为pdf时,
//浏览器将用acrobat打开。
su.setContentDisposition(null);
// 下载文件
su.downloadFile("/upload/test.doc");
Package com.jspsmart.upload 的API
JspSmart之upload组件源码及使用
http://www.wzsky.net/html/Program/jsp/5860.html