JSP簡單練習-上傳檔案,jsp練習上傳檔案

來源:互聯網
上載者:User

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做的簡單的檔案上傳下載代碼

暈,給你寫完了,人怎麼沒了呢……
 

聯繫我們

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