Servlet上傳下載

來源:互聯網
上載者:User

標籤:for   inpu   沒有   value   char   getc   類型   table   conf   

下面是一個jsp頁面,表單中有4個檔案輸入文字框,可以同時上傳4個檔案:
<form action="file?file=upLoadByjs" method="post" ENCTYPE="multipart/form-data">
<input type="file" name=file1" size="30"><Br>
<input type="file" name=file2" size="30"><Br>
<input type="file" name=file3" size="30"><Br>
<input type="file" name=file4" size="30"><Br>
<input type="submit" value="上傳">
</form>
當然可以設定同時上傳更多的檔案......
servlet處理常式:
1String path ="D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad";
2 //建立一個jsmartUpLoad對象
3 SmartUpload smartUpload = new SmartUpload();
4 //上傳初始化
5 smartUpload.initialize(this.getServletConfig(),request,response);
6 try {
7 //設定上傳限制
8 //限制每個上傳檔案的最大長度;將最大設定為1024*1024*20
9 smartUpload.setMaxFileSize(1024*1024*10);
10 //限制總上傳資料的長度
11 smartUpload.setTotalMaxFileSize(1024*1024*20);
12 //限制允許上傳的檔案類型、允許doc、txt、bat檔案
13 smartUpload.setAllowedFilesList("doc,txt,bat");
14 //限制禁止上傳的檔案類型,禁止exe、jsp、和沒有副檔名的檔案
15 smartUpload.setDeniedFilesList("exe,jsp,,");
16 //上傳檔案
17 smartUpload.upload();
18 //將檔案儲存到指定的目錄下
19 smartUpload.save(path);
20 } catch (SQLException e) {
21 e.printStackTrace();
22 } catch (SmartUploadException e) {
23 e.printStackTrace();
24 }
25
26 //逐一提取檔案資訊,同時輸出上傳檔案的資訊
27 for (int i = 0; i < smartUpload.getFiles().getCount(); i++) {
28 com.jspsmart.upload.File myFile =smartUpload.getFiles().getFile(i);
29 //若檔案表單中的檔案選項沒有選擇檔案則繼續
30 if(myFile.isMissing())
31 continue;
32 //顯示當前檔案的資訊
33 response.setContentType("text/html;charset=utf-8");
34 PrintWriter out = response.getWriter();
35 out.println("<table border=‘1‘>");
36 out.println("<tr><td>表單選項</td><td>"+myFile.getFieldName()+"</td></tr>");
37 out.println("<tr><td>檔案長度:</td><td>"+myFile.getSize()+"</td></tr>");
38 out.println("<tr><td>檔案名稱</td><td>"+myFile.getFileName()+"</td></tr>");
39 out.println("<tr><td>副檔名</td><td>"+myFile.getFileExt()+"</td></tr>");
40 out.println("<tr><td>檔案全名</td><td>"+myFile.getFilePathName()+"</td></tr>");
41 out.println("</table><br>");
42 }
該程式直接使用SmartUploa對象來實現檔案上傳。在申請對象後,必須要對其進行初始化:smartUpload.initialize(this.getServletConfig(),request,response);

使用jspSmartUpload組件進行檔案下載,可以非常簡單:
jsp頁面:

<a href="${pageContext.request.contextPath }/file1?file=downByJsmart&name=user.txt">下載user</a>
1 //擷取下載檔案名稱
2 String fileName = request.getParameter("name");
3 //建立一個smartUpload對象
4 SmartUpload smartUpload = new SmartUpload();
5 //初始化
6 smartUpload.initialize(this.getServletConfig(), request, response);
7 //設定contentDisposition為null以禁止瀏覽器自動開啟檔案
8 //保證單擊連結後是下載檔案。
9 smartUpload.setContentDisposition(null);
10 //下載檔案
11 try {
12 smartUpload.downloadFile("D:/work space/JSP workspace/jsp_servlet_upAndLoad/file/upLoad/"+fileName);
13 } catch (SmartUploadException e) {
14 e.printStackTrace();
15 }

Servlet上傳下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.