javascript
jsp servlet示例_Servlet和JSP中的文件上传示例
jsp servlet示例
使用Servlet和JSP將文件上傳到服務(wù)器是Java Web應(yīng)用程序中的常見任務(wù)。 在對Servlet或JSP進行編碼以處理文件上傳請求之前,您需要了解一點有關(guān)HTML和HTTP協(xié)議中文件上傳支持的知識。 如果要讓用戶從文件系統(tǒng)中選擇文件并上傳到服務(wù)器,則需要使用<input type =” file” />。 這將使您能夠選擇任何文件形式的文件系統(tǒng)并上傳到服務(wù)器。 接下來的事情是,表單方法應(yīng)為enctype為multipart / form-data的 HTTP POST ,這將使文件數(shù)據(jù)在請求正文中的各個部分中可用。 現(xiàn)在,為了讀取這些文件部分并在Servlet中創(chuàng)建文件,可以使用ServletOutputStream完成。 最好使用Apache commons FileUpload (一個開放源代碼庫)。 當(dāng)您將表單方法的發(fā)布和內(nèi)容類型設(shè)置為“ multipart / form-data”時,Apache FileUpload處理解析HTTP請求的所有低級細(xì)節(jié),這些細(xì)節(jié)均會確認(rèn)RFC 1867或“ HTML中基于表單的文件上傳 ”。
要點:
Java Servlet和JSP中的文件上傳示例
這是使用Servlet和JSP在Java Web應(yīng)用程序中上傳文件的完整代碼。 此文件上傳示例需要四個文件:
FileUploadHandler.java
import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload;/*** Servlet to handle File upload request from Client* @author Javin Paul*/ public class FileUploadHandler extends HttpServlet {private final String UPLOAD_DIRECTORY = "C:/uploads";@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//process only if its multipart contentif(ServletFileUpload.isMultipartContent(request)){try {List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);for(FileItem item : multiparts){if(!item.isFormField()){String name = new File(item.getName()).getName();item.write( new File(UPLOAD_DIRECTORY + File.separator + name));}}//File uploaded successfullyrequest.setAttribute("message", "File Uploaded Successfully");} catch (Exception ex) {request.setAttribute("message", "File Upload Failed due to " + ex);} }else{request.setAttribute("message","Sorry this Servlet only handles file upload request");}request.getRequestDispatcher("/result.jsp").forward(request, response);}}index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>File Upload Example in JSP and Servlet - Java web application</title></head><body> <div><h3> Choose File to Upload in Server </h3><form action="upload" method="post" enctype="multipart/form-data"><input type="file" name="file" /><input type="submit" value="upload" /></form> </div></body> </html>result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>File Upload Example in JSP and Servlet - Java web application</title></head><body> <div id="result"><h3>${requestScope["message"]}</h3></div></body> </html>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/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>FileUploadHandler</servlet-name><servlet-class>FileUploadHandler</servlet-class></servlet><servlet-mapping><servlet-name>FileUploadHandler</servlet-name><url-pattern>/upload</url-pattern></servlet-mapping><session-config><session-timeout>30</session-timeout></session-config><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>總之,在使用Java Web應(yīng)用程序上傳文件時,請記住三件事
相依性
為了在任何Web服務(wù)器(例如Tomcat)中編譯并運行此Java Web應(yīng)用程序,您需要在WEB-INF lib文件夾中包括以下依賴項JAR。
commons-fileupload-1.2.2.jar
commons-io-2.4.jar
如果您正在使用Maven,則還可以使用以下依賴項:
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.2</version> </dependency> <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version> </dependency> 這就是如何在Java Web應(yīng)用程序中使用Servlet和JSP上傳文件的全部內(nèi)容。 可以使用JSP,Filter或Servlet編寫此文件上載示例,因為這三個都是Java Web應(yīng)用程序中請求的入口點。 為了簡單起見,我已經(jīng)使用Servlet處理文件上傳請求。 通過Servlet 3.0 API,Servlet可以支持多部分表單數(shù)據(jù),您可以使用HttpServletRequest的getPart()方法來處理文件上傳。
翻譯自: https://www.javacodegeeks.com/2013/08/file-upload-example-in-servlet-and-jsp.html
jsp servlet示例
總結(jié)
以上是生活随笔為你收集整理的jsp servlet示例_Servlet和JSP中的文件上传示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 郁闷!华尔街日报:iPhone 15未能
- 下一篇: 因收到“死亡威胁”,Unity 位于得州