`
Circle
  • 浏览: 11320 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论
阅读更多
   上海夜未眠
正在受苦或正在摆脱受苦的人是没有权利诉苦的。
登录注册全站当前博客空间博客好友相册留言用户操作
[即时聊天] [发私信] [加为好友]
richard MengID:success_dream

共201552次访问,排名346好友10人,关注者29人
诚实,进取.
success_dream的文章
原创 271 篇
翻译 7 篇
转载 164 篇
评论 99 篇
上海夜未眠的公告
 
最近评论
abc:驾校
澳洲留学
留学澳洲
货架
<……
ver:不知道从什么时候开始高空作业成了一个热门的行业.工资高的不得了,一般在上岗之前要经过专业的培训.先把烟囱刷航标放上去,再模拟烟囱维修,看看是怎么新建烟囱……
Hugh:powercenter價格如果是連sybase和oracle會到多高的地步?
wqe09f:wqe09f
gtg09y
wow gold
wow gold
wow gold
pageContext??
文章分类
DataBase/Data Warehouse(RSS)
Eclipse(RSS)
Hibernate/DAO(RSS)
J2EE/EJB/JMS/JMAIL(RSS)
Java基础应用(RSS)
Java求职(RSS)
Java设计模式(RSS)
Swing/SWT/Applet(RSS)
Tomcat/Weblogic/Jboss/websphere(RSS)
UNIX/LINUX(RSS)
Web开发(RSS)
XML/WEB SERVICE(RSS)
金融(RSS)
框架/开源(RSS)
生活/文化/随笔(RSS)
实用英语(RSS)
算法研究(RSS)
项目开发总结(RSS)
收藏
好帖收藏
相册
北京/上海的景色
我的朋友们
我的相册
IT+管理
AMT公共知识库
IT经理世界
中国文档网
信息周刊
福布斯
IT人的英语学习网站
business english
CCTV 9
chinadaily
ICS
ICS
shanghai-star
中国英语学习网
托业英语论坛
托业论坛2
爱词霸
英语听力
贝壳网
UNIX/LINUX
UNIX
官网学习站点
BEA
BEA dev2dev
BI第一门户
IBM developerWorks
ITPUB
javaeye
Java技术开发网
JBOSS
jdk1.5中文版API
mysql
oracle学习网
Spring中文论坛
中国eclipse论坛
中国JAVA技术网
中国oracle用户讨论组
数据仓库之路
用友软件论坛
金融
新浪财经
LaoK的博客
qq证券
wind资讯
东方财富网
中国农业银行
中国基金网
中国基金网2
中国建设银行
中国证券报
优利基金
兴业银行
刘杉的博客
叶 弘
和讯
富兰克林
搜财网
易秋霖的BLOG
晨星
模拟炒股
清议的BLOG
渣打银行
炒股互动学习论坛
理柏
申银万国
第一理财网
网易财经
股票基金知识普及网
股票知识网
股票网
花旗银行
财经博客
酷基金网
金融界
银华投资
黄金之星
生活工作站点
21世纪人才网
51job
IT公司速查手册
丁丁地图
上海住房公积金网
上海劳动保障服务网
上海房产网
上海招聘网
上海本地宝
上海楼市论坛
上海热线
中华英才网
中国IT人才网
中国外企人才网
中国软件网
列车时刻表
外企人才网
天涯
客齐集
我的共享空间
我的网摘
找工作
招聘汇
搜房网
无忧生活网
智联招聘网
海归网
火车票网
焦点房地产
硕博之窗
网上房地产
优秀个人博客链接
baggio785的专栏
ccsdba
chensheng913
crybaby的博客
daxing
ezdevelop
gehantao
JAVA夜未眠
Johnny的专栏
kevinliuu的专栏
lip009的专栏
Liu
redboy2008
shendl
srx的专栏
tiger
不胜人生一场醉
分享java快乐
小P的专栏
技术研究
沉淀思想
海东的技术资料
笑看人参
优秀技术网站链接
bbs-javascript
blogjava
chinabs
chinaitlab
javaeye
javaresearch
javascript
javathinker
jspcn
Matrix
mldn
mldn
open-open
phpcat
Sun china club
UMLCHINA
免费java资源
八教123
编程论坛
赛迪网JAVA频道
软件开发教程网
资料下载站点
AJAX
Apache Software Foundation
codechina
CSS/JS/HTML教程
eclipse插件资料中心
EJB3 Persistence开发手册
FTP站点
hibernate3中文文档
hibernate技术专题
Html教程
lupaworld
PCLIB
Spring Framework 开发参考手册
Struts
verycd
中国无忧下载网
中国源码下载站
中国盟动力源码网
中国站长下载
中文方案文档站
多特软件站
得益网
源码之家
西安信息资源网
存档
2008年08月(4)
2008年07月(5)
2008年06月(1)
2008年05月(12)
2008年04月(10)
2008年03月(37)
2008年02月(11)
2008年01月(20)
2007年12月(13)
2007年11月(6)
2007年10月(19)
2007年09月(32)
2007年08月(6)
2007年07月(43)
2007年06月(13)
2007年05月(15)
2007年04月(20)
2007年03月(27)
2007年02月(1)
2007年01月(2)
2006年12月(27)
2006年11月(56)
2006年10月(41)
2006年09月(19)
软件项目交易
订阅我的博客
 









  项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用 收藏
新一篇: 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)  | 旧一篇:  把数据库库表记录转成XML
最近在项目开发中用到了jsp上传和下载的功能,所以在网上找了些资料,加上自己的一些总结,希望对大家有所帮助!

一.JspSmart之upload组件源码及使用

JspSmart之upload组件源码主要包括了5个java文件

File.java



Files.java

import java.io.IOException;
import java.util.*;

// Referenced classes of package com.jspsmart.upload:
// File, SmartUpload

public class Files
{

private SmartUpload m_parent;
private Hashtable m_files;
private int m_counter;

Files()
{
m_files = new Hashtable();
m_counter = 0;
}

protected void addFile(File file)
{
if(file == null)
{
throw new IllegalArgumentException("newFile cannot be null.");
} else
{
m_files.put(new Integer(m_counter), file);
m_counter++;
return;
}
}

public File getFile(int i)
{
if(i < 0)
throw new IllegalArgumentException("File's index cannot be a negative value (1210).");
File file = (File)m_files.get(new Integer(i));
if(file == null)
throw new IllegalArgumentException("Files' name is invalid or does not exist (1205).");
else
return file;
}

public int getCount()
{
return m_counter;
}

public long getSize()
throws IOException
{
long l = 0L;
for(int i = 0; i < m_counter; i++)
l += getFile(i).getSize();

return l;
}

public Collection getCollection()
{
return m_files.values();
}

public Enumeration getEnumeration()
{
return m_files.elements();
}
}


Request.java

import java.util.Enumeration;
import java.util.Hashtable;

public class Request
{

private Hashtable m_parameters;
private int m_counter;

Request()
{
m_parameters = new Hashtable();
m_counter = 0;
}

protected void putParameter(String s, String s1)
{
if(s == null)
throw new IllegalArgumentException("The name of an element cannot be null.");
if(m_parameters.containsKey(s))
{
Hashtable hashtable = (Hashtable)m_parameters.get(s);
hashtable.put(new Integer(hashtable.size()), s1);
} else
{
Hashtable hashtable1 = new Hashtable();
hashtable1.put(new Integer(0), s1);
m_parameters.put(s, hashtable1);
m_counter++;
}
}

public String getParameter(String s)
{
if(s == null)
throw new IllegalArgumentException("Form's name is invalid or does not exist (1305).");
Hashtable hashtable = (Hashtable)m_parameters.get(s);
if(hashtable == null)
return null;
else
return (String)hashtable.get(new Integer(0));
}

public Enumeration getParameterNames()
{
return m_parameters.keys();
}

public String[] getParameterValues(String s)
{
if(s == null)
throw new IllegalArgumentException("Form's name is invalid or does not exist (1305).");
Hashtable hashtable = (Hashtable)m_parameters.get(s);
if(hashtable == null)
return null;
String as[] = new String[hashtable.size()];
for(int i = 0; i < hashtable.size(); i++)
as[i] = (String)hashtable.get(new Integer(i));

return as;
}
}


SmartUploadException .java

public class SmartUploadException extends Exception
{

SmartUploadException(String s)
{
super(s);
}
}


SmartUpload.java

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

// Referenced classes of package com.jspsmart.upload:
// Files, Request, SmartUploadException, File

public class SmartUpload
{

protected byte m_binArray[];
protected HttpServletRequest m_request;
protected HttpServletResponse m_response;
protected ServletContext m_application;
private int m_totalBytes;
private int m_currentIndex;
private int m_startData;
private int m_endData;
private String m_boundary;
private long m_totalMaxFileSize;
private long m_maxFileSize;
private Vector m_deniedFilesList;
private Vector m_allowedFilesList;
private boolean m_denyPhysicalPath;
private boolean m_forcePhysicalPath;
private String m_contentDisposition;
public static final int SAVE_AUTO = 0;
public static final int SAVE_VIRTUAL = 1;
public static final int SAVE_PHYSICAL = 2;
private Files m_files;
private Request m_formRequest;

public SmartUpload()
{
m_totalBytes = 0;
m_currentIndex = 0;
m_startData = 0;
m_endData = 0;
m_boundary = new String();
m_totalMaxFileSize = 0L;
m_maxFileSize = 0L;
m_deniedFilesList = new Vector();
m_allowedFilesList = new Vector();
m_denyPhysicalPath = false;
m_forcePhysicalPath = false;
m_contentDisposition = new String();
m_files = new Files();
m_formRequest = new Request();
}

public final void init(ServletConfig servletconfig)
throws ServletException
{
m_application = servletconfig.getServletContext();
}

public void service(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
m_request = httpservletrequest;
m_response = httpservletresponse;
}

public final void initialize(ServletConfig servletconfig, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException
{
m_application = servletconfig.getServletContext();
m_request = httpservletrequest;
m_response = httpservletresponse;
}

public final void initialize(PageContext pagecontext)
throws ServletException
{
m_application = pagecontext.getServletContext();
m_request = (HttpServletRequest)pagecontext.getRequest();
m_response = (HttpServletResponse)pagecontext.getResponse();
}

public final void initialize(ServletContext servletcontext, HttpSession httpsession, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse, JspWriter jspwriter)
throws ServletException
{
m_application = servletcontext;
m_request = httpservletrequest;
m_response = httpservletresponse;
}

public void upload()
throws ServletException, IOException, SmartUploadException
{
int i = 0;
boolean flag = false;
long l = 0L;
boolean flag1 = false;
String s = new String();
String s2 = new String();
String s4 = new String();
String s5 = new String();
String s6 = new String();
String s7 = new String();
String s8 = new String();
String s9 = new String();
String s10 = new String();
boolean flag2 = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
int j;
for(; i < m_totalBytes; i += j)
try
{
m_request.getInputStream();
j = m_request.getInputStream().read(m_binArray, i, m_totalBytes - i);
}
catch(Exception exception)
{
throw new SmartUploadException("Unable to upload.");
}

for(; !flag1 && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
flag1 = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];

if(m_currentIndex == 1)
return;
for(m_currentIndex++; m_currentIndex < m_totalBytes; m_currentIndex = m_currentIndex + 2)
{
String s1 = getDataHeader();
m_currentIndex = m_currentIndex + 2;
boolean flag3 = s1.indexOf("filename") > 0;
String s3 = getDataFieldValue(s1, "name");
if(flag3)
{
s6 = getDataFieldValue(s1, "filename");
s4 = getFileName(s6);
s5 = getFileExt(s4);
s7 = getContentType(s1);
s8 = getContentDisp(s1);
s9 = getTypeMIME(s7);
s10 = getSubTypeMIME(s7);
}
getDataSection();
if(flag3 && s4.length() > 0)
{
if(m_deniedFilesList.contains(s5))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(s5))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > 0L && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException("Size exceeded for this file : " + s4 + " (1105).");
l += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > 0L && l > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(flag3)
{
com.facet.jspsmart.upload.File file = new com.facet.jspsmart.upload.File();
file.setParent(this);
file.setFieldName(s3);
file.setFileName(s4);
file.setFileExt(s5);
file.setFilePathName(s6);
file.setIsMissing(s6.length() == 0);
file.setContentType(s7);
file.setContentDisp(s8);
file.setTypeMIME(s9);
file.setSubTypeMIME(s10);
if(s7.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
file.setSize((m_endData - m_startData) + 1);
file.setStartData(m_startData);
file.setEndData(m_endData);
m_files.addFile(file);
} else
{
String s11 = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
m_formRequest.putParameter(s3, s11);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
}

}

public int save(String s)
throws ServletException, IOException, SmartUploadException
{
return save(s, 0);
}

public int save(String s, int i)
throws ServletException, IOException, SmartUploadException
{
int j = 0;
if(s == null)
s = m_application.getRealPath("/");
if(s.indexOf("/") != -1)
{
if(s.charAt(s.length() - 1) != '/')
s = s + "/";
} else
if(s.charAt(s.length() - 1) != '\\')
s = s + "\\";
for(int k = 0; k < m_files.getCount(); k++)
if(!m_files.getFile(k).isMissing())
{
m_files.getFile(k).saveAs(s + m_files.getFile(k).getFileName(), i);
j++;
}

return j;
}

public int getSize()
{
return m_totalBytes;
}

public byte getBinaryData(int i)
{
byte byte0;
try
{
byte0 = m_binArray[i];
}
catch(Exception exception)
{
throw new ArrayIndexOutOfBoundsException("Index out of range (1005).");
}
return byte0;
}

public Files getFiles()
{
return m_files;
}

public Request getRequest()
{
return m_formRequest;
}

public void downloadFile(String s)
throws ServletException, IOException, SmartUploadException
{
downloadFile(s, null, null);
}

public void downloadFile(String s, String s1)
throws ServletException, IOException, SmartUploadException, SmartUploadException
{
downloadFile(s, s1, null);
}

public void downloadFile(String s, String s1, String s2)
throws ServletException, IOException, SmartUploadException
{
downloadFile(s, s1, s2, 65000);
}

public void downloadFile(String s, String s1, String s2, int i)
throws ServletException, IOException, SmartUploadException
{
if(s == null)
throw new IllegalArgumentException("File '" + s + "' not found (1040).");
if(s.equals(""))
throw new IllegalArgumentException("File '" + s + "' not found (1040).");
if(!isVirtual(s) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
if(isVirtual(s))
s = m_application.getRealPath(s);
java.io.File file = new java.io.File(s);
FileInputStream fileinputstream = new FileInputStream(file);
long l = file.length();
boolean flag = false;
int k = 0;
byte abyte0[] = new byte[i];
if(s1 == null)
m_response.setContentType("application/x-msdownload");
else
if(s1.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(s1);
m_response.setContentLength((int)l);
m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
if(s2 == null)
m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + getFileName(s));
else
if(s2.length() == 0)
m_response.setHeader("Content-Disposition", m_contentDisposition);
else
m_response.setHeader("Content-Disposition", m_contentDisposition + " filename=" + s2);
while((long)k < l)
{
int j = fileinputstream.read(abyte0, 0, i);
k += j;
m_response.getOutputStream().write(abyte0, 0, j);
}
fileinputstream.close();
}

public void downloadField(ResultSet resultset, String s, String s1, String s2)
throws ServletException, IOException, SQLException
{
if(resultset == null)
throw new IllegalArgumentException("The RecordSet cannot be null (1045).");
if(s == null)
throw new IllegalArgumentException("The columnName cannot be null (1050).");
if(s.length() == 0)
throw new IllegalArgumentException("The columnName cannot be empty (1055).");
byte abyte0[] = resultset.getBytes(s);
if(s1 == null)
m_response.setContentType("application/x-msdownload");
else
if(s1.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(s1);
m_response.setContentLength(abyte0.length);
if(s2 == null)
m_response.setHeader("Content-Disposition", "attachment;");
else
if(s2.length() == 0)
m_response.setHeader("Content-Disposition", "attachment;");
else
m_response.setHeader("Content-Disposition", "attachment; filename=" + s2);
m_response.getOutputStream().write(abyte0, 0, abyte0.length);
}

public void fieldToFile(ResultSet resultset, String s, String s1)
throws ServletException, IOException, SmartUploadException, SQLException
{
try
{
if(m_application.getRealPath(s1) != null)
s1 = m_application.getRealPath(s1);
InputStream inputstream = resultset.getBinaryStream(s);
FileOutputStream fileoutputstream = new FileOutputStream(s1);
int i;
while((i = inputstream.read()) != -1)
fileoutputstream.write(i);
fileoutputstream.close();
}
catch(Exception exception)
{
throw new SmartUploadException("Unable to save file from the DataBase (1020).");
}
}

private String getDataFieldValue(String s, String s1)
{
String s2 = new String();
String s3 = new String();
int i = 0;
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
s2 = s1 + "=" + '"';
i = s.indexOf(s2);
if(i > 0)
{
int j = i + s2.length();
int k = j;
s2 = "\"";
int l = s.indexOf(s2, j);
if(k > 0 && l > 0)
s3 = s.substring(k, l);
}
return s3;
}

private String getFileExt(String s)
{
String s1 = new String();
int i = 0;
int j = 0;
if(s == null)
return null;
i = s.lastIndexOf(46) + 1;
j = s.length();
s1 = s.substring(i, j);
if(s.lastIndexOf(46) > 0)
return s1;
else
return "";
}

private String getContentType(String s)
{
String s1 = new String();
String s2 = new String();
int i = 0;
boolean flag = false;
s1 = "Content-Type:";
i = s.indexOf(s1) + s1.length();
if(i != -1)
{
int j = s.length();
s2 = s.substring(i, j);
}
return s2;
}

private String getTypeMIME(String s)
{
String s1 = new String();
int i = 0;
i = s.indexOf("/");
if(i != -1)
return s.substring(1, i);
else
return s;
}

private String getSubTypeMIME(String s)
{
String s1 = new String();
int i = 0;
boolean flag = false;
i = s.indexOf("/") + 1;
if(i != -1)
{
int j = s.length();
return s.substring(i, j);
} else
{
return s;
}
}

private String getContentDisp(String s)
{
String s1 = new String();
int i = 0;
int j = 0;
i = s.indexOf(":") + 1;
j = s.indexOf(";");
s1 = s.substring(i, j);
return s1;
}

private void getDataSection()
{
boolean flag = false;
String s = new String();
int i = m_currentIndex;
int j = 0;
int k = m_boundary.length();
m_startData = m_currentIndex;
m_endData = 0;
while(i < m_totalBytes)
if(m_binArray[i] == (byte)m_boundary.charAt(j))
{
if(j == k - 1)
{
m_endData = ((i - k) + 1) - 3;
break;
}
i++;
j++;
} else
{
i++;
j = 0;
}
m_currentIndex = m_endData + k + 3;
}

private String getDataHeader()
{
int i = m_currentIndex;
int j = 0;
boolean flag = false;
for(boolean flag1 = false; !flag1;)
if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13)
{
flag1 = true;
j = m_currentIndex - 1;
m_currentIndex = m_currentIndex + 2;
} else
{
m_currentIndex++;
}

String s = new String(m_binArray, i, (j - i) + 1);
return s;
}

private String getFileName(String s)
{
String s1 = new String();
String s2 = new String();
int i = 0;
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
i = s.lastIndexOf(47);
if(i != -1)
return s.substring(i + 1, s.length());
i = s.lastIndexOf(92);
if(i != -1)
return s.substring(i + 1, s.length());
else
return s;
}

public void setDeniedFilesList(String s)
throws ServletException, IOException, SQLException
{
String s1 = "";
if(s != null)
{
String s2 = "";
for(int i = 0; i < s.length(); i++)
if(s.charAt(i) == ',')
{
if(!m_deniedFilesList.contains(s2))
m_deniedFilesList.addElement(s2);
s2 = "";
} else
{
s2 = s2 + s.charAt(i);
}

if(s2 != "")
m_deniedFilesList.addElement(s2);
} else
{
m_deniedFilesList = null;
}
}

public void setAllowedFilesList(String s)
{
String s1 = "";
if(s != null)
{
String s2 = "";
for(int i = 0; i < s.length(); i++)
if(s.charAt(i) == ',')
{
if(!m_allowedFilesList.contains(s2))
m_allowedFilesList.addElement(s2);
s2 = "";
} else
{
s2 = s2 + s.charAt(i);
}

if(s2 != "")
m_allowedFilesList.addElement(s2);
} else
{
m_allowedFilesList = null;
}
}

public void setDenyPhysicalPath(boolean flag)
{
m_denyPhysicalPath = flag;
}

public void setForcePhysicalPath(boolean flag)
{
m_forcePhysicalPath = flag;
}

public void setContentDisposition(String s)
{
m_contentDisposition = s;
}

public void setTotalMaxFileSize(long l)
{
m_totalMaxFileSize = l;
}

public void setMaxFileSize(long l)
{
m_maxFileSize = l;
}

protected String getPhysicalPath(String s, int i)
throws IOException
{
String s1 = new String();
String s2 = new String();
String s3 = new String();
boolean flag = false;
s3 = System.getProperty("file.separator");
if(s == null)
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(s.equals(""))
throw new IllegalArgumentException("There is no specified destination file (1140).");
if(s.lastIndexOf("\\") >= 0)
{
s1 = s.substring(0, s.lastIndexOf("\\"));
s2 = s.substring(s.lastIndexOf("\\") + 1);
}
if(s.lastIndexOf("/") >= 0)
{
s1 = s.substring(0, s.lastIndexOf("/"));
s2 = s.substring(s.lastIndexOf("/") + 1);
}
s1 = s1.length() != 0 ? s1 : "/";
java.io.File file = new java.io.File(s1);
if(file.exists())
flag = true;
if(i == 0)
{
if(isVirtual(s1))
{
s1 = m_application.getRealPath(s1);
if(s1.endsWith(s3))
s1 = s1 + s2;
else
s1 = s1 + s3 + s2;
return s1;
}
if(flag)
{
if(m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
else
return s;
} else
{
throw new IllegalArgumentException("This path does not exist (1135).");
}
}
if(i == 1)
{
if(isVirtual(s1))
{
s1 = m_application.getRealPath(s1);
if(s1.endsWith(s3))
s1 = s1 + s2;
else
s1 = s1 + s3 + s2;
return s1;
}
if(flag)
throw new IllegalArgumentException("The path is not a virtual path.");
else
throw new IllegalArgumentException("This path does not exist (1135).");
}
if(i == 2)
{
if(flag)
if(m_denyPhysicalPath)
throw new IllegalArgumentException("Physical path is denied (1125).");
else
return s;
if(isVirtual(s1))
throw new IllegalArgumentException("The path is not a physical path.");
else
throw new IllegalArgumentException("This path does not exist (1135).");
} else
{
return null;
}
}

public void uploadInFile(String s)
throws IOException, SmartUploadException
{
int i = 0;
int j = 0;
boolean flag = false;
if(s == null)
throw new IllegalArgumentException("There is no specified destination file (1025).");
if(s.length() == 0)
throw new IllegalArgumentException("There is no specified destination file (1025).");
if(!isVirtual(s) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
i = m_request.getContentLength();
m_binArray = new byte[i];
int k;
for(; j < i; j += k)
try
{
k = m_request.getInputStream().read(m_binArray, j, i - j);
}
catch(Exception exception)
{
throw new SmartUploadException("Unable to upload.");
}

if(isVirtual(s))
s = m_application.getRealPath(s);
try
{
java.io.File file = new java.io.File(s);
FileOutputStream fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(m_binArray);
fileoutputstream.close();
}
catch(Exception exception1)
{
throw new SmartUploadException("The Form cannot be saved in the specified file (1030).");
}
}

private boolean isVirtual(String s)
{
if(m_application.getRealPath(s) != null)
{
java.io.File file = new java.io.File(m_application.getRealPath(s));
return file.exists();
} else
{
return false;
}
}
}


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



import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;

// Referenced classes of package com.jspsmart.upload:
// SmartUploadException, SmartUpload

public class File
{

private SmartUpload m_parent;
private int m_startData;
private int m_endData;
private int m_size;
private String m_fieldname;
private String m_filename;
private String m_fileExt;
private String m_filePathName;
private String m_contentType;
private String m_contentDisp;
private String m_typeMime;
private String m_subTypeMime;
private String m_contentString;
private boolean m_isMissing;
public static final int SAVEAS_AUTO = 0;
public static final int SAVEAS_VIRTUAL = 1;
public static final int SAVEAS_PHYSICAL = 2;

File()
{
m_startData = 0;
m_endData = 0;
m_size = 0;
m_fieldname = new String();
m_filename = new String();
m_fileExt = new String();
m_filePathName = new String();
m_contentType = new String();
m_contentDisp = new String();
m_typeMime = new String();
m_subTypeMime = new String();
m_contentString = new String();
m_isMissing = true;
}

public void saveAs(String s)
throws IOException, SmartUploadException
{
saveAs(s, 0);
}

public void saveAs(String s, int i)
throws IOException, SmartUploadException
{
String s1 = new String();
s1 = m_parent.getPhysicalPath(s, i);
if(s1 == null)
throw new IllegalArgumentException("There is no specified destination file (1140).");
try
{
java.io.File file = new java.io.File(s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(m_parent.m_binArray, m_startData, m_size);
fileoutputstream.close();
}
catch(IOException ioexception)
{
throw new SmartUploadException("File can't be saved (1120).");
}
}

public void fileToField(ResultSet resultset, String s)
throws ServletException, IOException, SmartUploadException, SQLException
{
long l = 0L;
int i = 0x10000;
int j = 0;
int k = m_startData;
if(resultset == null)
throw new IllegalArgumentException("The RecordSet cannot be null (1145).");
if(s == null)
throw new IllegalArgumentException("The columnName cannot be null (1150).");
if(s.length() == 0)
throw new IllegalArgumentException("The columnName cannot be empty (1155).");
l = BigInteger.valueOf(m_size).divide(BigInteger.valueOf(i)).longValue();
j = BigInteger.valueOf(m_size).mod(BigInteger.valueOf(i)).intValue();
try
{
for(int i1 = 1; (long)i1 < l; i1++)
{
resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, i), i);
k = k != 0 ? k : 1;
k = i1 * i + m_startData;
}

if(j > 0)
resultset.updateBinaryStream(s, new ByteArrayInputStream(m_parent.m_binArray, k, j), j);
}
catch(SQLException sqlexception)
{
byte abyte0[] = new byte[m_size];
System.arraycopy(m_parent.m_binArray, m_startData, abyte0, 0, m_size);
resultset.updateBytes(s, abyte0);
}
catch(Exception exception)
{
throw new SmartUploadException("Unable to save file in the DataBase (1130).");
}
}

public boolean isMissing()
{
return m_isMissing;
}

public String getFieldName()
{
return m_fieldname;
}

public String getFileName()
{
return m_filename;
}

public String getFilePathName()
{
return m_filePathName;
}

public String getFileExt()
{
return m_fileExt;
}

public String getContentType()
{
return m_contentType;
}

public String getContentDisp()
{
return m_contentDisp;
}

public String getContentString()
{
String s = new String(m_parent.m_binArray, m_startData, m_size);
return s;
}

public String getTypeMIME()
throws IOException
{
return m_typeMime;
}

public String getSubTypeMIME()
{
return m_subTypeMime;
}

public int getSize()
{
return m_size;
}

protected int getStartData()
{
return m_startData;
}

protected int getEndData()
{
return m_endData;
}

protected void setParent(SmartUpload smartupload)
{
m_parent = smartupload;
}

protected void setStartData(int i)
{
m_startData = i;
}

protected void setEndData(int i)
{
m_endData = i;
}

protected void setSize(int i)
{
m_size = i;
}

protected void setIsMissing(boolean flag)
{
m_isMissing = flag;
}

protected void setFieldName(String s)
{
m_fieldname = s;
}

protected void setFileName(String s)
{
m_filename = s;
}

protected void setFilePathName(String s)
{
m_filePathName = s;
}

protected void setFileExt(String s)
{
m_fileExt = s;
}

protected void setContentType(String s)
{
m_contentType = s;
}

protected void setContentDisp(String s)
{
m_contentDisp = s;
}

protected void setTypeMIME(String s)
{
m_typeMime = s;
}

protected void setSubTypeMIME(String s)
{
m_subTypeMime = s;
}

public byte getBinaryData(int i)
{
if(m_startData + i > m_endData)
throw new ArrayIndexOutOfBoundsException("Index Out of range (1115).");
if(m_startData + i <= m_endData)
return m_parent.m_binArray[m_startData + i];
else
return 0;
}
}

发表于 @ 2006年11月18日 20:07:00|评论(4)|编辑

新一篇: 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)  | 旧一篇:  把数据库库表记录转成XML评论
#shaoshao 发表于2007-08-22 14:29:32  IP: 124.230.47.*
楼主,你好!看到你这几篇文章真是太高兴了,你能发给我一份 JspSmartupload组件的源代码吗?我的邮箱是:qiaochu1013@tom.com
非常谢谢,很期待你的回复啊!#shaoshao 发表于2007-08-22 17:13:50  IP: 124.230.35.*
楼主,
你有没有发现下载text文件会出问题啊?
我出这样的错误
getOutputStream() has already been called for this response
不知道你有没有碰到?#shaoshao 发表于2007-08-22 17:27:51  IP: 124.230.35.*
我发现了 不是text文件格式的问题 是有些text文件可以下载 有些text文件下载就出错 excel文件也是一样
都是这个错误getOutputStream() has already been called for this response
是什么原因呢?#daming 发表于2007-12-30 22:17:28  IP: 221.11.20.*
分享到:
评论
1 楼 yyh84yangtao 2008-09-23  
楼主,你好!看到你这几篇文章真是太高兴了,你能发给我一份 JspSmartupload.zip吗?我的邮箱是yangyonghui_y@163.com,谢谢!

相关推荐

Global site tag (gtag.js) - Google Analytics