Upload File trong JSP | 91 bài học JSP miễn phí hay nhất jsp file uploading

5 127 0
Upload File trong JSP | 91 bài học JSP miễn phí hay nhất jsp file uploading

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/jsp  /index.jsp                                                                        Copyright  ©  vietjack.com     Upload File JSP Một JSP sử dụng với thẻ HTML form phép người dùng upload file tới Server Một file tải lên file dạng text, nhị phân hình ảnh tài liệu khác Tạo File Upload Form JSP HTML code sau tạo Upload Form Sau điểm quan trọng cần ghi nhớ: • Thuộc tính Phương thức thẻ form nên thiết lập phương thức POST phương thức GET sử dụng • Thuộc tính enctype thẻ form nên thiết lập multipart/form-data • Thuộc tính action thẻ form nên thiết lập JSP file mà xử lý việc upload file Backend Server Ví dụ sau sử dụng chương trình uploadFile.jsp để upload file • Để upload file đơn, bạn nên sử dụng thẻ đơn với thuộc tính type=”file” Để cho phép upload nhiều file lên, bạn sử dụng nhiều thẻ input với giá trị thuộc tính khác Trình duyệt liên kết nút Browse với giá trị File Uploading Form File Upload: Select a file to upload: Nó hiển thị kết sau, cho phép bạn chọn file từ PC nội người dùng click “Upload File”, Form đệ trình với file chọn: http://vietjack.com/                                                                                                                            Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/jsp  /index.jsp                                                                        Copyright  ©  vietjack.com     File Upload: Select a file to upload: Ghi chú: Form form giả không làm việc, nên thử code thiết bị bạn để làm cho làm việc Viết Backend JSP Script Đầu tiên, định nghĩa vị trí để lưu giữ file upload Bạn mã hóa điều chương trình bạn tên thư mục thêm vào sử dụng cấu hình ngoại vi chẳng hạn phần tử context-param web.xml sau: Location to store uploaded file file-upload c:\apache-tomcat-5.5.29\webapps\data\ Sau source code cho UploadFile.jsp, mà xử lý nhiều thao tác tải file lên thời điểm Trước tiến hành, bạn chắn rằng: • Ví dụ sau phụ thuộc vào FileUpload, chắn bạn sử dụng phiên commonsfileupload.x.x.jar file Classpath bạn Bạn tải từ: http://commons.apache.org/fileupload/ • FileUpload phụ thuộc vào Commons IO, thể đảm bảo bạn có phiên commons-io-x.x.jar file Classpath bạn Bạn download tại: http://vietjack.com/                                                                                                                            Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/jsp  /index.jsp                                                                   Copyright â vietjack.com http://commons.apache.org/io/ Trong kiểm nghiệm ví dụ này, bạn nên upload file mà có kích cỡ nhỏ maxFileSize, khơng file khơng tải lên • Bạn chắn tạo thư mục c:\temp c:\apache-tomcat5.5.29\webapps\data = 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize factory.setRepository(new File("c:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded upload.setSizeMax( maxFileSize ); try{ // Parse the request to get file items List fileItems = upload.parseRequest(request); http://vietjack.com/                                                                                                                            Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/jsp  /index.jsp                                                                        Copyright  ©  vietjack.com     // Process the uploaded file items Iterator i = fileItems.iterator(); out.println(""); out.println(""); out.println("JSP File upload"); out.println(""); out.println(""); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if( fileName.lastIndexOf("\\") >= ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + ""); } } out.println(""); out.println(""); }catch(Exception ex) { System.out.println(ex); } }else{ out.println(""); out.println(""); out.println("Servlet upload"); out.println(""); out.println(""); out.println("

No file uploaded

"); http://vietjack.com/                                                                                                                            Trang  chia  sẻ  các  bài  học  online  miễn  phí     http://vietjack.com/jsp  /index.jsp                                                                        Copyright  ©  vietjack.com     %> } out.println(""); out.println(""); Bây giờ, bạn thử upload file sử dụng HTML form bạn tạo Khi bạn thử http://localhost:8080/UploadFile.htm, cho kết sau: File Upload: Select a file to upload: Nếu JSP script làm việc tốt, file bạn tải lên thư mục c:\apache-tomcat-5.5.29\webapps\data\   http://vietjack.com/                                                                                                                            Trang  chia  sẻ  các  bài  học  online  miễn  phí     ... new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded upload. setSizeMax( maxFileSize ); try{ // Parse the request to get file. .. Write the file if( fileName.lastIndexOf("\") >= ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\")+1))... giờ, bạn thử upload file sử dụng HTML form bạn tạo Khi bạn thử http://localhost:8080/UploadFile.htm, cho kết sau: File Upload: Select a file to upload: Nếu JSP script làm việc tốt, file bạn tải

Ngày đăng: 02/12/2017, 22:04

Tài liệu cùng người dùng

Tài liệu liên quan