您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)J2EE如何實(shí)現(xiàn)Servlet上傳文件到服務(wù)器并相應(yīng)顯示功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
編輯上傳文件的頁面upload.html
注意事項(xiàng):上傳方式使用POST不能使用GET(GET不能上傳文件)
表單 enctype 屬性應(yīng)該設(shè)置為 multipart/form-data.(表示提交的數(shù)據(jù)是二進(jìn)制文件)
upload.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上傳</title> </head> <body> <form action="UploadPhotoServlet" method="POST" enctype="multipart/form-data"> 人物名稱:<input type="text" name="heroName"/><br> 上傳頭像:<input type="file" name="filepath"/><br> <input type="submit" value="上傳"> </form> </body> </html>
UPloadPtotoServlet文件上傳類--上傳功能的開發(fā)
將commons-io-1.4.jar和commons-fileupload-1.2.2.jar 兩個(gè)jar包放到WEB-INF/lib 目錄下。
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class UploadPhotoServlet */ @WebServlet("/UploadPhotoServlet") public class UploadPhotoServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UploadPhotoServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // response.getWriter().append("Served at: ").append(request.getContextPath()); String filename=null; DiskFileItemFactory factory=new DiskFileItemFactory(); //磁盤文件條目工廠 ServletFileUpload upload=new ServletFileUpload(factory); //負(fù)責(zé)處理上傳的文件數(shù)據(jù),并將表單中每個(gè)輸入項(xiàng)封裝成一個(gè)fileitem對(duì)象中 //設(shè)置上傳文件的大小為10M factory.setSizeThreshold(2*1024*1024); List items=null; try { //parse 解析 items=upload.parseRequest(request); //得到一個(gè)保存了所有上傳內(nèi)容的List對(duì)象 } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } Iterator iter=items.iterator(); //迭代上傳的文件數(shù)據(jù) while(iter.hasNext()){ FileItem item=(FileItem) iter.next(); if(!item.isFormField()){ //如果不是上傳的 //根據(jù)時(shí)間戳創(chuàng)建頭像文件 filename=System.currentTimeMillis()+".jpg"; //通過getrealpath獲取上傳文件夾,如果項(xiàng)目存在將存在當(dāng)前項(xiàng)目下 不存在的話創(chuàng)建項(xiàng)目文件夾 //圖片文件夾 String photoFolder=request.getServletContext().getRealPath("uploaded"); File f=new File(photoFolder,filename); f.getParentFile().mkdirs(); //如果父文件夾不存在則自動(dòng)創(chuàng)建 //通過item.getInputStream() 獲取瀏覽器上傳的文件 InputStream is = item.getInputStream(); //將文件讀進(jìn)來 //復(fù)制文件 FileOutputStream fos=new FileOutputStream(f); //往界面上顯示 byte[] b=new byte[2*1024*1024]; int len=0; while((len=is.read(b))!=-1){ fos.write(b, 0, len); } fos.close(); }else{ System.out.println(item.getFieldName());//heroName String value=item.getString(); value=new String(value.getBytes("ISO-8859-1"), "UTF-8"); System.out.println(value); //桑葚 } } String html="<img width='200' height='150' src='uploaded/%s'/>"; response.setContentType("text/html"); PrintWriter pw=response.getWriter(); pw.format(html, filename); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
運(yùn)行結(jié)果:
關(guān)于“J2EE如何實(shí)現(xiàn)Servlet上傳文件到服務(wù)器并相應(yīng)顯示功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。