웹 개발에서 여러 번 컴퓨터에서 웹 서버로 일부 파일을 업로드해야합니다.
예를 들어, BBS 시스템, 사용자 가이 시스템을 사용하면 시스템의 일부 사진과 문서를 서버에 업로드 할 수 있습니다. 그러면 다른 사용자 가이 파일을 다운로드 할 수 있으므로 이런 방식으로 파일 업로드를 스스로 프로그래밍 할 수 있습니다.
그러나 더 좋은 방법은 기존 구성 요소를 사용 하여이 업로드 기능을 구현하는 데 도움이됩니다.
일반적인 업로드 구성 요소 :
Apache의 Commons FileUpload
Javazoom의 업로드 비안
jspsmartupload
upload.jsp
암호;
<%@ page language = "java"contenttype = "text/html; charset = utf-8"%> <html> <head> <title> 커먼즈를 사용하여 파일을 업로드하기 위해 업로드 </title> </head> <tyle>* {font-family : "安体"; font-size : 14px} </style> <body> <p align = "center"> 업로드 할 파일을 선택하십시오 </p> <form id = "form1"name = "form1"method = "post"action = "servlet/fileservlet"enctype = "multipart/form-data"> <td> <td> uploader 이름 = "name"type = "text"id = "name"size = "20"> </td> </tr> <tr> <td> 파일 업로드 : </td> <td> <input name = "file ="file "size ="20 "> </td> </tr> <tr> <td> </td> <td> <input type =" "value =" "input type =" "value =" "input type =" "value =" "value" type = "Reset"name = "Reset"value = "Reset"> </td> </tr> </table> </form> </body> </html>FileUploadServlet.java 코드 :
/***/package com.b510.example; import java.io.file; import java.io.ioexception; import java.util.*; import javax.servlet.servletconfig; import javax.servlet.servletcontext; import javax.servlet.servletexception; javax.servlet.http.httpervletrequest; import javax.servlet.http.htttp.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;/** * * @author xhw * * @date 2011-7-26 * */public class pileUploadlet {private serial lerial serial serial serial serial serial serial serial htpervert { -774462534830285257L; Private ServletContext SC; 개인 문자열 savepath; public void doget (httpservletrequest request, httpservletresponse 응답)은 servletexception, ioexception {dopost (요청, 응답); } public void init (servletconfig config) {// web.xml = config.getInitParameter ( "SavePath")에서 설정 한 초기화 매개 변수 SavedPath 세트; sc = config.getServletContext (); } public void dopost (httpservletRequest 요청, httpservletResponse 응답) servletexception, ioexception {request.setcharacterencoding ( "utf-8"); DiskFileItemFactory Factory = 새로운 DiskFileItemFactory (); ServletFileUpload 업로드 = 새로운 ServletFileUpload (공장); try {list items = upload.parserequest (요청); 반복자 itr = items.iterator (); while (itr.hasnext ()) {fileitem item = (fileitem) itr.next (); if (item.isformfield ()) {system.out.println ( "양식 매개 변수 이름 :" + item.getfieldName () + ", 양식 매개 변수 값 :" + item.getString ( "UTF-8")); } else {if (item.getName ()! = null &&! item.getName (). equals ( "")) {System.out.println ( "파일 크기를 업로드 :" + item.getSize ()); System.out.println ( "파일 유형 업로드 :" + item.getContentType ()); // item.getName () 클라이언트 시스템에서 업로드 된 파일의 전체 경로 이름을 반환합니다. file tempfile = 새 파일 (item.getName ()); // 파일 업로드 경로 저장 파일 파일 = 새 파일 (sc.getRealPath ( "/") + savepath, tempfile.getName ()); item.write (파일); request.setAttribute ( "upload.message", "파일 업로드가 성공적으로 업로드!"); } else {request.setAttribute ( "upload.message", "업로드 파일 없음!"); }}}}} catch (fileUploadexception e) {e.printstacktrace (); } catch (예외 e) {e.printstacktrace (); request.setAttribute ( "upload.message", "파일 업로드 실패!"); } request.getRequestDisPatcher ( "/uploadResult.jsp"). FORMPER (요청, 응답); }}uploadResult.jsp 코드 :
<%@ page language = "java"import = "java.util.*"pageencoding = "utf-8"%> <! doctype html public "-// w3c // dtd html 4.01 전환 // content = "no-cache"> <meta http-equiv = "cache-control"content = "no-cache"> <meta http-equiv = "meta"content = "0"> <meta http-equiv = "keywords"content = "keyword1, keyword2, keyword3"> <meta http- equiv = "the inta" "description ="this is this. rel = "stylesheet"type = "text/css"href = "styles.css"rel = "external nofollow"> -> </head> </head> </head> </head> </head> </head> </head> </head> </head> </head> </head> </href = <a href = <a href = "/upload/uploadfile.jsp"rel = "external nofollow"> upload </ </html "
web.xml
<Servlet> <cression> 이것은 내 j2ee 구성 요소의 설명 </description> <display-name> 이건 내 j2ee 구성 요소의 디스플레이 이름 </display-name> <servlet-name> fileUploadServlet </servlet-name> <servlet-class> com.b510.example.fileUploadServlet </servlet-class> < <! -세트 초기화 매개 변수-> <init-param> <param-name> savepath </param-name> <param- value> 업로드 </param- value> </init-param> </servlet> <servlet-mapping> <servlet-name> fileUploadServlet </servlet-name> <Url-patter/servlet/filest </urlpattern>
이 기사는 "Java 업로드 작업 기술의 요약"으로 편집되었으며 모든 사람들은 배우고 읽을 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.