第一步:先导入包
第二步:
<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;//跳出当前循环一次
// }
// }