public static void main(String[] args) { String str = "abc.gif"; //例:substring(1,3):下标从0开始,截取部分包括1,不包括3! System.out.println(str.substring(1,3));//这里打印 bc }
String str = "abc.gif"; //str.lastIndexof(".") = 3;
// 登陆首页: <body> <form action="${pageContext.request.contextPath}/registerAction.do?flag=registe" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="usrname"/><br/> 密码:<input type="text" name="usrpwd"/><br/> 选择头像:<input type="file" name="usrphoto"/><br/> <input type="submit" value="注册"/><input type="reset" value="重写"/> </form> </body> // 下载: // 代码场景(如上“登陆首页”):点击某条超连接,该超连接向downloadFile()方法传递用户名, // 在该方法中根据用户名得到该用户的完整的 信息 // (用户名、头像图片的原始文件名以及对应的uuid文件名、);而后根据这些信息作下载动做。 public class DownloadAction extends DispatchAction{ public ActionForward downloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res){ res.setContentType("text/html;charset=utf-8"); String userName = req.getParameter("userName"); String tmpStr=""; try { // 从jsp获取传过来的用户名,若是是中文名的状况就必须转码 // (可是视频上好像没有作这个动做,不知道为何) tmpStr = new String(userName.getBytes("ISO8859_1"), "utf-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } Usr user = new UsrService().getUserByName(tmpStr); String oriFileName = ""; try { // 文件名是中文名的状况也必须转码! oriFileName = java.net.URLEncoder.encode(user.getOriFileName(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //设置头,告诉浏览器接下来要作下载动做:uploadfile是服务上用来存放上传文件的目录 res.setHeader("Content-Disposition", "attachment; filename="+oriFileName); String filePath = this.getServlet().getServletContext().getRealPath("/uploadfile"); String fullPath = filePath+"\\"+user.getUuidFileName(); OutputStream os = null; FileInputStream fis = null; byte[] buffer = new byte[1024]; int readLen = 0; try{ fis = new FileInputStream(fullPath); os = res.getOutputStream(); while((readLen = fis.read(buffer)) > 0){ os.write(buffer, 0, readLen); } os.flush(); }catch(IOException e){ e.printStackTrace(); return mapping.findForward("errPage"); }finally{ try{ if( fis != null ){ fis.close(); } if( os != null ){ os.close(); } }catch(IOException e){ e.printStackTrace(); } } return mapping.findForward("/usrAction"); } } // 上传: // 代码场景: // 在表单里有一个文件选择控件、用户名输入文本框。选择一个图片后, // 点击submit按钮后将该图片写到服务器指定文件夹中,并保存用户名、文件名、 // 以及文件名对应的生成的UUID到数据库中。 public class RegisterAction extends DispatchAction { public ActionForward registe(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res){ RegisterForm registerForm = ( RegisterForm )form; FormFile usrPhoto = registerForm.getUsrphoto(); String fileName = usrPhoto.getFileName(); String uuidFileName = Tools.changeToUUIDName(fileName); String savePath = this.getServlet().getServletContext().getRealPath("/uploadfile"); InputStream is = null; OutputStream os = null; try{ is = usrPhoto.getInputStream(); os = new FileOutputStream(savePath+"\\"+uuidFileName); int readLen = 0; byte[] buffer = new byte[ 1024 ]; while( (readLen = is.read(buffer)) > 0){ os.write(buffer, 0, readLen); } os.flush(); // upload ok --> insert this user into db Usr user = new Usr(); user.setUsrName(registerForm.getUsrname()); user.setOriFileName(fileName); user.setUuidFileName(uuidFileName); if(!(new UsrService().insertUsr(user))){ return mapping.findForward("errPage"); } return mapping.findForward("okPage"); }catch(IOException e){ e.printStackTrace(); return mapping.findForward("errPage"); }finally{ try { if( is != null ){ is.close(); } if( os != null ){ os.close(); } } catch (Exception e2) { e2.printStackTrace(); } } } }