jimingminlovefly

统计

最新评论

案例-jspSmartUpload上传下载2

 注意:在struts2项目中只能用自身的上传功能,该jsp上传功能不行

 <form name ="Form1" enctype="multipart/form-data" method="post" action="jspsmart.jsp">
<p>上传文件1:<input type ="file" name ="File1" size="20" maxlength="20"/></p>
<input type ="submit" value="上传"/>
<input type ="reset" value="清除"/>
</form>

 

jspsmart.jsp中代码:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<h2>文件上传规范-jspSmart</h2>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
//计算文件上传个数
int count=0;

//SmartUpload的初始化,使用这个jspsmart一定要在一开始就这样声明
mySmartUpload.initialize(pageContext);

//声明限制上传的文件的大小5MB
mySmartUpload.setMaxFileSize(
5*1024*1024);

//依据form的内容上传
mySmartUpload.upload();

//将上传的文件一个一个取出来处理
for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
//取出一个文件
com.jspsmart.upload.File myFile
= mySmartUpload.getFiles().getFile(i);

//如果文件存在,则做存档操作
if (!myFile.isMissing()){
//将文件存放在绝对路径的位置
myFile.saveAs(
"C:\\upload\\"+myFile.getFileName(),mySmartUpload.SAVE_PHYSICAL);

//显示此上传文件的详细信息
out.println(
"FieldName="+myFile.getFieldName()+"<BR>");
out.println(
"Size="+myFile.getSize()+"<BR>");
out.println(
"FileName="+myFile.getFileName()+"<BR>");
out.println(
"FileExt="+myFile.getFileExt()+"<BR>");
out.println(
"FilePathName="+myFile.getFilePathName()+"<BR>");
out.println(
"ContentType="+myFile.getContentType()+"<BR>");
out.println(
"ContentDisp="+myFile.getContentDisp()+"<BR>");
out.println(
"TypeMIME="+myFile.getTypeMIME()+"<BR>");
out.println(
"SubTypeMIME="+myFile.getSubTypeMIME()+"<BR>");
count
++;
}
}

//显示应该上产的文件数目
out.println(
"<BR>"+mySmartUpload.getFiles().getCount()+"file could be uploaded.<BR>");

//显示成功上传的文件数目
out.println(count
+"file(s) upload.");
%>
</body>
</html>

 

downlload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<h2>文件下载</h2>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
//SmartUpload的初始化
mySmartUpload.initialize(pageContext);

//必须如此声明,否则将会把文件显示与浏览器中
mySmartUpload.setContentDisposition(
"inline;");

//将sample.zip下载 下载默认名称为download.zip
mySmartUpload.downloadFile(
"C:\\upload\\aa.txt","application/x-zip-compressed","downloaded.zip");
%>
</body>
</html>

posted on 2012-02-03 11:14 计明敏 阅读(486) 评论(0)  编辑  收藏 所属分类: jsp/servlet


只有注册用户登录后才能发表评论。


网站导航: