smartupload 的Readme.docx
《smartupload 的Readme.docx》由会员分享,可在线阅读,更多相关《smartupload 的Readme.docx(24页珍藏版)》请在冰豆网上搜索。
smartupload的Readme
图片及其他信息一起上传详解
在写代码之前,先准备文件上传组件,即MyjspSmartUpload.jar或jsmartcom_zh_CN.jar这两个jar包随便一个都行。
那我就从上传页面来说吧,即smartUpload.jsp页面。
页面是使用一个表单进行提交信息的,在表单中有一行代码
在这里面有enctype="multipart/form-data"这句代码是用来上传文件到服务器的,如果没有这句代码的话,就会默认是enctype=”application/x-www-form-urlencoded”
以上是设置表单的MIME编码的。
详细介绍见:
注:
上传页面的编码应为pageEncoding="GB18030"其中GB18030为国家标准编码,若改为GB2312、GBK或UTF-8时,传递参数时为空或为乱码。
在此页面head标签里给于javascript代码对上传表单参数进行判断。
在上传处理页面uploadimage.jsp中,其编码也是GB18030,jar包中,com.jspsmart.upload包中有一个SmartUpload.class文件,当然还用到其他两个.class文件,即是File.class和Files.class
在此页面中首先定义一个SmartUpload对象为mySmartUpload,
即是:
SmartUploadmySmartUpload=newSmartUpload();
以下java代码是处理上传图片到服务器的及生成缩略图代码。
<%
SmartUploadmySmartUpload=newSmartUpload();
//response.setContentType("text/html;charset=utf-8");
longfile_size_max=4000000;//图片最大尺寸接近3.8MB,如果需要的话,可以对其数字进行修改!
StringfileName2="",ext="",testvar="";
Stringurl="images/";//应保证在根目录中有此(images)目录的存在Stringurl="uploadfile/images/";
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try{
mySmartUpload.setAllowedFilesList("jpg,gif,jpeg,png,bmp,JPG,JPEG,GIF,PNG,BMP");
//上载文件
mySmartUpload.upload();
}catch(Exceptione){
%>
alert("只允许上传jpg、jpeg、gif、png、bmp类型图片文件");
window.location='/project/admin/smartUpload.jsp';
<%
}
try{
com.jspsmart.upload.FilemyFile=mySmartUpload.getFiles().getFile(0);
if(myFile.isMissing()){%>
alert("请先选择要上传的文件");
window.location='/project/admin/smartUpload.jsp';
<%}
else{
StringmyFileName=myFile.getFileName();//取得上载的文件的文件名
ext=myFile.getFileExt();//取得后缀名
intfile_size=myFile.getSize();//取得文件的大小
Stringsaveurl="";
if(file_size>file_size_max){
out.print("");
out.print("alert('上传文件大小不能超过"+(file_size_max/1000)+"K');");
out.print("window.location='/project/admin/smartUpload.jsp';");
out.print("");
}
else{
//更改文件名,取得当前上传时间的毫秒数值
Calendarcalendar=Calendar.getInstance();
Stringfilename=String.valueOf(calendar.getTimeInMillis());
saveurl=request.getRealPath("/")+url;
//request.getRealPath()方法现在已经过时,但任可以用的,目前推荐使用下面的方法:
request.getSession().getServletContext().getRealPath("");
saveurl+=myFileName;//保存路径saveurl+=myFileName+"."+ext;
myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
System.out.println("图片名称:
"+myFileName);
//-----------------------上传完成,开始生成缩略图-------------------------
java.io.Filefile=newjava.io.File(saveurl);
//读入刚才上传的文件
Stringnewurl=request.getRealPath("/")+url+filename+"_mini."+ext;
//新的缩略图保存地址
Imagesrc=javax.imageio.ImageIO.read(file);//构造Image对象
floattagsize=200;
intold_w=src.getWidth(null);//得到源图宽
intold_h=src.getHeight(null);//得到源图长
intnew_w=0;//定义缩略图的新宽度
intnew_h=0;//定义缩略图的新宽度
inttempsize;
floattempdouble;
if(old_w>old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
new_w=Math.round(old_w/tempdouble);
new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImagetag=newBufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);//绘制缩小后的图
FileOutputStreamnewimage=newFileOutputStream(newurl);//输出到文件流
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag);//近JPEG编码模式
newimage.close();
在接受表单里的其它参数时,应放在文件上传代码之后,在接受参数时,应用到SmartUpload类中的getRequest()方法,例如接收“商品编号”参数时应是:
StringgoodsID=mySmartUpload.getRequest().getParameter("goodsID");//商品编号
goodsID=newString(goodsID.getBytes(),"GBK");
System.out.println("商品编号:
"+goodsID);
其中将得到的参数进行转码,即转为GBK(GBK编码支持简体中文),接收到参数之后,将数据插入到数据库中,这其中goodsPicture的值为myFileName(即上传文件的文件名)。
商品的销售量(goodsSaleNum)默认值为零,可对其修改。
下面附上上传文件的全部代码:
smartUpload.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+":
//"+request.getServerName()+":
"+request.getServerPort()+path+"/";
%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
">
MyJSP'smartUpload.jsp'startingpage
--
-->
--
.STYLE1{font-size:
18px;
font-weight:
bold;
}
-->
functionIs_Ok(){
if(document.getElementById("goodsID").value==""||isNaN(form1.goodsID.value)){
alert("请输入产品编号且为数字!
");
document.getElementById("goodsID").focus();
returnfalse;
}
if(document.form1.goodsID.value.length>11){
alert("输入产品编号过长,应在1-11位之间!
");
document.form1.goodsID.focus();
returnfalse;
}
if(document.getElementById("cate_name").value==""){
alert("请输入产品类别!
");
document.getElementById("cate_name").focus();
returnfalse;
}
if(document.form1.file.value==""){
alert("上传图片不能为空!
");
document.form1.file.focus();
returnfalse;
}
if(document.getElementById("goodsName").value==""){
alert("请输入产品名称!
");
document.getElementById("goodsName").focus();
returnfalse;
}
if(document.getElementById("goodsRealPrice").value==""||isNaN(form1.goodsRealPrice.value)){
alert("请输入产品实时价格且为数字!
");
document.getElementById("goodsRealPrice").focus();
returnfalse;
}
if(document.form1.goodsRealPrice.value.length>5){
alert("输入产品实价过大,应在1-5位之间!
");
document.form1.goodsRealPrice.focus();
returnfalse;
}
if(document.getElementById("goodsDisPrice").value==""||isNaN(form1.goodsDisPrice.value)){
alert("请输入产品折扣价格且为数字!
");
document.getElementById("goodsDisPrice").focus();
returnfalse;
}
if(document.form1.goodsDisPrice.value.length>5){
alert("输入产品打折价过大,应在1-5位之间!
");
document.form1.goodsDisPrice.focus();
returnfalse;
}
if(document.getElementById("goodsQuantity").value==""||isNaN(form1.goodsQuantity.value)){
alert("请输入产品数量且为数字!
");
document.getElementById("goodsQuantity").focus();
returnfalse;
}
if(document.form1.goodsQuantity.value.length>5){
alert("输入产品数量过大,应在1-5位之间!
");
document.form1.goodsQuantity.focus();
returnfalse;
}
if(document.getElementById("goodsIntroduction").value==""){
alert("请输入产品介绍!
");
document.getElementById("goodsIntroduction").focus();
returnfalse;
}
if(document.getElementById("goodsFeature").value==""){
alert("请输入产品功能!
");
document.getElementById("goodsFeature").focus();
returnfalse;
}
if(document.getElementById("goodsWash").value==""){
alert("请输入洗涤方式!
");
document.getElementById("goodsWash").focus();
returnfalse;
}
if(document.getElementById("goodsFabric").value==""){
alert("请输入产品材料!
");
document.getElementById("goodsFabric").focus();
returnfalse;
}
if(document.getElementById("goodsColor").value==""){
alert("请输入产品颜色!
");
document.getElementById("goodsColor").focus();
returnfalse;
}
if(document.getElementById("goodsSize").value==""||isNaN(form1.goodsSize.value)){
alert("请输入产品尺寸且为数字!
");
document.getElementById("goodsSize").focus();
returnfalse;
}
if(document.form1.goodsSize.value.length>3){
alert("输入产品尺寸过大,应在1-3位之间!
");
document.form1.goodsSize.focus();
returnfalse;
}
}
欢迎添加新产品
产品编号:
|
产品类别:
迷你裙
半身裙
吊带裙
连衣裙
超短裙
其 他
|
选择图片:
|
产品名称:
|
产品实时价格:
|
产品折扣价格:
|
产品数量:
|
展开阅读全文
相关搜索