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(); } } } }