DWR第五篇之檔案上傳

來源:互聯網
上載者:User

標籤:body   type   new   public   version   inpu   comm   bst   load   

1. 在第一篇架構基礎上進行

2. 修改maven依賴

 1 <dependencies> 2     <dependency> 3         <groupId>org.directwebremoting</groupId> 4       <artifactId>dwr</artifactId> 5       <version>3.0.1-RELEASE</version> 6     </dependency> 7     <dependency> 8       <groupId>commons-logging</groupId> 9       <artifactId>commons-logging</artifactId>10       <version>1.2</version>11     </dependency>12     <dependency>13         <groupId>commons-fileupload</groupId>14         <artifactId>commons-fileupload</artifactId>15         <version>1.3.1</version>16     </dependency>17     <dependency>18         <groupId>commons-io</groupId>19         <artifactId>commons-io</artifactId>20         <version>2.4</version>21     </dependency>22 </dependencies>

3. 編寫jsp頁面

 1 <html> 2 <head> 3 <base href="<%=basePath%>"> 4  5 <title>dwr</title> 6 <script type=‘text/javascript‘ src=‘dwr/engine.js‘></script> 7 <script type=‘text/javascript‘ src=‘dwr/util.js‘></script> 8 <script type=‘text/javascript‘ src=‘dwr/interface/CoreServlet.js‘></script> 9 </head>10 <body>11 12     <input type="file" name="file" />13     <button onclick="upload();">上傳</button>14 15 </body>16 <script type="text/javascript">17     function upload() {18         var file = dwr.util.getValue("file");  19         CoreServlet.uploadFile(file, file.value, function(data) {20             if (data == true) {21                 alert("上傳成功!");22             }23         });24     }25 </script>26 </html>

4. 編寫後台代碼:

 1 public class CoreServlet { 2  3     public boolean uploadFile(InputStream is, String path) throws Exception { 4         String fileName = path.substring(path.lastIndexOf("\\") + 1, path.length()); 5         FileOutputStream fos = new FileOutputStream(new File("E://" + fileName)); 6         byte[] b = new byte[1024]; 7         while ((is.read(b)) != -1) { 8             fos.write(b); 9         }10         is.close();11         fos.close();12         return true;13     }14 15 }

DWR第五篇之檔案上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.