使用SmartUpload的步骤

第一步:先导入包
第二步:

<form action = "<%=basePath%>/tb_addcommodityServlet" method="post" enctype="multipart/form-data"  >  //在jsp页面的    //在jsp页面的
<input type="file" name ="file"/>//这里的name必带不然获取不到
</from>


第三步:在Servlet 中先实例化

SmartUpload smart = new SmartUpload();


第四步:设置smartUpload的格式  

 smart.setCharset("utf-8");


第五步:初始化smartUpload的属性

smart.initialize(this.getServletConfig(), req, resp);


第六步:上传SmartUpload

try {
smart.upload();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


第七步:要想获得参数

String title = smart.getRequest().getParameter("title");   //标题名称


 

获得图片:
// smart.setTotalMaxFileSize(60*1024*1024);//设置总上传数据的最大长度
// smart.setMaxFileSize(4*1024*1024);//设置每个上传文件数据的最大长度
// smart.setAllowedFilesList("jpg,gif,png,jpeg");//设置允许上传文件的格式
// try {
// // smart.setDeniedFilesList("exe,bat,jsp,html,htm");   //设置禁止上传的文件
// smart.upload();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// System.out.println("上传失败........");
// }
// Files files = smart.getFiles();
// System.out.println(files.getCount()+"files.getCount()");
// for(int f=0; f <files.getCount();f++){
//   UUID uu = UUID.randomUUID();  //获得随机数
//   String uuid = uu.toString(); //将随机数转为String类型
// File file2 = files.getFile(f); //此处的file是com.jspsmart.upload包内的File.class
// //得到图片名字
// name = file2.getFileName();
// //得到拼接的名字
// String filename = uuid+new Date().getTime()+"."+file2.getFileExt();
// if(!file2.isMissing()){
// filepath = "D:\\Testimage\\"+filename; //保存的路径
// Tb_Wares tw = new Tb_Wares();     //实例化我的对象
// tw.setTb_addcommodity(addcommodity); //添加我的对象属性
// tw.setWares_artworkfront(filepath); //添加我的对象属性
// twb.AddImage(tw); //添加我的对象属性
// try {
// file2.saveAs(filepath,smart.SAVE_PHYSICAL); //上传到我的本地地址
//file2.saveAs(filepath,smart.SAVE_VIRTUAL); //上传到服务器
// } catch (SmartUploadException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } 
// }else { //为了应对上传的多个文件中出现空的情况 
// continue;//跳出当前循环一次 
// } 
// }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值