ก่อนอื่นให้ฉันแนะนำการอัปโหลดไฟล์
ชั้นเรียน
นำเข้า java.sql.timestamp; / ** * * @described ไฟล์อัปโหลดคลาสเอนทิตี * */ คลาสสาธารณะ upfile {รหัสส่วนตัว id; // รหัสหลักคีย์หลักจะสร้างสตริงส่วนตัว uuidName; // uuid ชื่อสตริงส่วนตัวชื่อไฟล์; // ชื่อไฟล์สตริงส่วนตัวบันทึก savepath; // บันทึก PATH PATH Private Timestamp Uploadtime; // อัปโหลดเวลาสตริงส่วนตัวคำอธิบาย; // ไฟล์คำอธิบายสตริงส่วนตัวชื่อผู้ใช้ชื่อ; // ชื่อผู้ใช้สาธารณะ upfile () {super (); } public upfile (string id, string uuidName, string filename, string savepath, timestamp uploadtime, คำอธิบายสตริง, ชื่อผู้ใช้สตริง) {super (); this.id = id; this.uuidName = uuidName; this.filename = ชื่อไฟล์; this.savepath = SavePath; this.uploadtime = uploadtime; this.description = คำอธิบาย; this.username = ชื่อผู้ใช้; } สตริงสาธารณะ getDescription () {return คำอธิบาย; } สตริงสาธารณะ getFilename () {return filename; } สตริงสาธารณะ getId () {return id; } สตริงสาธารณะ getSavePath () {return savepath; } timestamp สาธารณะ getUploadTime () {return uploadtime; } สตริงสาธารณะ getUserName () {ส่งคืนชื่อผู้ใช้; } สตริงสาธารณะ getUuidName () {return uuidName; } โมฆะสาธารณะ setDescription (คำอธิบายสตริง) {this.description = คำอธิบาย; } โมฆะสาธารณะ setFileName (ชื่อไฟล์สตริง) {this.filename = filename; } โมฆะสาธารณะ setId (รหัสสตริง) {this.id = id; } โมฆะสาธารณะ setSavePath (String savepath) {this.savepath = savepath; } โมฆะสาธารณะ setupLoadTime (timestamp uploadtime) {this.uploadtime = uploadtime; } โมฆะสาธารณะ setUserName (ชื่อผู้ใช้สตริง) {this.userName = ชื่อผู้ใช้; } โมฆะสาธารณะ setUuidName (string uuidName) {this.uuidName = uuidName; -หน้าหนังสือ
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%String Path = request.getContextPath (); String basepath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/"; %> <! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> my jsp 'upload.jsp' http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> <meta http-equiv = "คำหลัก" content = "keyword1, คำหลัก 2, คำหลัก type = "text/css" href = "styles.css">-> </head> <body> <h1> ไฟล์อัปโหลด </h1> <form action = "$ {pagecontext.request.contextpath}/upload" method = "post" enctype = "multipart/form-data type = "text" name = "username"/> </td> </tr> <tr> <td> ไฟล์อัปโหลด: </td> <td> <อินพุต type = "file" name = "file"/> </td> </tr> <td> <td> คำอธิบาย: </td> <td> </tr> <tr> <td> <อินพุต type = "ส่ง" value = "อัปโหลดเริ่มต้น"/> </td> </tr> </table> </form> <div> </div> <a href = "$ {pageContext.Request.ContextPath} เซล็ต
นำเข้า java.io.ioException; นำเข้า javax.servlet.servletexception; นำเข้า Javax.servlet.http.httpservlet; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.apache.Commons.FileUpload.FileUploadBase.FilesizElimitexceedEdException; นำเข้า org.apache.Commons.FileUpload.FileUploadBase.SizeLimitexceedEdException; นำเข้า org.apache.commons.fileupload.servlet.servletFileUpload; นำเข้า com.itheima.domain.upfile; นำเข้า com.itheima.exception.myexception; นำเข้า com.itheima.service.UpFileservice; นำเข้า com.itheima.service.impl.upfileserviceimpl; นำเข้า com.itheima.untils.webuntil; Public Class uploadFileservlet ขยาย httpservlet {โมฆะสาธารณะ doget (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) โยน servletexception, ioexception {dopost (คำขอ, การตอบกลับ); } โมฆะสาธารณะ dopost (คำขอ httpservletrequest, httpservletResponse การตอบสนอง) พ่น servletexception, ioexception {// ตัดสินว่ารูปแบบนั้นประกอบไปด้วยหลายส่วนหรือไม่ถ้า (servletfileupload.ismultipartcontent request.getRequestDispatcher ("/upload.jsp") ส่งต่อ (คำขอ, การตอบกลับ); กลับ ; } // หากเป็นหลายส่วน, get และ traverse เพื่อส่งคืนวัตถุอัพโหลดไฟล์ที่มีข้อมูลที่อัปโหลดทั้งหมดลอง {upfile upfile = webuntil.upload (คำขอ); Upfileservice Upfileservice = ใหม่ upfileserviceimpl (); ธงบูลีน = upfileservice.add (upfile); if (flag) {request.setAttribute ("msg", "อัปโหลดสำเร็จ"); request.getRequestDispatcher ("/upload.jsp") ส่งต่อ (คำขอ, การตอบกลับ); กลับ ; } else {request.setAttribute ("msg", "การอัปโหลดล้มเหลวโปรดลองอีกครั้ง"); request.getRequestDispatcher ("/upload.jsp") ส่งต่อ (คำขอ, การตอบกลับ); กลับ ; }} catch (filesizeLimitexceedEdException e) {e.printStackTrace (); request.setAttribute ("msg", "ขนาดไฟล์เดียวเกินขีด จำกัด สูงสุด"); request.getRequestDispatcher ("/upload.jsp") ส่งต่อ (คำขอ, การตอบกลับ); กลับ ; } catch (sizelimitexceedeDexception e) {e.printstacktrace (); request.setAttribute ("msg", "ขนาดไฟล์รวมเกินขีด จำกัด สูงสุด"); request.getRequestDispatcher ("/upload.jsp") ส่งต่อ (คำขอ, การตอบกลับ); กลับ ; -เครื่องมือ
นำเข้า Java.io.File; นำเข้า java.io.filenotfoundException; นำเข้า Java.io.FileOutputStream; นำเข้า java.io.ioException; นำเข้า Java.io.InputStream; นำเข้า Java.io.OutputStream; นำเข้า java.io.usupportencodingexception; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า java.util.uuid; นำเข้า Javax.servlet.http.httpservletRequest; นำเข้า org.apache.Commons.FileUpload.FileItem; นำเข้า org.apache.Commons.FileUpload.FileUploadBase; นำเข้า org.apache.Commons.FileUpload.FileUploadBase.FilesizElimitexceedEdException; นำเข้า org.apache.commons.fileupload.fileuploadexception; นำเข้า org.apache.commons.fileupload.progresslistener; นำเข้า org.apache.commons.fileupload.disk.diskfileitemfactory; นำเข้า org.apache.commons.fileupload.servlet.servletFileUpload; นำเข้า com.itheima.domain.upfile; นำเข้า com.itheima.exception.myexception; / ** * คลาสเครื่องมืออัปโหลดไฟล์ * @describe toDo * */ คลาสสาธารณะ webuntil {/ ** * วิธีการอัปโหลดไฟล์ * @param คำขอผ่านพารามิเตอร์คำขอใน * @return ส่งคืนวัตถุ upfile * @throws filesizeLimetexceedEdException * @throws filesizeLimitexceedEdException, sizelimitexceedeDexception {upfile upfile = new upfile (); ArrayList <String> fileList = initList (); ลอง {// รับไฟล์ดิสก์ไฟล์วัตถุโรงงาน DiskFileItemFactory โรงงาน = ใหม่ diskFileItemFactory (); String tmp = request.getSession (). getServletContext (). getRealPath ("/tmp"); System.out.println (TMP); // เริ่มต้น SetFactory โรงงาน (โรงงาน, TMP); // รับไฟล์อัปโหลดตัวแยกวิเคราะห์ servletFileUpload อัปโหลด = ใหม่ servletFileUpload (โรงงาน); // เริ่มต้น setupload ตัวแยกวิเคราะห์ (อัปโหลด); // รับรายการไฟล์ <fileItem> list = upload.parserequest (คำขอ); // travel for (fileitem items: list) {// ตัดสินว่ามันเป็นวัตถุแบบฟอร์มทั่วไปหรือไม่ถ้า (items.isformfield ()) {// รับชื่อของฟอร์มอัพโหลดสตริง fieldName = items.getfieldName (); // ค่าสตริง fieldValue = items.getString ("UTF-8"); // ตัดสินว่า ("ชื่อผู้ใช้" .Equals (FieldName)) {// set upfile.setUserName (FieldValue); } อื่นถ้า ("คำอธิบาย" .Equals (fieldName)) {// ตั้งค่าแอตทริบิวต์ upfile.setDescription (fieldValue); }} else {// เป็นไฟล์พร้อมที่จะอัปโหลด // รับชื่อสตริงชื่อไฟล์ filename = items.getName (); // จัดการความแตกต่างในชื่อไฟล์ที่ได้รับเนื่องจากเบราว์เซอร์ที่แตกต่างกัน IND INDEX = filename.lastindexof ("//"); if (index! =-1) {filename = filename.substring (ดัชนี+1); } // สร้างสตริงชื่อไฟล์สุ่ม uuidName = generateFileName (ชื่อไฟล์); // รับสตริงพา ธ ไฟล์ที่อัปโหลดบันทึก = request.getSession (). getServletContext (). getRealPath ("/web-inf/upload"); // รับสตรีมอินพุตในวัตถุคำขออินพุตสตรีมใน = items.getInputStream (); // ทำลายไฟล์และจัดเก็บในเส้นทางที่แตกต่างกันค้นหาเส้นทาง savepath = generaterandomdir (savepath, uuidName); // คัดลอกไฟล์ uploadfile (ใน, savepath, uuidname); string id = uuid.randomuuid (). toString (); upfile.setid (id); upfile.SetSavePath (SavePath); upfile.setUuidName (uuidName); upfile.setFilename (ชื่อไฟล์); // ล้างรายการแคช delete (); }}} catch (fileuploadbase.filesizeLimitexceedeDexception e) {// top a smemond throw e; } catch (fileuploadbase.sizeLimitexceedeDexception e) {// ข้อยกเว้นโยน e; } catch (fileuploadexception e) {e.printstacktrace (); } catch (unsupportencodingexception e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); } catch (exception e) {e.printstacktrace (); } return upfile; } / *** เริ่มต้นรายการไฟล์* @return* / ArrayList แบบคงที่ส่วนตัว <String> initList () {arrayList <String> list = new ArrayList <String> (); list.add (". jpg"); list.add (". rar"); list.add (". txt"); list.add (". png"); รายการคืน; } /*** คัดลอกไฟล์* อินพุตสตรีมใน @param ในรายการ* @param บันทึกพา ธ บันทึกพา ธ* @param uuidname ชื่อไฟล์* /โมฆะคงที่ส่วนตัว updoadfile (inputStream in, สตริงบันทึก, สตริง uuidName) {// รับไฟล์ไฟล์ = ไฟล์ใหม่ outputstream out = null; ลอง {int len = 0; ไบต์ [] buf = ไบต์ใหม่ [1024]; // รับเอาต์พุตสตรีมออก = ใหม่ fileOutputStream (ไฟล์); ในขณะที่ ((len = in.read (buf))! =-1) {out.write (buf, 0, len); }} catch (filenotfoundException e) {e.printStackTrace (); } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {ลอง {in.close (); } catch (ioexception e) {e.printstacktrace (); } ลอง {out.close (); } catch (ioexception e) {e.printstacktrace (); }}} /*** สร้างเส้นทางการจัดเก็บแบบสุ่ม* @param บันทึกพา ธ พา ธ* @param uuidName ชื่อ UUID ที่สร้างขึ้น* @return* ใช้ hashCode เพื่อให้เสร็จสิ้น* /ส่วนตัวสตริงคงที่ generateromdir System.out.println ("uuidName"+uuidName); int hashCode = uuidName.hashCode (); // container stringbuilder sb = new StringBuilder (); ในขณะที่ (hashCode> 0) {// int 15 int tmp = hashCode & 0xf; sb.append ("/"); sb.append (tmp+""); hashCode = hashCode >> 4; } // แยกเส้นทางสตริงเส้นทางใหม่ = savepath+sb.toString (); System.out.println ("Path"+Path); ไฟล์ไฟล์ = ไฟล์ใหม่ (พา ธ ); // ตัดสินว่าเส้นทางนั้นมีอยู่หรือไม่ถ้า (! file.exists ()) {// สร้าง file.mkdirs (); } // ส่งคืนเส้นทางการส่งคืนเส้นทาง; } / *** สร้างชื่อไฟล์ใหม่* @param uuidName ชื่อแบบสุ่มชื่อ* @param ชื่อไฟล์ชื่อเดิม* @return* / สตริงคงที่ส่วนตัว generateFilename (ชื่อสตริงไฟล์) {สตริง uuidName = uuid.randomuuid (). toString (); return uuidName.replace ("-", "") .toString ()+"_"+ชื่อไฟล์; } / *** การเริ่มต้น parser* @param อัปโหลด* / โมฆะคงที่ส่วนตัว setupload (การอัปโหลด servletFilePilePload) {// ตั้งค่าการเข้ารหัสอักขระอัปโหลด Setheaderencoding ("UTF-8"); // ตั้งค่าขนาดไฟล์อัปโหลด SetFilesizEmax (1024*1024*20); // ตั้งค่าขนาดไฟล์ทั้งหมดอัปโหลด SetsizeMax (1024*1024*50); // ตั้งค่าความคืบหน้าของผู้ฟัง upload.setProgressListener (ใหม่ progressionListener () {การอัปเดตโมฆะสาธารณะ (ยาว pbytesread, pcontentlength ยาว, int pitems) {system.out.println ("อ่าน:"+pbytesRead+"ทั้งหมด:"+pcontentlength " โรงงาน*@Param TMP บัฟเฟอร์ไดเรกทอรี*/ โมฆะคงที่ setFactory (DiskFileItemfactory Factory, String TMP) {/// กำหนดค่าเริ่มต้นของบัฟเฟอร์ค่าเริ่มต้นโรงงาน Setsizethreshold (1024*1024) ดาวน์โหลดสองไฟล์
เซล็ต
ชั้นเรียนสาธารณะ DownUpFileservlet ขยาย Httpservlet {โมฆะสาธารณะ doget (httpservletrequest คำขอ, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {dopost (คำขอ, การตอบสนอง); } โมฆะสาธารณะ dopost (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {// รับ id id id = request.getParameter ("id"); // อินเทอร์เฟซของเลเยอร์ธุรกิจ Upfileservice Upfileservice = ใหม่ upfileserviceimpl (); // ค้นหาวัตถุนี้ตาม ID upfile upfile = upfileservice.findupFileById (ID); if (upfile == null) {return; } // รับชื่อจริงของชื่อไฟล์สตริงไฟล์ = upfile.getFilename (); // หากมีภาษาจีนในชื่อไฟล์จะต้องถูกแปลงรหัสมิฉะนั้นจะไม่มีชื่อไฟล์ชื่อไฟล์ = urlencoder.encode (ชื่อไฟล์ "UTF-8"); // เปลี่ยนชื่อสตริง uuidName = upfile.getUuidName (); // บันทึกสตริงพา ธ savepath = upfile.getSavePath (); ไฟล์ file = ไฟล์ใหม่ (savepath, uuidName); // พิจารณาว่าไฟล์มีอยู่ถ้า (! file.exists ()) {request.setAttribute ("msg", "ไฟล์ที่ดาวน์โหลดหมดอายุแล้ว"); request.getRequestDispatcher ("/index") ส่งต่อ (คำขอ, การตอบกลับ); กลับ; } // ตั้งค่าไฟล์ตอบกลับการตอบสนองส่วนหัวการตอบกลับ setheader ("เนื้อหา-การจัดสรร", "แนบ; filename ="+ชื่อไฟล์); // ใช้ IO Stream เพื่อเอาต์พุตอินพุตใน = ใหม่ FileInputStream (ไฟล์); servletoutputStream out = response.getOutputStream (); int len = 0; ไบต์ [] buf = ไบต์ใหม่ [1024]; ในขณะที่ ((len = in.read (buf))! =-1) {out.write (buf, 0, len); } in.close (); -ฐานข้อมูล
สร้างฐานข้อมูล upload_download_exercise; ใช้ upload_download_exercise; สร้างตาราง upfiles (id varchar (100), // ใช้ uuid เพื่อสร้าง uuidname varchar (255), // uuid บวกชื่อไฟล์ต้นฉบับชื่อไฟล์ filename varchar (100), // ชื่อไฟล์จริงบันทึก Varchar (255), // บันทึกการอัปโหลดเวลาอัปโหลด
ข้างต้นเป็นเนื้อหาที่เกี่ยวข้องของ Java โดยใช้ Commos-Fileupload เพื่อให้ได้การอัปโหลดและดาวน์โหลดไฟล์ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน