jspSmartUpload使用初步

jsp中使用SmartUpload上传文件

1、下载jspSmartUpload组件后,解压缩。把com目录复制到应用程序的WEB-INFclasses目录下。

2、尤其注意

(1)文件上传表单要添加enctype="multipart/form-data",如:

<form action="product_update_ok.jsp?p_id=<%= p_id %>" method="post" onSubmit="return check_product(this);"  enctype="multipart/form-data">

(2)对于文件上传表单,通过request对象无法获得表单项的值,必须通过jspSmartUpload组件提供的Request类来获取,如:

String p_name=su.getRequest().getParameter("p_name");

且该代码要放在文件上传代码之后。

3、主要代码

//新建上传对象
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();
// 将上传文件全部保存到指定目录,必须保证images目录在应用程序根文件夹中存在
//int count = su.save("/images", su.SAVE_VIRTUAL);
int count = su.save("/images");
String p_image_temp="";
if(count==1) //重新上传了图片
{
com.jspsmart.upload.File file = su.getFiles().getFile(0);
p_image_temp="images\"+file.getFileName();
}
else
p_image_temp=(String)session.getAttribute("p_iamge"); //没有重新上传图片

//获取表单其他信息
String p_id=su.getRequest().getParameter("p_id");
String p_type=su.getRequest().getParameter("p_type");
String p_name=su.getRequest().getParameter("p_name");
String p_price=su.getRequest().getParameter("p_price");
String p_quantity=su.getRequest().getParameter("p_quantity");
String p_description=su.getRequest().getParameter("p_description");
String p_time=String.valueOf(date.getMonth()+1)+"-"+date.getDate()+"-20"+String.valueOf(date.getYear()).substring(1);

原文地址:https://www.cnblogs.com/zhouhb/p/4692416.html