JSP 및 Serlvet을 사용하여 참조를 위해 가장 간단한 업로드를 달성하십시오. 특정 내용은 다음과 같습니다
1. Page Index.jsp
<%@ page language = "java"pageencoding = "utf-8"%> <%string path = request.getContextPath (); String BasePath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerport ()+path+"/"; %> <! doctype html public "-// w3c // dtd html 4.01 전환 // en"> <html> <head> <base href = "<%= basepath%>"> <title> index.jsp </title> <meta http-equiv = "pragma"> <meta ache "> http-equiv = "cache-control"content = "no-cache"> <meta http-equiv = "contment ="0 "> </head> <body> <form action ="업로드 "메소드 ="enctype = "post"enctype = "intut type"name = "un up"file "/> <br> 이름 = "pic"/> <br> <입력 유형 = "제출"value = "제출"> </input> </body> </html>
2. 작업은 업로드 서블릿으로 이동하므로 web.xml, web.xml에서 구성해야합니다.
<? xml version = "1.0"encoding = "utf-8"?> <web-app version = "2.5"xmlns = "http://java.sun.com/xml/xml/ns/javaee"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance. xsi : schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name> upload-name> <Servlet-class> com.pop.action.smartuploadaction </servlet-class> </servlet> <servlet-mapping> <servlet-name> 업로드 됨 </servlet-name> <Url-pattern>/upload/*</url-pattern> </servlet-mapping> <welcome-file-list> index.jsp>
3. 액션 파일, smartuploadaction.java에 대한지도 :
패키지 com.pop.action; import java.io.ioexception; javax.servlet.servletexception import; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.jsp.jspfactory; import javax.servlet.jsp.pagecontext; com.soft4j.httpupload4j.request 가져 오기; com.soft4j.httpupload4j.smartupload import; com.soft4j.httpupload4j.smartuploadexception 가져 오기; 공개 클래스 SmartUploadaction은 httpservlet을 확장합니다. @override protected void dopost (httpservletrequest req, httpservletresponse resp) servletexception, ioexception {req.setcharacterencoding ( "utf-8"); resp.setcharacterencoding ( "UTF-8"); smartUpload su = new smartUpload (); // multipart/form-data의 전송으로 인해 req를 사용할 수 없으므로 smartUpload에서 생성 된 요청은 요청 reqest = su.getRequest (); // pageContext 객체 객체 pagecontext pagecontext = jspfactory.getDefaultFactory () .getPageContext (req, resp, null, true, 8192, true); su.initialize (pagecontext); try {su.upload (); //이 프로젝트의 업로드 디렉토리에 업로드 Su.Save ( "업로드"); } catch (smartUploadexception e) {e.printstacktrace (); } // smartUpload에서 생성 된 reqest 객체를 사용하여 페이지로 전달되는 매개 변수를 얻습니다. System.out.println (uname); }}최종 설명 : 사용 된 구성 요소 패키지는 SmartUpload.zip입니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.