JSP簡單練習-上傳檔案,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>
jsp簡單上傳代碼
servlet檔案上傳
login.jsp
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
輸入使用者名稱<input typ="text" name ="username">
<input type="file"name="file"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>上傳結果頁面</title>
</head>
<body>
username:${requestScope.username }
filename:${requestScope.file }
</body>
</html>
UploadServlet.java
package com.test.servlet......餘下全文>>
JSP做的簡單的檔案上傳下載代碼
暈,給你寫完了,人怎麼沒了呢……