jspsmartupload 上传示例

来源:互联网 发布:单片机开发板哪个好 编辑:程序博客网 时间:2024/05/21 11:22
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub// 设置编码格式request.setCharacterEncoding("GB2312");response.setCharacterEncoding("GB2312");// 第一步:新建一个对象SmartUpload smart = new SmartUpload();// 第二步:初始化smart.initialize(super.getServletConfig(), request, response);// 第三步:设置上传文件的类型smart.setAllowedFilesList("jpg,png");try {smart.setDeniedFilesList("bat");smart.setMaxFileSize(10*1024*1024);} catch (SQLException e1) {e1.printStackTrace();}String name = null;try {smart.upload();Files fs = smart.getFiles();// 得到所有的文件for (int i = 0; i < fs.getCount(); i++) {// 对文件个数进行循环File f = fs.getFile(i);name = System.currentTimeMillis() + f.getFileName();if (f.isMissing() == false) {String path = request.getContextPath()+"/file/image/" + name;//String path ="/yjdata/www/www/ROOT/file/image/" + name;System.out.println("basePath="+path);f.saveAs(path);}}} catch (Exception e) {e.printStackTrace();}int id  =Integer.parseInt(request.getParameter("id")) ;if(AdminDAO.addimg(id, name)==1){System.out.println("上传成功");}}
页面引用图片
  <img height="130px" width="160px"  src= "<%=path%>/file/image/${apage.imgname}" alt="通用的占位符缩略图">

0 0
原创粉丝点击