日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件2. 文件重命名

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件2. 文件重命名 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

servlet對本機已存在的文件進行重命名。

.jsp界面

1 <form action="<%=basePath %>fileAction" method="get" > 2 <table> 3 <tr> 4 <td>輸入文件路徑</td> 5 <td><input type="text" name="filePath" /></td> 6 </tr> 7 <tr> 8 <td>新文件名</td> 9 <td><input type="text" name="newName"/></td> 10 </tr> 11        <tr> 12 <td colspan="2"> 13             <input type="submit" value="修改"/> 14          </td> 15 </tr> 16        <tr> 17 <td colspan="2"><input type="hidden" name="method" value="file"/></td> 18 </tr> 19 </table> 20 </form>

?

?

servlet層FileAction.java代碼

1 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 2 throws ServletException, IOException { 3 req.setCharacterEncoding("utf-8"); 4 resp.setCharacterEncoding("utf-8"); 5 resp.setContentType("text/html;charset=utf-8"); 6 PrintWriter out=resp.getWriter(); 7 String method=req.getParameter("method"); 8 9 if(method.equals("file")) { 10 //對文件進行操作 11 12 13 /* 14 * 判斷文件是否存在 15 */ 16 //    1. 獲取請求中的文件路徑(get方式,獲得表單中的含有中文的值) 17 // 若是英文,則直接用req.getParameter("name"); 18 String filePath=new String(req.getParameter("filePath").getBytes("iso8859-1"),"utf-8"); 19 // 2. 根據路徑定義相應的文件 20 File file=new File(filePath); 21 // 3. 使用exists();函數,判斷生成的文件是否存在 22 boolean bool=file.exists(); 23 if(bool==true) { 24   /* 25 * 文件重命名renameTo(); 26 * // 1. 獲取新的文件名 27         String newName =new String(req.getParameter("newName").getBytes("ISO8859-1"),"UTF-8"); 28         2. 根據源文件父路徑和新的文件名,組合創建一個新的File對象 29         File newFile=new File(file.getParent()+File.separator+newName); 30         boolean reName=file.renameTo(newFile); 31       if(reName) 32            out.write("<script>alert('文件重命名成功!');</script>"); 33     } else { 34 out.write("<script>alert('該文件不存在!');</script>"); 35 }
      }

    }

web.xml

1 <servlet> 2 <servlet-name>fileAction</servlet-name> 3 <servlet-class>servlet.FileAction</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>fileAction</servlet-name> 7 <url-pattern>/fileAction</url-pattern> 8 </servlet-mapping>

?

轉載于:https://www.cnblogs.com/hyystudy/p/8046694.html

總結

以上是生活随笔為你收集整理的文件2. 文件重命名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。