JSP簡單練習-上傳檔案

來源:互聯網
上載者:User

標籤:jsp上傳檔案介面

注意:在編寫上傳檔案的代碼時,需確保"WEB-INF/lib"下含有jspsmartupload.jar包,否則會出錯。

jspSmartupload.jar下載

<!-- uploadfileform.jsp --><%@ page language="java" contentType="text/html; charset=gb2312" %><html><head><title>上傳檔案</title></head><body><form method="post" action="uploadfileaccept.jsp" name="book" enctype="multipart/form-data"><table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474"><tr><td align="center" colspan="2">錄入書籍資訊</td></tr><tr><td width="150">書名:</td><td width="324"><input type="text" name="bookname"></td></tr><tr><td>封面:</td><td><input type="file" name="photofile"></td></tr><tr><td>作者:</td><td><input type="text" name="author"></td></tr><tr><td>出版社:</td><td><input type="text" name="publisher"></td></tr><tr><td colspan="2" width="474"><input type="submit" value="提交"></td></tr></table></form></body></html>

<!-- uploadfileaccept.jsp --><%@ page language="java" contentType="text/html; charset=gb2312" %><%@page import="com.jspsmart.upload.*"%><html><head><title>上傳檔案</title></head><body><%// 建立一個SmartUpload對象SmartUpload su=new SmartUpload();// 上傳初始化,pageContext為JSP的內建對象su.initialize(pageContext);// 設定允許上傳的檔案(通過副檔名限制),僅允許jpg,bmp,gif檔案su.setAllowedFilesList("jpg,bmp,gif");// 上傳檔案su.upload();// 將上傳檔案全部儲存到指定目錄su.save("/upload",SmartUpload.SAVE_VIRTUAL);%><table cellspacing="0" cellpadding="3" border="1" width="474"><tr><td width="150">書名:</td><td width="324"><%=su.getRequest().getParameter("bookname") %></td></tr><tr><td>封面</td><td><img src="<%=su.getFiles().getFile(0).getFileName() %>" ></td></tr><tr><td>作者:</td><td><%=su.getRequest().getParameter("author") %></td></tr><tr><td>出版社:</td><td><%=su.getRequest().getParameter("publisher") %></td></tr></table></body></html>


聯繫我們

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